poj 1845 Sumdiv
Sumdiv
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). 参考思路:http://blog.csdn.net/lyy289065406/article/details/6648539 #include<stdio.h> #include<math.h> #include<string.h> #define MOD 9901 long long p[100000],ans[100000]; long long Pow1(long long p,long long n)//计算p的n次方的函数,不能直接用pow函数 { /* long long temp=p;//开始直接用循环,超时; int i; for(i=0;i<n-1;i++) temp=(temp%MOD*p%MOD)%MOD; //printf("temp=%lld\n",temp); return temp;*/ long long ret=1,s=p;//快速幂算法 while(n) { if(n&1) ret=(ret%MOD*s%MOD)%MOD; if(n>>=1) s=(s%MOD*s%MOD)%MOD; else break; } return ret; } long long Sum(long long p,long long n)//二分求等比数列的和 { if(n==0) return 1; else if(n%2!=0) return (((1+Pow1(p,n/2+1)%MOD))*(Sum(p,n/2))%MOD)%MOD;//两个公式,可以自己推下 else return (((1+Pow1(p,n/2+1)%MOD)*(Sum(p,(n/2-1)))%MOD)+(Pow1(p,n/2))%MOD)%MOD; } int main() { long long i,j,k,cnt,sum; int A,B; //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d %d",&A,&B)!=EOF) { memset(ans,0,sizeof(ans)); memset(p,0,sizeof(p)); sum=1; k=0; for(i=2;i<=sqrt(A);i++)//把A分解 { cnt=0; while(A%i==0) { p[k]=i; ans[k]++; cnt=1; A/=i; } if(cnt) k++; } /* if(A!=1)//这里很重要,我开始只考虑这个数是单个质数的情况,也就是A=13这种情况, { p[0]=A;//WA了几次,还有一种情况就是A=13*17这种如果我这样做的话,13的话就被覆盖了 ans[0]=1; k=1; }*/ if(A!=1) { p[k]=A; ans[k]+=1; k++; } for(j=0;j<k;j++) { sum=(sum%MOD*Sum(p[j],ans[j]*B)%MOD)%MOD; } printf("%lld\n",sum%MOD); } return 0; }
|