这是TopCoder SRM283的一道题目,属于数论的范畴。如果使用算法不适合,会出现超时。算法如下:
ret = 1;
for(int p = 2;(long long )p*p<=b; p++){
if(b%p==0){//p is prime factor of b
k = 0;
//calculate k that p^k is factor of b and p^(k+1) is not
while(b%p==0){
b = (int)(b/p);
k++;
}
c = (long long )(a/p);
h = 0;
int temp = p;
//judge if p^k is factor of a, if not , calculate h that
//p^h is factor of a, and p^(h+1) is not
while(a>=temp){
h += int(a/temp);
temp = temp*p;
if(h>k) break;
}
ret = ret*(int)pow(p*1.0,double(MIN(k,h)));
}
}
if(a>=b) ret = ret*b;
return ret;
ret = 1;
for(int p = 2;(long long )p*p<=b; p++){
if(b%p==0){//p is prime factor of b
k = 0;
//calculate k that p^k is factor of b and p^(k+1) is not
while(b%p==0){
b = (int)(b/p);
k++;
}
c = (long long )(a/p);
h = 0;
int temp = p;
//judge if p^k is factor of a, if not , calculate h that
//p^h is factor of a, and p^(h+1) is not
while(a>=temp){
h += int(a/temp);
temp = temp*p;
if(h>k) break;
}
ret = ret*(int)pow(p*1.0,double(MIN(k,h)));
}
}
if(a>=b) ret = ret*b;
return ret;