POJ 1845 --Sumdiv 约数基本定理+乘法逆元
来源:互联网 发布:word mac破解版 编辑:程序博客网 时间:2024/06/05 08:59
Description
Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S modulo 9901 (the rest of the division of S by 9901).
Input
The only line contains the two natural numbers A and B, (0 <= A,B <= 50000000)separated by blanks.
Output
The only line of the output will contain S modulo 9901.
Sample Input
2 3
Sample Output
15
Hint
2^3 = 8.
The natural divisors of 8 are: 1,2,4,8. Their sum is 15.
15 modulo 9901 is 15 (that should be output).
The natural divisors of 8 are: 1,2,4,8. Their sum is 15.
15 modulo 9901 is 15 (that should be output).
题目大意:给你两个整数A,B。然后求A^B的所有约数的和。
题目解析:根据约数基本定理如何一个自然数都可以分解为A=(a1^n1)*(a2^n2)*(a3^n3)...(am^nm) 的形式,其中a1,a2,a3......为质数。所有约数的个数为(a1+1)*(a2+1).....*(am+1).而所有约数的和为(a1^0+a1^1+a1^2……a1^n1)*(a2^0+a2^1+a2^2……a2^n2)……*(am^0+am^1+am^2……am^nm)
然后就可以根据等比数列求每一项的和然后相乘 即(a1^(n1+1)-1)/(a1-1) mod 9901 根据前面逆元的知识:a/b mod c = (a mod (b*c))/ b就可以得到 (a1^(n1+1)-1)mod (9901*(a1-1)) / (a1-1)。
AC代码:
#include<iostream>#include<cstring>#include<cstdio>using namespace std;typedef long long LL;const int N=10005;const int MOD=9901;bool prime[N];int p[N];int cnt;void isprime(){cnt=0;memset(prime,true,sizeof(prime));for(int i=2;i<N;i++){if(prime[i]){p[cnt++]=i;for(int j=i+i;j<N;j+=i){prime[j]=false;}}}}LL multi(LL a,LL b,LL m){LL ans=0;a%=m;while(b){if(b&1){ans=(ans+a)%m;b--;}b>>=1;a=(a+a)%m;}return ans;}LL quick_mod(LL a,LL b,LL m){LL ans=1;a%=m;while(b){if(b&1){ans=multi(ans,a,m);b--;}b>>=1;a=multi(a,a,m);}return ans;}void Solve(LL A,LL B){LL ans=1;for(int i=0;p[i]*p[i]<=A;i++){if(A%p[i]==0){int num=0;while(A%p[i]==0){num++;A/=p[i];}LL M=(p[i]-1)*MOD;ans*=(quick_mod(p[i],num*B+1,M)-1)/(p[i]-1);ans%=MOD;}}if(A>1){LL M=MOD*(A-1);ans*=(quick_mod(A,B+1,M)-1)/(A-1);ans%=MOD;}cout<<ans<<endl;}int main(){LL A,B;isprime();while(cin>>A>>B){Solve(A,B);}return 0;}
阅读全文
0 0
- POJ 1845 --Sumdiv 约数基本定理+乘法逆元
- poj-1845 Sumdiv (逆元+费马小定理+因子和)
- POJ 1845 Sumdiv 逆元 费马小定理 Trick
- POJ1845 Sumdiv A^B的约数和(逆元解法或二分乘法)
- POJ 1845 Sumdiv 快速求幂+同余+乘法逆元
- poj 1845 Sumdiv (快速求幂+同余或 乘法逆元)
- POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)
- POJ 1845 Sumdiv(逆元)
- POJ 1845:Sumdiv 快速幂+逆元
- POJ 1845 Sumdiv(逆元+快速幂)
- POJ 1845 Sumdiv (逆元 等比数列求和)
- POJ 1845 Sumdiv 【推公式 | 逆元】
- POJ 1845 Sumdiv (数论,约数和)
- 洛谷 P2715 约数和(唯一分解定理+等比数列求和+乘法逆元)
- poj 1845 (逆元 + 约数和)
- POJ 1845 Sumdiv 解题报告(二分 & 逆元)
- poj 1845 Sumdiv 数论--等比数列和(逆元或者递归)
- POJ 1845 Sumdiv(逆元的应用)
- EBS中RTF报表Excel格式输出字段长度不正确的解决办法
- 根据数组某一项的某个键,删除数组的这一项
- 关于传统企业数据规划的看法
- [robot framework] 脚本解析模块
- 如何在 Linux 虚拟机上扩展根文件系统
- POJ 1845 --Sumdiv 约数基本定理+乘法逆元
- 自定义view画圆
- java设计模式(十二)——策略模式(Strategy)
- setTimeout() setInterval()
- js内存泄漏
- 二维码
- python学习随笔
- WIFI P2P (WIFI直连)源码解析
- SpringBoot整合cas单点登录