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



發佈了48 篇原創文章 · 獲贊 7 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章