您的位置:首页 > 移动开发

HDU 1452 Happy 2004(因数之和)

2018-04-03 20:58 387 查看

Happy 2004

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


[align=left]Problem Description[/align]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.
 
[align=left]Input[/align]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.
 
[align=left]Output[/align]For each test case, in a separate line, please output the result of S modulo 29.
 
[align=left]Sample Input[/align]
1100000 
[align=left]Sample Output[/align]
610
首先,任意一个大于1的整数都能唯一分解成有限素数的乘积。
再看积性函数:积性函数指对于所有互质的整数a和b有性质f(ab)=f(a)f(b)的数论函数



某个数的所有因子之和对应的函数恰为积性函数,那么我们定义f(n)为n的所有因子之和,那么f( n )的质因子可以得到(素数打表)。那么f(p1^a1)在p1为素数时等于什么呢?很容易知道p1^a1的因子为1 p1 p1^2 ... p1^a1所以其和为(p1^(a1+1)-1)/(p1-1)//等比序列求和。那么f(2014) = f(2^2)*f(3)*f(167)=f((2^(2+1)-1)/(2-1))*f((3^(1+1)-1)/(3-1)*ff((167^(1+1)-1)/(167-1);
f(2004^k)%29 = f(2^(2*k)) * f(3^k) * f(167^k)%29                          =(2^(2*k+1)-1) * ((3^(k+1)-1)/2) * ((167^(k+1)-1)/166) % 29根据同余方程:若a = b % m 则a^k = b^k % m结合(a ± b) % m = a%m ± b%m所以原式=(2^(2*k+1)-1) * ((3^(k+1)-1)/2) * ((22^(k+1)-1)/21) % 29再结合公式:(a*b) % m = a%m * b%m(a/b) % m = (a * b^(-1))%mb与b^(-1)关于m互逆,即b * b^(-1) %m = 1(b^(-1)为整数)//逆元对应扩展欧几里得方程b*x = 1(mod m) 解出b即为b^-1,e_gcd(b,m , x , y);因为这里只需要求2和166的逆,直接找出即可,没有写扩展欧几里得的必要很容易得到2的逆元素是15,因为2*15=30 % 29=1 % 29
21的逆元素是18,因为21*18=378% 29 =1 % 29
所以计算下式即可:a=(power(2,2*x+1,29)-1)% 29;
b=(power(3,x+1,29)-1)*15 % 29;
c=(power(22,x+1,29)-1)*18 % 29;
ans=(a*b)% 29*c % 29;

#include<stdio.h>
#include<string.h>
using namespace std;
/*void e_gcd(int a,int b,int &x,int &y)
{
if(b==0)
{
x=1;
y=0;
return;
}
e_gcd(b,a%b,x,y);
int temp=x;
x=y;
y=temp-a/b*y;
}*/
int power(int a,int n,int mod)//快速幂
{
int res=1;
a%=mod;
while(n>0)
{
if(n&1)
res=res*a%mod;
a=a*a%mod;
n>>=1;
}
return res;
}
int main()
{
int x;
while(~scanf("%d",&x)&&x)
{
int a=(power(2,2*x+1,29)-1)%29;
int b=(power(3,x+1,29)-1)*15%29;
int c=(power(22,x+1,29)-1)*18%29;
int ans=(a*b)%29*c%29;
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: