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.
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.
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
题目意思为,给出一个n,需要求2004^n的所有约数的和,假设函数f(x)为求x的约数和的函数,我们可以知道,f(x)为一个乘性函数也即f(m*n)=f(m)*f(n); 因为有这个性质,所以我们可以先对2004进行素数分解,得到分解后的一系列素数以及素数的指数,对于一个素数,可以知道,他只有1和本身两个约数,对于一个素数的n次方,假设p为素数,则p^n的约数为1, p^1, p^2, p^3
....p^n,所以可以求的f(p^n)=1+p+p^2+......+p^n;是一个等比数列,所以由等比数列求和可知f(p^n)=(1-p^(n+1))/(1-p);所以对于任何一个数,都可以先得出分解形式,n = (p1^a1)*(p2^a2)*(p3^a3)*......*(pn^an);所以f(n) = f(p1^a1)*f(p2^a2)*f(p3^a3)*......*f(pn^an);对于n^x,f(n^x)=f(p1^(a1*x))*f(p2^(a2*x))*f(p3^(a3*x))*......f(pn^(an*x));对于每一个f(n^x)可以用等比数列求得,到了这里,大致思路就清晰了,但是因为等比数列求和公式里面有除法,所以不能直接除法取模,在取模之前,需要先求一下除数的逆元,转除法为乘法,然后久可以直接取模了,求逆元可以用费马小定理,也可以用扩展欧几里得计算,这里代码用的是费马小定理。
代码:
#include<stdio.h> #include<iostream> using namespace std; int quick_pow(int a, int b) { int res = 1; while(b) { if(b & 1) { res = res*a%29; } a = a*a%29; b >>= 1; } return res%29; } int main() { int n; while(scanf("%d", &n) != EOF) { if(n == 0) return 0; int ans = 1; int x = quick_pow(2, 2*n+1)-1; int y = (quick_pow(3, n+1)-1)*quick_pow(2, 27); int z = (quick_pow(167, n+1)-1)*quick_pow(166, 27); ans = x*y*z%29; printf("%d\n", ans); } return 0; }