因子和

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 ;
}










评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值