题目
Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004X. 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 20041 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的约数和
就是一个积性函数
积性函数:
a和b均为质数
求因数和
是质数
费马小定理
定理
假如是一个整数
,
是一个质数,
那么是
的倍数,可以表示为
如果
不是
的倍数,这个定理也可以写成
在本题中,费马小定理的应用为:
与
互质时,
有
再由同余定理:
可得
取模
加减法
乘法
除法
//不能直接分配了,要转化成乘法再做
结论
推导
快速幂取模
快速幂
求
int pow(int x,int n)
{
int ans = 1,base = x;
while(n)
{
if(n & 1)
ans *= base;
base *= base;
n >>= 1;
}
return ans;
}
快速幂取模
求
int pow_mod(int x, int n, int p)
{
int base = x, ans = 1;
while(n)
{
if(n & 1) ans *= base;
base = (base * base) % p;
n >>= 1;
}
return ans % p;
}
题目代码
#include <stdio.h>
long long int pow_mod(int x,int b);
int cal(int x);
int main()
{
int x[100],s[100];
int counter = 0,i;
scanf("%d",x);
while(x[counter] != 0)
{
s[counter] = cal(x[counter]) % 29;
//printf("%d\n",s[counter]);
counter++;
scanf("%d",&x[counter]);
}
for(i = 0;i < counter;i++)
printf("%d\n",s[i]);
return 0;
}
int cal(int x)
{
int a,b,c;
a = (pow_mod(2, 2 * x + 1) - 1 ) % 29;
b = (pow_mod(3, x + 1) - 1) * pow_mod(2, 27) % 29;
c = (pow_mod(22, x + 1) - 1) * pow_mod(21, 27) % 29;
return a * b * c % 29;
}
long long int pow_mod(int x,int b)
{
long long int base = x, ans = 1;
while(b)
{
if(b & 1) ans *= base;
base = (base * base) % 29;
b >>= 1;
}
return ans % 29;
}
坑点
1.取模那里要用long long int,用int 可能会不够长,我用int的时候会出现WA,改成long long int 就accepted了(不过我没想明白为什么不够长,base是不断取模的,按道理来讲,也不会太大啊?)
- 我也不知道这道题happy不happy,反正我是不怎么happy了。。。
总结了多位大佬们的博客,加上自己的一些理解,写了一份给自己看的总结,看不懂就去看看大佬们写的博客吧。这个博客主要还是写给我自己看的,阅读性不太强。