POJ 1730 Perfect Pth Powers

来源:互联网 发布:超短线战法 知乎 编辑:程序博客网 时间:2024/06/08 06:11
Perfect Pth Powers
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 17339 Accepted: 3974

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

171073741824250

Sample Output

1302


题目大意:要求出一个完美的平方数,完美的平方数是这样的,n = bp的当P最大的时候才是一个完美的平方数

解题思路:将P从31到1遍历枚举,使用POW函数将他求出来,不过有一点问题就是精度问题,当用POW(125,1/3),直接取整的时候是4,所以需要在后面加一个0.1,也就是(int)(POW(125,1/3)+0.1).这样就可以求出结果了,还有要注意的是,他给出的N可能是负数,所以要对负数特殊处理,也就是说当N是负数的时候,P不可能是偶数,只能是奇数。

#include <cstdio>#include <cmath>using namespace std;//typedef long long ll;//ll x;int x;int main(){    while (scanf("%d", &x), x){        if (x > 0){            for (int i = 31; i>= 1; i--){                int a = (int)(pow(x * 1.0, 1.0 / i) + 0.1);                int b = (int)(pow(a * 1.0, 1.0 * i) + 0.1);                if (x == b){                    printf("%d\n", i);                    break;                }            }        }        else{            x = -x;            for (int i = 31; i>= 1; i -= 2){                int a = (int)(pow(x * 1.0, 1.0 / i) + 0.1);                int b = (int)(pow(a * 1.0, 1.0 * i) + 0.1);                if (x == b){                    printf("%d\n", i);                    break;                }            }        }    }    return 0;}


0 0
原创粉丝点击