设为首页 加入收藏

TOP

HDU 1452 Happy 2004 (因子和)
2015-11-21 01:03:54 来源: 作者: 【 】 浏览:2
Tags:HDU 1452 Happy 2004 因子

?

Happy 2004

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



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
10000
0

Sample Output
6
10
简单的约数求和。2014^x,由素数基本定理,将2014分解,2014=2^2 *  3 * 167,则2014^x=2^(2*x) * 3^x  *   167^x,由约数和公式(后附)得2014^x的约数和为:[2^(2*x+1)-1]/(2-1)  * [3^(x+1)-1]/(3-1)  *  [167^(x+1)-1]/(167-1) = r/332,其中r=[2^(2*x+1)-1] * [3^(x+1)-1] *  [167^(x+1)-1],答案就是(r/332)%29,在取模运算里,除以一个数等于乘以这个数的逆元。而332关于模29的逆元是9,故答案就是(r*9)% 29.
在这里还学到的知识:费马小定理,对任意的素数p,任意整数a,有a^(p-1) mod p =1。 在本题里29是素数,那么对于a=2014就符合费马小定理,由费马小定理,a^28 mod 29 =1,则a^x mod 29 = a^(x mod 28) mod 29.   所以在代码中处理时,先把x对28取余。
补:约数和公式,有素数基本定理,n=p1^a1 * p2^a2 ....... * pk^ak,约数和为:(p1^(a1+1)-1)/(p1-1) * (p2^(a2+1)-1)/(p2-1) .... * (pk^(ak+1)-1)/(pk-1)
#include 
  
    #include 
   
     #include 
    
      #include 
     
       using namespace std; typedef __int64 ll; const int MOD=29; quick_mod(int a,int b){ //a^b%MOD int res=1; a%=MOD; while(b){ if(b&1) res=(res*a)%MOD; b/=2; a=(a*a)%MOD; } return res; } int main() { int i,j,n,res,k; while(scanf(%d,&n),n){ int x1,x2,x3; x1=(2*n+1)%(MOD-1); x1=quick_mod(2,x1)-1; x2=x3=(n+1)%(MOD-1); x2=quick_mod(3,x2)-1; x3=quick_mod(167,x3)-1; res=(x1*x2*x3*9)%MOD; printf(%d ,res); } return 0; } 
     
    
   
  

补充逆元的求法:a*b mod m =1,则称a为b的逆元,a,b互为逆元,a模m存在逆元的充要条件是:a和m互素。(这有线性同余方程的知识可证)。将a*b mod m =1变形:a*b - m*y =1,要求a的逆元的话就是求b,即线性同余方程的解,对a和m构造同余方程,然后求解即可。(用到了扩展欧几里得算法Exgcd)
void Exgcd(int a,int b,int &d,int &x,int &y){
	if(b==0){
		x=1;y=0;d=a;return ;
	}
	Exgcd(b,a%b,d,y,x);
	y-=(a/b)*x;
}

//求模n意义下a的逆元,如果不存在则返回-1
int inv(int a,int n){
	int d,x,y;
	Exgcd(a,n,d,x,y);
	return d==1?(x+n)%n:-1;
}

所以本题中332在模29意义下的逆元可以通过 inv(332,29)求得。
?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇SDUT 1447 水题(y*y = x*x + n) 下一篇How to launche powershell scrip..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: