【HDU - 1452】 Happy 2004(因子和,逆元,快速幂)

题干:

                                      Happy 2004

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


 

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

 

 

Source

ACM暑期集训队练习赛(六)

 

题意:求2004^x的所有因子和对29取余的结果。

 

题解:因为所有数都可以被分解成a=p1^c1*p2^c2*...*pk^ck,并有约数和定理:sum=(p1^0+...+p1^c1)*(p2^0+...+p2^c1)*...*(pk^0+...+pk^ck)。每一项可以通过等比数列求和得到。2004=2^2*3*167,所以答案就是2^(2*n+1)*3^(n+1)*167^(n+1)/(2*166)。由于需要取余,所以不能直接除要求出2*166的逆元改为相乘。

 

AC代码:

#include<bits/stdc++.h>
#define debug cout<<"aaa"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;
 
const int N = 100000 + 5;
const int mod = 29;
 
int e_gcd(int a,int b,int &x,int &y) {
	if(b==0) {
		x=1,y=0;return a;
	}
	int q=e_gcd(b,a%b,y,x);
	y-=a/b*x;
	return q;
	
}
 
int quick(int a,int b){
	int ans=1;
	while(b){
		if(b&1){
			ans=(ans*a)%mod;
		}
		b>>=1;
		a=(a*a)%mod;
	}
	return ans;
}
 
int main(){
	int n,x,y,a,b,c,ans;
	exgcd(166*2,29,x,y);
	while(~scanf("%d",&n)&&n){
		a=(quick(2,2*n+1)-1)%mod;
		b=(quick(3,n+1)-1)%mod;
		c=(quick(167,n+1)-1)%mod;
		ans=((a*b*c*x)%mod+mod)%mod;
		printf("%d\n",ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值