HDU1452 Happy 2004 (因子和)

Happy 2004

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)



Problem Description
Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your job is to determine S modulo 29 (the rest of the division of S by 29).

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 29 is equal to 6.
 
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
6 10
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1695 1573 1788 1060 1370
首先2004 = 2*2*3*167
然后,利用因子和是积性函数的性质(蒟蒻准备专门写一篇持续更新的有关积性函数证明及学习的文章):
σ(2004^x) =  σ(2^2x) * σ (3^x) * σ(167^x) Mod 29
∵167≡22(Mod29)
故σ(2004^x) =  σ(2^2x) * σ (3^x) * σ(22^x) Mod 29
                   =  [2^(2x+1)-1][3^(x+1)-1]/2*[22^(x+1)-1]/21
因为2的模29乘法逆元为15 ,22的模29乘法逆元为18
故σ(2004^x)  =  [2^(2x+1)-1][3^(x+1)-1]*15*[22^(x+1)-1]*18
即可用快速幂求解
 1 #include<set>
 2 #include<queue>
 3 #include<vector>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 using namespace std;
10 const int Mod = 29;
11 #define Rep(i,n) for(int i=1;i<=n;i++)
12 #define For(i,l,r) for(int i=l;i<=r;i++)
13 
14 int ans,x;
15 
16 int quickpow(int m,int n){
17     int ans=1;
18     while(n){
19         if(n&1) ans=(ans*m)%Mod;
20         m=(m*m)%Mod;
21         n>>=1; 
22     }
23     return ans%Mod;
24 }
25 
26 int main(){
27     while(scanf("%d",&x),x){
28         int ans=0;
29         ans=(quickpow(2,2*x+1)-1)%Mod;
30         ans=ans%Mod*(quickpow(3,x+1)-1)*15%Mod;
31         ans=ans%Mod*(quickpow(22,x+1)-1)*18%Mod;
32         printf("%d\n",ans%Mod);
33     }
34     return 0;
35 }
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。