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 DescriptionConsider 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.
InputThe 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.
OutputFor each test case, in a separate line, please output the result of S modulo 29.
Sample Input1100000
Sample Output610
分析:
因子和
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;}
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.
A test case of X = 0 indicates the end of input, and should not be processed.
1100000
610
2的因子是1,2; 2的因子和是s(2)=1+2=3;
3的因子是1,3; 3的因子和是s(3)=1+3=4;
4的因子和是 s(4)=1+2+4=7;
5的因子和是 s(5)=1+5=6;
s(20)=s(4)*s(5)=7*6=42;
数论中s(x)叫积性函数
求解 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)
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
- hdu 1452 Happy 2004
- HDU 1452 Happy 2004
- hdu 1452 Happy 2004
- hdu 1452 Happy 2004
- HDU 1452 Happy 2004
- hdu 1452 happy 2004
- HDU 1452 Happy 2004
- 【HDU】 1452 Happy 2004
- HDU 1452 Happy 2004
- HDU 1452 Happy 2004
- G - Happy 2004------(HDU 1452)
- HDU 1452 Happy 2004 快乐2004
- HDU 1452——Happy 2004
- HDU 1452 Happy 2004(因子和)
- hdu 1452 Happy 2004 因子和
- hdu 1452 Happy 2004(快速幂取模)
- hdu 1452 Happy 2004(积性函数)
- HDU 1452 Happy 2004 (因子和)
- OpenWrt的swconfig结构框架
- android WebView总结
- 多线程,信号!!!
- HiHo 1014 Trie树
- AES128、AES256加密 在iOS上实现
- hdu 1452 Happy 2004
- xode5.1.1设置IOS欢迎界面的方法
- DB2客户端,增加查看节点和数据库编目
- 机房收费系统之无效使用Null值
- 贝叶斯后验定理与预测(信号与噪声读书笔记)
- 编程艺术五 HASH表 0-1背包
- linux下编译器gcc/g++编译步骤
- maven Error occurred during initialization of VM
- 你不可不知的JSON