原题链接:
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的因子之和。
题解:
利用唯一分解定理,将a标准分解,然后每一个素数的指数都扩大b倍,最后利用公式求和,等比数列求和时,分母可以通过费马小定理求逆元,然后一同求余。
附上AC代码:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int mod=9901;
const int N=sqrt(5e7)+5;
int prime[N],cnt=0;
bool vis[N];
void el()//素数打表
{
memset(vis,0,sizeof(vis));
for(int i=2;i<=N;i++)
{
if(!vis[i])
{
prime[cnt++]=i;
for(int j=i*2;j<=N;j+=i)
{
vis[j]=1;
}
}
}
}
int quickpow(int a,int b)//快速幂
{
int ans=1;
while(b!=0)
{
if(b&1)
ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return ans;
}
int main()
{
el();
int a,b;
scanf("%d%d",&a,&b);
int sum=1;
for(int i=0;i<cnt&&prime[i]<=a;i++)//唯一分解定理求出a的分解后各个素数的指数
{
if(a%prime[i]==0)
{
int e=0;
while(a%prime[i]==0)
{
a/=prime[i];
e++;
}
//利用快速幂计算出因子的和(等比数列求和)以及费马小定理求出逆元
int x=(((quickpow(prime[i],(b*e+1)))-1)*quickpow(prime[i]-1,mod-2))%mod;
sum=(sum*x)%mod;//求和
}
}
printf("%d\n",sum);
return 0;
}
欢迎评论!