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).
#include
#include
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int h = 10001;
int n,m;
struct node
{
int x;
int y;
}q[h];
long long int power(long long int pn,long long int pm) ///反复平方法来求A^B 省时间
{
long long int sq = 1;
while(pm>0)
{
if(pm%2)
{
sq = (sq*pn)%9901;
}
pm = pm / 2;
pn = pn * pn % 9901;
}
return sq;
}
long long int updata(long long int pn,long long int pm) ///递归二分求等比数列的和
{
if(pm == 0)
{
return 1;
}
if(pm%2)
{
return (updata(pn,pm/2)(1+power(pn,pm/2+1)))%9901; /// 当pm为奇数时,有公式来求等比数列的和 (1 + p + p^2 +…+ p^(n/2)) * (1 + p^(n/2+1))
}
else
{
return (updata(pn,pm/2-1)(1+power(pn,pm/2+1)) + power(pn,pm/2))%9901; ///当pm为偶数时,有公式来求等比数列的和 (1 + p + p^2 +…+ p^(n/2-1)) * (1+p^(n/2+1)) + p^(n/2);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int k = 0;
for(int i=2;ii<=n;) ///寻找质因子,一个很好的方法
{
if(n%i == 0)
{
q[k].x = i;
q[k].y = 0;
while(n%i == 0)
{
q[k].y++;
n /= i;
}
k++;
}
if(i == 2)
{
i++;
}
else
{
i = i + 2;
}
}
if(n!=1)
{
q[k].x = n;
q[k].y = 1;
k++;
}
int ans = 1;
for(int i=0;i<k;i++)
{
ans = (ans(updata(q[i].x,q[i].y*m)%9901)%9901);
}
printf("%d\n",ans);
}
return 0;
}
本文介绍了一种通过模运算求解自然数幂次方所有因数之和的问题,使用了高效的反复平方算法和等比数列求和公式,对大整数的处理进行了优化。
1111

被折叠的 条评论
为什么被折叠?



