HDU 1452 Happy 2004(因数之和)

Happy 2004

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


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
100000
 

Sample Output

6
10

首先,任意一个大于1的整数都能唯一分解成有限素数的乘积。

再看积性函数:积性函数指对于所有互质的整数a和b有性质f(ab)=f(a)f(b)的数论函数

HDU 1452 Happy 2004(因数之和)_第1张图片


某个数的所有因子之和对应的函数恰为积性函数,那么我们定义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))%m

b与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
#include
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);
	}
} 


你可能感兴趣的:(数论)