http://acm.hit.edu.cn/hoj/problem/view?id=1991
Consider a positive integer X, and let S be the sum of all positive integer divisors of 2005X. Your job is to determine S modulo 29 (the rest of the division of 29).
Take X = 1 for example. The positive integer divisor of 20051 are 1 5 401 2005. Therefore S = 2412, and S modulo 29 is equal to 5.
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
5 2
map<int , int> factor ;
const int Mod = 29 ;
void make_factor(int n){
factor.clear() ;
int sum ;
for(int i = 2 ; i*i <= n ; i++){
if(n % i == 0){
sum = 0 ;
while(n % i == 0){
sum++ ;
n /= i ;
}
factor[i] = sum ;
}
}
if(n != 1)
factor[n] = 1 ;
}
int Pow(int x , int y){
int ans = 1 ;
for(; y ; y>>=1){
if(y&1){
ans *= x ;
ans %= Mod ;
}
x *= x ;
x %= Mod ;
}
return ans ;
}
int ex_gcd(int a , int b , int &x , int &y){
if(b==0){
x=1 ;
y=0 ;
return a ;
}
int gcd=ex_gcd(b,a%b,x,y) ;
int temp=x ;
x=y ;
y=temp-(a/b)*y ;
return gcd ;
}
int Gao(int p , int a){
int x , y ;
ex_gcd(p-1 , Mod , x , y) ;
int ans = Pow(p , a+1) ;
ans = (ans - 1 + Mod) % Mod ;
ans = (ans * x ) % Mod ;
return (ans + Mod) % Mod ;
}
int Ans(int x){
map<int, int> ::iterator it ;
int ans = 1 ;
for(it = factor.begin() ; it != factor.end() ; it++){
int p = it->first ;
int a = (it->second) * x ;
ans = ans * Gao(p,a) ;
ans %= Mod ;
}
return ans ;
}
int main(){
make_factor(2005) ;
int x ;
while(scanf("%d",&x) && x){
printf("%d\n" , Ans(x)) ;
}
return 0 ;
}