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).
评价:因为取模比较小,所以我都用了int,但是此题坑点是,我忘记了会有大素数,导致的相乘溢出,细节原因,换用long long即可。
AC
#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#define maxx 10050
#define mod 9901
using namespace std;
int a,b;
char pri[maxx];
vector<int> prime;
void init()
{
for(int i=2;i<maxx;i++)
{
if(!pri[i])
prime.push_back(i);
for(int j=i*i;j<=maxx;j+=i)
pri[j]=1;
}
}
int fac[100];
int e[100];
int cnt;
void getFac(int x)
{
cnt=0;
for(int i=0;prime[i]*prime[i]<=x;i++)
{
if(x%prime[i]==0)
{
fac[cnt]=prime[i];
while(x%prime[i]==0)
{
e[cnt]++;
x/=prime[i];
}
cnt++;
}
}
if(x>1)
{
fac[cnt]=x;
e[cnt++]=1;
}
}
long long _p(long long a,long long b)
{
long long ans=1;
while(b)
{
if(b&1)
ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
long long get(long long p,long long x)
{
if(x==0)
return 1;
if(x==1)
return (p+1)%mod;
int ans=(_p(p,x/2+1)+1)*get(p,x-x/2-1);
if(x%2)
return ans%mod;
else
return (ans+_p(p,x>>1))%mod;
}
int main()
{
init();
cin>>a>>b;
getFac(a);
long long ans=1;
for(int i=0;i<cnt;i++)
ans=ans*get(fac[i],e[i]*b)%mod;
cout<<ans<<endl;
return 0;
}