TOJ 1438.Perfect Pth Powers

来源:互联网 发布:西安鼓楼网络售票 编辑:程序博客网 时间:2024/05/17 10:26

题目链接:http://acm.tju.edu.cn/toj/showp1438.html


1438.   Perfect Pth Powers
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 bx = b2. Similarly, x is a perfect cube if, for some integer bx = b3. More generally, x is a perfect pth power if, for some integer bx = bp. Given an integer x you are to determine the largest p such that x is a perfect pth 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

171073741824250

Output for Sample Input

1302


Source: Waterloo Local Contest Jan. 31, 2004
Submit   List    Runs   Forum   Statistics

水题,直接用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;}}}}}


0 0
原创粉丝点击