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

【原创】【数论】HDU-1452 Happy 2004(约数和定理)

2017-07-07 23:13 363 查看

Happy 2004 HDU - 1452

题目描述

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

题目分析

引理:约数和定理

内容:若x可以质因数分解为
x=p0^a0*p1^a1*......pk^ak
,则x所有因数的和为:
(p1^0+p1^1+p1^2+…p1^a1)*(p2^0+p2^1+p2^2+…p2^a2)*…*(pk^0+pk^1+pk^2+…pk^ak)


证明:

我不知道。

而上述因数和式中每个括号内都是等比数列,可以套用等比数列求和公式。

那么这道2004就简单了。

唯一需要注意的是,题目要求mod29。而求和公式涉及除法,所以要求逆元。

怎么求逆元呢?

由于29是质数,所以:

根据费马小定理: a^(p-1)%p=1

则:a*a^(p-2)%p=1

所以a^(p-2)就是a关于p的逆元。

所以用快速幂就可以解决这道题了。

//话说我居然背错公式了,把括号之间的乘号记成加号害我没过样例QAQ

详见代码

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

int Read(int &p)
{
p=0;
char c=getchar();
while(c<'0' || c>'9') c=getchar();
while(c>='0' && c<='9')
p=p*10+c-'0',c=getchar();
return 1;
}

int x,ans;

int speed(int a,int b,int c)
{
int ans=1;
a%=c;
while(b)
{
if(b&1) ans=ans*a%c;
b>>=1; a=a*a%c;
}
return ans%c;
}

int dow(int a,int b)
{
int k=(speed(a,b+1,29)+28)%29;
return k*speed(a-1,27,29)%29;
}

int main()
{
while(Read(x)&&x)
{
ans=dow(2,2*x)*dow(3,x)%29*dow(167,x)%29;
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: