Description
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
A test case of X = 0 indicates the end of input, and should not be processed.
Output
Sample Input
Sample Output
该题讲的是因子和问题,因子和是什么就不用说了,在百度里有,可以上网搜一下,主要是讨论做该题会不会超时,如果按照常规解法,肯定超时,所以,用快速幂乘的方法。
因子和
6的因子是1,2,3,6; 6的因子和是 s(6)=1+2+3+6=12;
20的因子是1,2,4,5,10,20; 20的因子和是 s(20)=1+2+4+5+10+20=42;
2的因子是1,2; 2的因子和是 s(2)=1+2=3;
3的因子是1,3; 3的因子和是 s(3)=1+3=4;
4的因子和是 s(4)=1+2+4=7;
5的因子和是 s(5)=1+5=6;
s(6)=s(2)*s(3)=3*4=12;
s(20)=s(4)*s(5)=7*6=42;
这是巧合吗?
再看 s(50)= 1+2+5+10+25+50=93=3*31=s(2)*s(25),s(25)=1+5+25=31.
这在数论中叫积性函数,当gcd(a,b)=1时 s(a*b)=s(a)*s(b);
如果p是素数
s(p^n)=1+p+p^2+...+p^n= (p^(n+1)-1) /(p-1) (1)
例 hdu1452 Happy 2004
计算 因子和 s(2004^X) mod 29 ,
2004=2^2 *3 *167
s(2004^X) ) = (s(2^2X))) * (s(3^X))) * (s(167^X)))
167)=22;
s(2004^X) ) = (s(2^2X))) * (s(3^X))) * (s(22^X)))
a=s(2^2X)=(2^(2X+1)-1) //根据 (1)
b=s(3^X)= (3^(X+1)-1)/2 //根据 (1)
c=s(22^X)= (22^(X+1)-1)/21 //根据 (1)
%运算法则 1. (a*b) %p= ( a%p) *(b%p)
%运算法则 2. (a/b) %p= ( a *b^(-1)%p)
b^(-1)是 b的逆元素 (%p)
2的逆元素是15 ()) ,因为2*15=30 % 29=1 % 29
21的逆元素是18 ()) ,因为21*18=378% 29 =1 % 29
因此
a=(powi(2,2*x+1,29)-1)% 29;
b=(powi(3,x+1,29)-1)*15 % 29;
c=(powi(22,x+1,29)-1)*18 % 29;
ans=(a*b)% 29*c % 29;
#include <iostream> #include <math.h> const int M=29; using namespace std; int p(int a,int n) { int b=1; while(n>1) if(n%2==0) { a=a*a%M; n/=2; } else { b=b*a%M; n--; } return a*b%M; } int main() { int n,a,b,c; while(cin>>n&&n) { a=p(2,2*n+1)-1; b=(p(3,n+1)-1)%29*15; c=(p(22,n+1)-1)*18; cout<<a*b%29*c%29<<endl; } return 0; }