http://poj.org/problem?id=1845
题意:
求A^B的所有正约数的和模9901的结果。 A,B<=50000000。
思路:
先将A^B进行因式分解,其实只要对A进行因式分解即可,假设A的一个质因子为
pi,次数为ci ,则A^B中pi的次数就是ci*B次。这样就可以得到下面的式子:
A^B = (p1^c1 * p2^c2 * p3^c3 * .. * pn^cn )^B , 也就是下面这个式子:
A^B = p1^(c1*B) * p2^(c2*B) * ...... * pn^(cn*B) ) ,因为A^B的约数,就是在它的
质因子中进行选择,每个质因子都有(ci*B+1)中选择,换一种想法也就是每个质
因子都有不选,选1个,选2个,选3个....选ci*B个这么多种选择,而要构成一个
因数,则选择所有的质因子的选择情况想乘起来,然后再将这些因数相加就是最
终的结果。也就是sum = (1+p1+p1^2+p1^3 + .. + p1^(c2*B) ) * (1+p2+p2^2+p2^3
+ .. + p2^(c2*B) ) * ()* (1+pn+pn^2+pn^3 + .. + p1^(cn*B) ),解下去的就是快速幂
取模的运算了,这里不再详细说明。
代码:
#include<stdio.h>
#include<string.h>
typedef long long LL ;
LL a ,b ;
const LL Mod = 9901 ;
const int N = 10000 ;
bool is_p[N] ;
int p[N] ,cnt ;
LL ans ;
void init(){
for(int i=1;i<N;i++) is_p[i] = 1 ;
cnt = 0 ;
is_p[1] = 0 ;
for(int i=2;i<N;i++){
if( is_p[i] == 0 )continue ;
p[cnt++] = i ;
for(int j=2;j*i<N;j++){
is_p[i*j] = 0 ;
}
}
}
LL cal(LL a ,LL b){
LL res=1 , add =a ;
while(b){
if(b & 1){
res = res * add % Mod ;
}
add = add * add % Mod ;
b /= 2 ;
}
return res ;
}
LL calc(LL p , LL n){
if(n == 0) return 1 ;
if(n == 1) return (p+1)%Mod ;
LL a = (n+1) / 2 - 1;
LL res = calc( p, a );
LL rr = cal( p , a+1 ) ;
rr = ( rr + 1 ) % Mod ;
res = res*rr % Mod ;
if(n & 1){
;
}
else{
res = (res + cal(p , n) ) % Mod ;
}
return res ;
}
void solve(){
LL n = a ,res;
ans = 1 ;
for(int i=0;i<cnt && p[i]*p[i]<=n;i++){
if( n%p[i] == 0 ){
LL c = 0 ;
while( n%p[i]==0 ){
c++ ;
n /= p[i] ;
}
res = c * b ;
res = calc( p[i] , res ) ;
ans = ans * res % Mod ;
}
}
if(n > 1){
res = b ;
res = calc( n , res ) ;
ans = ans * res % Mod ;
}
printf("%lld\n",ans);
}
int main(){
init() ;
while(scanf("%lld%lld",&a,&b) == 2){
solve() ;
}
return 0;
}