Sumdiv
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). Source |
提示
题意:
给出a,b(0<=a,b<=50000000),求出q为a,n为b的等比数列和。例如:1+2+2^2+2^3+......
思路:
之前我还以为直接上公式再取余就完了,之后就惨不忍睹。
真正的做法是:
1.先把a分解成小数,比如36^100=2^200*3^200(至于为什么一定要分解就不清楚了,我直接递归求和是WA)
2.递归求和
n为奇数时:(1+p+p^2...p^(n/2))*(1+p^(n/2+1))
n为偶数时:(1+p+p^2...p^(n/2-1))*(1+p^(n/2+1))+p^(n/2)
这个式子可以在纸上推出来,黑色部分做递归就可以了,当n=0时就是递归的终点。
对于乘方需要快速幂,这里不给予叙述,详情请咨询百度。
示例程序
Source Code
Problem: 1845 Code Length: 866B
Memory: 388K Time: 32MS
Language: GCC Result: Accepted
#include <stdio.h>
long long Pow(int x,int n) //快速幂
{
long long t=x,num=1;
while(n!=0)
{
if(n%2==1)
{
num=(num*t)%9901;
}
t=(t*t)%9901;
n=n/2;
}
return num;
}
long long Sum(int p,long long n) //递归求和
{
if(n==0)
{
return 1;
}
else if(n%2==1)
{
return Sum(p,n/2)*(1+Pow(p,n/2+1))%9901;
}
else
{
return (Sum(p,n/2-1)*(1+Pow(p,n/2+1))%9901+Pow(p,n/2))%9901;
}
}
long long f(int a,int b)
{
long long sum=1;
int i,k;
for(i=2;a>=i*i;i++) //只需判断到i*i就行了,这和试除法是一样的道理
{
k=0;
while(a%i==0) //因式分解
{
a=a/i;
k++;
}
sum=sum*Sum(i,b*k)%9901; //分解出来去计算
}
if(a!=1)
{
sum=sum*Sum(a,b)%9901; //如果还没有分解完全还需要再计算一次
}
return sum;
}
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%lld",f(a,b));
return 0;
}