hdu 1452 Happy 2004

来源:互联网 发布:大富豪3.4.1最新源码 编辑:程序博客网 时间:2024/05/20 07:37

Happy 2004

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 983    Accepted Submission(s): 700


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
1100000
 

Sample Output
610
 

分析:

因子和
2的因子是1,2; 2的因子和是s(2)=1+2=3;
3的因子是1,3; 3的因子和是s(3)=1+3=4;
6的因子是1,2,3,6; 6的因子和是s(6)=1+2+3+6=12;
s(6)=s(2)*s(3)=3*4=12;

4的因子和是 s(4)=1+2+4=7;
5的因子和是 s(5)=1+5=6;
20的因子是1,2,4,5,10,20; 20的因子和是s(20)=1+2+4+5+10+20=42;
s(20)=s(4)*s(5)=7*6=42;

数论中s(x)叫积性函数
当gcd(a,b)=1时,s(a*b)=s(a)*s(b)
当p是素数时,s(p^n)=1+p+p^2+...+p^n=(p^(n+1)-1) /(p-1)  ................ (1)

求解 s(2004^X) % 29 的具体过程

2004 = 2^2 *3 *167   ................  (分成质数之积)
s(2004^X)%29 = s(2^2X)%29 * s(3^X)%29 * s(167^X)%29
s(2004^X)%29 = s(2^2X)%29 * s(3^X)%29 * s(22^X)%29  .......(167%29=22)

a=s(2^2X)%29=(2^(2X+1)-1)%29             //根据(1)
b=s(3^X)%29=((3^(X+1)-1)/2)%29          //根据(1)
c=s(22^X)%29=((22^(X+1)-1)/21)%29      //根据(1)

%运算法则 1. (a*b) % p= ( (a%p)*(b%p) )%p
%运算法则 2. (a/b) % p= (a*c%p)         
//c是b的逆元 (即:b*c=1 (mod p) 或 (b*c)%p=1%p 或 (b*c)%p=1)

例如:
2的逆元是15, 因为2*15=30 % 29=1 % 29
21的逆元是18,因为21*18=378 % 29 =1 % 29

因此
a=(pow(2,2*x+1)-1) % 29;
b=(pow(3,x+1)-1)*15 % 29;
c=(pow(22,x+1)-1)*18 % 29;
ans=(a*b)% 29 * c % 29;



#include<stdio.h>#include<algorithm>#include<iostream>using namespace std;__int64 pow(__int64 x,__int64 y)  // 快速幂取模{ __int64 sum=1; x%=29; while(y) {   if(y%2==1)   sum=sum*x;   x=x*x%29;   y=y/2; } return sum;}int main (){__int64 x,a,b,c,ans;while(scanf("%I64d",&x)&&x!=0){  a=(pow(2,2*x+1)-1)%29;    // 具体过程见分析  b=(pow(3,x+1)-1)*15%29;  c=(pow(22,x+1)-1)*18%29;  ans=(a*b)%29*c%29;  printf("%I64d\n",ans);}return 0;}



0 0
原创粉丝点击