题目链接:http://acm.tju.edu.cn/toj/showp1438.html
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 2341 Accepted Runs: 466
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 p
th 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 p
th power.
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.
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
Output for Sample Input
1 30 2
Source: Waterloo Local Contest Jan. 31, 2004
水题,直接用pow就好,注意n的范围。
#include <stdio.h>
#include <cmath>
using namespace std;
int main()
{
long long n;
while(~scanf("%lld",&n),n){
if(n>0){
for(int i=31;i>0;i--){
double p=pow(1.0*n,1.0/i);
for(long long j=p-10;j<=p+10;j++)
if( (long long)pow(1.0*j,i)==n){
printf("%d\n",i);
i=0;
break;
}
}
}
else {
n=-n;
for(int i=31;i>0;i-=2){
double p=pow(1.0*n,1.0/i);
for(long long j=p-10;j<=p+10;j++)
if( (long long)pow(1.0*j,i)==n){
printf("%d\n",i);
i=0;
break;
}
}
}
}
}