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.
A test case of X = 0 indicates the end of input, and should not be processed.
1 10000 0
6 10
比较基础的题,作为新手个人感觉 先看答案再做. 不知道方法,做再久也做不出来的.还不如先看代码.
这道题求2014的x次方的所有因子的和,输出%29后的值.
暴力求解 是绝对不可能的,我们要做的是对2004^x的因子和%29这个公式进行化简.
下面就是套路了,满满的套路,一定要记住.
对于求一个数的因子和,我们要做的是分解成素因数的乘积.
2004^x=(2*2*3*167)^x
f(2004^x)=f(2^x)*f(2^x)*f(3^x)*f(167^x);
接下来还是套路!ps:这个充满套路的世界
p^k=1+p^1+p^2+p^3+....+p^k;积性方程!
所以f(2004^x)可以继续化简;
最 后得到 2^(2X+1)-1) * (3^(X+1)-1)* (22^(X+1)-1)/334
之后%29;
加减乘不用说.
主要 是那 个334是除.对于除的,我们取倒数.就是说334%29=9. 我们就*9,而不是/9;
(2^(2X+1)-1) * (3^(X+1)-1)* (22^(X+1)-1)*9)%29
公式出来了,代码就简单了
#include<stdio.h>
int ans,x;
int hanshu(int m,int n){
int ans=1;
while(n){
if(n&1) ans=(ans*m)%29;
m=(m*m)%29;
n>>=1;
}
return ans%29;
}
int main(){
while(scanf("%d",&x),x){
int ans=0;
ans=(hanshu(2,2*x+1)-1)%29;
ans=ans%29*(hanshu(3,x+1)-1)*15%29;
ans=ans%29*(hanshu(22,x+1)-1)*18%29;
printf("%d\n",ans%29);
}
return 0;
}