uva 10622——Perfect P-th Powers

来源:互联网 发布:网站原型设计软件 编辑:程序博客网 时间:2024/06/06 03:05
题意:给定一个数n,求最大的一个数k使得n=x^k。

思路1:正规的做法是把这个素数分解,然后求指数的最大公约数就是所求(听说有人取了最小值也能过,数据水吧!),素数打表,分解+欧几里得并不麻烦!(注意分解质因子用long long)

思路2:既然是某个数的K次方,那就依次枚举K好了,反正最多只有32次,有点暴力哈!

code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;

const double ep=1e-5;
int sol(int n)
{
int f=1;
if (n<0) f=-1;
for (int j=32;j>=2;j--)
{
int t=(long long)(pow(1.0*n*f,1.0/j)+ep)*f;
if (pow(t,j)==n)
return j;
}
return 1;
}
int main()
{
int n;
while (~scanf("%d",&n)&&(n!=0))
printf("%d\n",sol(n));
}

0 0