Description
We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More generally, x is a perfect pth power if, for some integer b, x = bp. Given an integer x you are to determine the largest p such that x is a perfect pth power.
Input
Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.
Output
For each test case, output a line giving the largest integer p such that x is a perfect pth power.
Sample Input
17
1073741824
25
0
Sample Output
1
30
2
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x;
int f=0;
ll power(ll x,int y) {
ll res=1;
while(y) {
if(y&1) res*=x;
x*=x;
y>>=1;
}
return res;
}//快速幂
int main() {
while(true) {
scanf("%lld",&n);
if(n==0) break;
if(n<0) {
n=-n;
for(ll i=31; i>=1; i-=2) {
x=pow(n,1.0/i)+0.5;
f=0;
for(ll j=x-2; j<=x+1; j++) {
if(power(j,i)==n) {
printf("%lld\n",i);
f=1;
break;
}
}
if(f==1) break;
}
} else {
for(ll i=32; i>=1; i--) {
x=pow(n,1.0/i)+0.5;
f=0;
for(ll j=x-2; j<=x+1; j++) {
if(power(j,i)==n) {
printf("%lld\n",i);
f=1;
break;
}
}
if(f==1) break;
}
}
}
return 0;
}