Sumdiv
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 13761 | Accepted: 3389 |
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质因子分解成(p1^k1*p2^k2*...pn^kn)*B的形式,若设A^B的因子ti=(p1^ti1*p2^ti2...*pi^tin)的形式,则对他们进行求和实际上等价于求(1+p1^1+p1^2...+p1^bk)*(1+p2^1+p2^2+..p2^n)*...(1+pn^1+pn^2+....pn^n)的形式,接着对每一个等比形式的∑进行二分求解,在过程中取模就可以了。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
long long A,B;
long long a[10005];
long long n[10005];
const long long mod=9901;
long long power(long long a,long long b)
{
long long res=1;
while(b)
{
if(b&1)
res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
long long sum(long long a,long long b)
{
if(b==0)
return 1;
if(b%2==1)
return (sum(a,b/2)*(power(a,b/2+1)+1))%mod;
else
return (sum(a,b-1)+power(a,b))%mod;
}
int main()
{
while(cin>>A>>B)
{
long long cnt=0;
for(int i=2;i*i<=A;i++)
{
if(A%i==0)
{
a[cnt]=i;
n[cnt]=0;
while(A%i==0)
{
A/=i;
n[cnt]++;
}
cnt++;
}
}
if(A!=1)
{
a[cnt]=A;
n[cnt++]=1;
}
long long ans=1;
for(long long i=0;i<cnt;i++)
{
ans=(ans*sum(a[i],B*n[i]))%mod;
ans%=mod;
}
cout<<ans<<endl;
}
return 0;
}