【HDU - 1452】Happy 2004 (约数和+乘法逆元)

博客围绕求2004^x的所有因子的和并对29取余展开。先给出示例,接着说明输入输出格式。解题思路是将数分解成素数幂乘积形式,利用约数和公式,每一项为等比数列求和,因要取余需用逆元将除法转加法,还指出2004有素因子2、3、167。

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

题意:

求2004^x的所有因子的和并对29取余。

思路:

一个数A能被分解成A=(P1^K1)*(P2^K2)*(P3^K3).....*(Pn^Kn)这种形式,其中Pi为素数。

求约数和有个公式:S=(1+P1+P1^2+P1^3+.....P1^k1)*.....(1+Pn+Pn^2+Pn^3+.....Pn^kn)

每一项都是一个等比数列求和,这种等比数列化简能得到(Pi^k1-1)/(Pi-1)。因为要取余,所以我们需要求逆元,把除法转成加法。

分解2004可以得到2004的素因子有3个分别为2、3、167,分别有2个、1个、1个。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
#define mod (1000000007)
using namespace std;
typedef long long ll;
//2004的素因子有2,3,167||分别有2,1,1个 
ll qsm(ll a,ll b)
{
	ll t=1;
	while(b)
	{
		if(b&1)
		{
			t=(t*a)%29;
		}
		a=(a*a)%29;
		b>>=1;
	} 
	return t;
}
void solve(ll x)
{
	ll a=qsm(2,2*x+1)-1;
	ll b=qsm(3,x+1)-1;
	ll c=qsm(167,x+1)-1;
	ll d=qsm(2*166,27);//(3-1)*(167-1)的逆元 
	printf("%lld\n",(a*b*c*d)%29);
}
int main()
{
//	int kk=2004;
//	for(int i=2;i<=kk;i++)
//	{
//		if(isprime(i)&&kk%i==0)
//		{
//			printf("%d  ",i);
//			int sum=0;
//			while(kk%i==0)
//			{
//				sum++;
//				kk/=i;
//			}
//			printf("%d\n",sum);
//		}
//		
//	}
	ll x;
	while(~scanf("%lld",&x),x)
	{
		solve(x);
	} 
	
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值