Sumdiv
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11588 | Accepted: 2771 |
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).
将A^B 分解成素因数形式
A^B=(p1^k1)*(p2^k2)*(p3^k3)………
那么A^B所有因子之和就是
S=(1+p1+p1^2+p1^3+…..p1^k1)*(1+p2+p2^2+p2^3+…..p2^k2)*(1+p3+…)*…………..
计算1+p+p^2+p^3+p^4...+p^k可用二分求解
当k是偶数时,如k=4
1+p+p^2+p^3+p^4=(1+p)+p^2*(1+p(1+p))
k是奇数时,如k=5
1+p+p^2+p^3+p^4+p^5=(1+p+p^2)+p^3*(1+p+p^2)
#include<iostream>
#include<cstdio>
#define M 9901
#define MAX 10000
using namespace std;
int A,B,count;
int p[MAX],c[MAX];
//求解 x^n
__int64 Pow(__int64 x,__int64 n)
{
__int64 ret=1,s=x;
while(1)
{
if(n&1)
ret=(ret%M*s%M)%M;
if(n>>=1)
s=(s%M*s%M)%M;
else
break;
}
return ret;
}
//求解 1+p+p^2+...+p^n 等比数列的求和
__int64 Sum(__int64 p,__int64 n)
{
if(n==0)
return 1;
if(n&1)
return ((1+Pow(p,n/2+1))%M*Sum(p,n/2)%M)%M;
else
return ((1+Pow(p,n/2+1))%M*Sum(p,(n-1)/2)%M+Pow(p,n/2)%M)%M;
}
int main()
{
cin>>A>>B;
int i;
//将A分解成唯一p1^a1*p2^a2*...pi^ai...*pn^an
//p[i]记录pi,c[i]记录ai
for( i=2;i*i<=A;i++)
{
if(A%i==0)
{
p[++count]=i;
while(A%i==0)
{
A/=i;
c[count]++;
}
}
}
if(A!=1)
{
p[++count]=A;
c[count]=1;
}
//公式:(A*B)%C=((A%C)*(B%C))%C
__int64 res=1;
for(i=1;i<=count;i++)
res=(res%M*Sum(p[i],B*c[i])%M);
//__int64无法用cout输出
printf("%d\n",res);
return 0;
}