Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)

Problem Description

Considera positive integer X,and let S be the sum of all positive integer divisors of2004^X. Your job is to determine S modulo 29 (the rest of the division of S by29).

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 29is equal to 6.

 

 

Input

Theinput consists of several test cases. Each test case contains a line with theinteger X (1 <= X <= 10000000). 

A test case of X = 0 indicates the end of input, and should not be processed.

 

 

Output

Foreach test case, in a separate line, please output the result of S modulo 29.

 

 

Sample Input

1

10000

0

 

 

Sample Output

6

10

 

/***************************

解题思路:

参考大神的:http://www.cnblogs.com/372465774y/archive/2012/10/22/2733977.html

体重主要用到除数和函数。

除数和函数 :F(n) 求n的约数的和 ( 约数大于等于1 小于n ) 

除数和函数是一个积性函数,满足性质 :当m , n 互质时, f(m*n) = f(m) * f(n)

如果 p 是一个素数,则 f(p^n) = 1 + p + p^2 +p^3 +p^4 + .... + p^(n-1) + p^n = (p^(n+1) -1)/p-1  (等比数列求和)

则题目中  f(2004^n) = f(2^(2*n)) * f(3^n) * f(167^n)

    = (2^(2*n+1) -1) * (3^(n+1) -1)/2  *(167^(n+1) -1)/166    

用到乘法逆元:(同余性质)

a^k/d = a^k*(d-1)     d-1 即为d的逆元。   3的逆元为15     167 的逆元为18

具体参考:http://baike.baidu.com/link?url=pcN2WyxgeFP9isdQxd9bTobeiRH3MnXcrdIwHh7jCBsYkVyTfFhF5QiS-d8-HgNgslVb334pgqkClTiIp359Xa

然后还要用到 快速幂模:转换为位运算,这题要用这个,一般的会超时,具体看代码吧。

*************************/

Code:

#include<stdio.h>
using namespace std;
int Mod(int a,int b)//  快速幂模函数
{
    int t = 1;
    while(b)
    {
        if(b&1)
            t = t*a%29;
        b>>=1;
        a = a*a%29;
    }
    return t;
}
int main()
{

    int n,a,b,c;
    while(scanf("%d",&n)&&n)
    {
        a=(Mod(2,2*n+1)-1);
        b=(Mod(3,n+1)-1)*15;
        c=(Mod(22,n+1)-1)*18;
        printf("%d\n",a*b*c%29);
    }
    return 0;
}



你可能感兴趣的:(APP)