97. 约数之和

题目链接

97. 约数之和

假设现在有两个自然数 ABSAB 的所有约数之和。

请你求出 Smod9901 的值是多少。

输入格式

在一行中输入用空格隔开的两个整数 AB

输出格式

输出一个整数,代表 Smod9901 的值。

数据范围

0A,B5×107

输入样例:

2 3

输出样例:

15

注意: AB 不会同时为 0

解题思路

分治

先将 a 用算术基本定理表示为 p1c1×p2c2××pncn,则 ab=p1bc1×p2bc2××pnbcn,其约数之和为 (1+p1+p12++p1bc1)×(1+p2+p22++p2bc2)××(1+pn+pn2++pnbcn),相当于求解 sum(p,c)=1+p+p2++pc
分情况讨论:

  • n 为奇数,sum(p,c)=sum(p,n/2)+sum(p,c/2)×pn/2+1

  • n 为偶数,sum(p,c)=sum(p,n/21)+p(n/2)×sum(p,n/21)+pn

sa 算术基本定理下的幂次之和,则:

  • 时间复杂度:O(loga×log(bs))

代码

// Problem: 约数之和 // Contest: AcWing // URL: https://www.acwing.com/problem/content/99/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int mod=9901; int ksm(int a,int b,int p) { int res=1%p; while(b) { if(b&1)res=1ll*res*a%p; a=1ll*a*a%p; b>>=1; } return res; } int sum(int p,int c) { if(c==0)return 1; if(c&1)return sum(p,c/2)*(ksm(p,c/2+1,mod)+1)%mod; return (sum(p,c/2-1)*(1+ksm(p,c/2,mod))+ksm(p,c,mod))%mod; } int a,b,res=1; int main() { cin>>a>>b; if(!a) { cout<<0; return 0; } for(int i=2;i<=a/i;i++) { if(a%i==0) { int cnt=0; while(a%i==0)cnt++,a/=i; res=res*sum(i,cnt*b)%mod; } } if(a>1) res=res*sum(a,b)%mod; cout<<res; return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16095163.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示