Ask: Input x, 2004^x all the factors of the and, the result of 29.
Solution: Must not be violent, need to think of other methods, we know is: a number of X=P1^T1*P2^T2*...*PK^TK (of which PI is prime). The number of factors is (t1+1) * (t2+1) *...* (tk+1). How did you get that? Enumerating each exponent 0~ti, we can obtain all the factors. So factor and sum=p1^0*p2^0*...*pk^0+p1^0*p2^0*...*pk^1+......+p1^t1*p2^t2*. pk^tk= (P1^0+P1^1...+PK^T1) *...* (PK^0+PK^1+...+PK^TK). This is the product of the sum of the geometric progression. The next step is simple, 2004=2^2*3*167, so 2004^x=2^2x*3^x*167^x, Factor and sum= (2^0+...+2^2x) * (3^0+ ...). 3^X) * (167^0+...+167^x) = (2^ (2x+1)-1) * (3^ (x+1)-1)/2) * (167^ (x+1)-1)/166). One by one can be taken.
Here the division has been used before, two methods:
1. (A/b)%mod=a% (b*mod)/b%mod;
2. (A/b)%mod=a*b^ (mod-2)%mod,mod is prime (can be proved by the inverse)
Time Consuming: 0ms/1000ms
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int mod=29;
int pow_mod (int a,int b)
{
int s=1;
while (b)
{
if (b&1) s= (s*a)%mod;
A= (a*a)%mod;
b=b>>1;
}
return s;
}
int find (int a,int b)
{
int i,j,k,p,ans;
Ans=pow_mod (a,b+1)-1;
Ans= (Ans+mod)%mod;
Ans=ans*pow_mod (a-1,mod-2)%mod;
return ans;
}
int main ()
{
int x;
while (cin>>x)
{
if (x==0) break;
Cout<<find (2,2*x) *find (3,x) *find (167,x)%mod<<endl;
}
return 0;
}
/* 2004=2*2*3*167;
*/