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.
InputThe 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.
OutputFor 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,求2004的x次方的所有因子和s,取模29的值。
解题思路:首先告诉了,2004的因子有1,2,3, 4, 6, 12, 167, 334, 501, 668, 1002 and 2004,故和为s=4704,然后取模29得到6(这里有点问题),首先用暴力解法,直接枚举出所有因子再求和,那么一定会超时;然后根据整数的唯一分解定律:任意整数都有且只有一种方式写出其素因子的乘积表达式,A=(p1k1)*(p2k2)*(p3k3)*…*(pnkn);然后推出约数和公式,就是所有因子和S=(1+p1+p12+…p1k1)(1+p2+p22+…+p2k2)…(1+pn+pn2+pnkn)所以得利用这个公式来求解;再分析2004=2^23*167;即素因子就为2,3,167;
解题步骤:
1)通过验算找出2004的素因子,然后再找出为x次方状态下的情况;
2)带入那个求解S的约数和公式;
3)再定义一个函数,求解每一个素因子在约数和表达公式中所代表项的值;
#include <iostream>
#include <cstdio>
long long p(long long a,long long b)
{
long long x=1;
a=a%29;
while(b>0)
{
if(b%2==1)
x=(x*a)%29;
b=b/2;
a=a*a%29;
}
return x;
}
int main()
{
long long x;
while(scanf("%lld",&x)&&x)
{
long long ans;
ans=(p(2,2*x+1)-1)*(p(3,x+1)-1)*(p(167,x+1)-1)*9%29; //注意这里乘以一个9,具体原因我现在还不知道,应为不乘以的话,所取模的结果和要得到的结果不一致;
printf("%lld\n",ans);
}
return 0;
}