1452 Happy 2004 (所有因子求和)
来源:互联网 发布:程序员的电脑桌 编辑:程序博客网 时间:2024/06/07 01:25
Happy 2004
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 837 Accepted Submission(s): 586
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.
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.
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思路分析:设S(x)表示x的因子和。则题目求为:S(2004^X)mod 29因子和S是积性函数,即满足性质1。
性质1 :如果 gcd(a,b)=1 则 S(a*b)= S(a)*S(b)2004^X=4^X * 3^X *167^XS(2004^X)=S(2^(2X)) * S(3^X) * S(167^X)
性质2 :如果 p 是素数 则 S(p^X)=1+p+p^2+...+p^X = (p^(X+1)-1)/(p-1)因此:S(2004^X)=(2^(2X+1)-1) * (3^(X+1)-1)/2 * (167^(X+1)-1)/166167%29 == 22S(2004^X)=(2^(2X+1)-1) * (3^(X+1)-1)/2 * (22^(X+1)-1)/21
性质3 :(a*b)/c %M= a%M * b%M * inv(c)其中inv(c)即满足 (c*inv(c))%M=1的最小整数,这里M=29则inv(1)=1,inv(2)=15,inv(22)=15
有上得:S(2004^X)=(2^(2X+1)-1) * (3^(X+1)-1)/2 * (22^(X+1)-1)/21=(2^(2X+1)-1) * (3^(X+1)-1)*15 * (22^(X+1)-1)*18
快速幂取模就是在O(logn)内求出a^n mod b的值。算法的原理是ab mod c=(a mod c)(b mod c)mod c代码
- #include<iostream>
- using namespace std;
- const int pow[][3]={{2,5,32},{3,4,81},{22,2,484}};
- //2^5>29,3^4>29,22^2>29,用于求(b^i)%29
- int PowMod29(int x,int index) //快速模幂
- {
- int ans=1;
- while(index>=pow[x][1]) //当指数大于这个值将会超过29
- {
- ans=(ans*pow[x][2])%29; //所以要模29.并且要乘上前面的值!
- index-=pow[x][1];
- }
- while(index--) ans=(ans*pow[x][0])%29;//把剩余的不超过29的乘上!再记得模上29(因为有可能超过29)
- return ans;
- }
- int main()
- {
- int X,part2,part3,part167;
- while(cin>>X&&X!=0)
- {
- part2=PowMod29(0,2*X+1);
- part3=PowMod29(1,X+1);
- part167=PowMod29(2,X+1);
- cout<<((part2-1)*(part3-1)*15*(part167-1)*18)%29<<endl;//再模29,因为有超过29的可能.
- }
- return 0;
- }
- 1452 Happy 2004 (所有因子求和)
- hdoj 1452 Happy 2004 所有因子求和
- HDU 1452 Happy 2004 求2004^n的所有因子和 积性函数应用
- 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 (素因子分解+快速幂模+乘法逆元)
- 整数的因子和 ---TOJ 1089 Happy 2004
- hdu1452 Happy 2004(规律+因子和+积性函数)
- 【Algorithm】因子求和
- 1304因子求和
- hdu 1452 Happy 2004(快速幂取模)
- HDOJ 题目1452 Happy 2004()
- hdu 1452 Happy 2004
- HDU 1452 Happy 2004
- Vim 复制粘贴探秘
- UNIX网络编程--套接字可读或可写的条件
- 动态规划求一道 类最长递增公共子序列
- oracle trunc()函数的用法
- html文件 得到或者引用js变量的值
- 1452 Happy 2004 (所有因子求和)
- 两种Catalan C++的实现
- 关于oracle的backup database plus archivelog delete input备份命令
- Linux Commands for Beginners--which and whatis commands
- 百度知道删除技术
- 链表的新建和输出
- android sqlite 几个异常分析
- C# GDI+编程(一)
- 存储过程学习总结