Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your job is to determine S modulo 29 (the rest of the division of S by 29).
Take X = 1 for an example. The positive integer divisors of 2004^1 are 1, 2, 3, 4, 6, 12, 167, 334, 501, 668, 1002 and 2004. Therefore S = 4704 and S modulo 29 is equal to 6.
Input
The input consists of several test cases. Each test case contains a line with the integer X (1 <= X <= 10000000).
A test case of X = 0 indicates the end of input, and should not be processed.
Output
For each test case, in a separate line, please output the result of S modulo 29.
Sample Input
1 10000 0
Sample Output
6 10
题意:
求2004^x的所有因子的和并对29取余。
思路:
一个数A能被分解成A=(P1^K1)*(P2^K2)*(P3^K3).....*(Pn^Kn)这种形式,其中Pi为素数。
求约数和有个公式:S=(1+P1+P1^2+P1^3+.....P1^k1)*.....(1+Pn+Pn^2+Pn^3+.....Pn^kn)
每一项都是一个等比数列求和,这种等比数列化简能得到(Pi^k1-1)/(Pi-1)。因为要取余,所以我们需要求逆元,把除法转成加法。
分解2004可以得到2004的素因子有3个分别为2、3、167,分别有2个、1个、1个。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
#define mod (1000000007)
using namespace std;
typedef long long ll;
//2004的素因子有2,3,167||分别有2,1,1个
ll qsm(ll a,ll b)
{
ll t=1;
while(b)
{
if(b&1)
{
t=(t*a)%29;
}
a=(a*a)%29;
b>>=1;
}
return t;
}
void solve(ll x)
{
ll a=qsm(2,2*x+1)-1;
ll b=qsm(3,x+1)-1;
ll c=qsm(167,x+1)-1;
ll d=qsm(2*166,27);//(3-1)*(167-1)的逆元
printf("%lld\n",(a*b*c*d)%29);
}
int main()
{
// int kk=2004;
// for(int i=2;i<=kk;i++)
// {
// if(isprime(i)&&kk%i==0)
// {
// printf("%d ",i);
// int sum=0;
// while(kk%i==0)
// {
// sum++;
// kk/=i;
// }
// printf("%d\n",sum);
// }
//
// }
ll x;
while(~scanf("%lld",&x),x)
{
solve(x);
}
return 0;
}