# POJ 1730 Perfect Pth Powers 两种方法（质数分解+pow枚举）

Perfect Pth Powers

 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18756 Accepted: 4327

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

Source

Waterloo local 2004.01.31

a^p*b^p=(a*b)^p,(a^p)^q=a^(p*q)

144=2^4*3^2=(2*3)^2*4=12^2

p从31到1遍历（2^31= 2147483647,int数据最大范围），求n的p次开方，转为int型的t，再求t的p次方，转为int型的x，解决了pow精度可能不准问题。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
using namespace std;
typedef long long ll;

const long N = 70000;
long prime[N] = {0},num_prime = 0;  //prime存放着小于N的素数
int isNotPrime[N] = {1, 1};        // isNotPrime[i]如果i不是素数，则为1
int Prime()
{
for(long i = 2 ; i < N ; i ++)
{
if(! isNotPrime[i])
prime[num_prime ++]=i;
//无论是否为素数都会下来
for(long j = 0 ; j < num_prime && i * prime[j] <  N ; j ++)
{
isNotPrime[i * prime[j]] = 1;
if( !(i % prime[j] ) )  //遇到i最小的素数因子
//关键处1
break;
}
}
return 0;
}
ll gcd(ll a,ll b) { return b == 0 ? a : gcd(b, a % b); }
int main()
{
ll n;
Prime();
while(scanf("%lld",&n)!=EOF)
{
int flag=0;
if(n==0) break;
if(n<0)
{
n=-n;
flag=1;
}
int cnt=0,ans=0;
for(int i=0;i<num_prime&&n>1;i++)
{
cnt=0;
while(n%prime[i]==0)
{
n/=prime[i];
cnt++;
}
ans=gcd(ans,cnt);
}
if(n!=1) ans=1; //说明其为素数 ,一开始忘了竟然超时
if(flag==1)
{
while(ans%2==0)
ans/=2;
}
printf("%d\n",ans);
}
return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n) && n)
{
if(n > 0)
{
for(int i = 31; i >= 1; i--)
{
int t = (int)(pow(n*1.0,1.0/i) + 0.1);

int x = (int)(pow(t*1.0,1.0*i) + 0.1);

if(n== x)
{
printf("%d\n",i);
break;
}
}

}
else
{
n = -n;
for(int i = 31; i >= 1; i-=2)
{
int t = (int)(pow(n*1.0,1.0/i) + 0.1);
int x = (int)(pow(t*1.0,1.0*i) + 0.1);
if(n == x)
{
printf("%d\n",i);
break;
}
}
}
}
return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
using namespace std;
typedef long long ll;
int n;

ll quick_qow(ll a,ll b)
{
long long  res=1;
while(b>0)
{
if(b%2) res=(res*a);
a=a*a;
b/=2;
}
return res;
}
int isPrime(int n)
{	//返回1表示判断为质数，0为非质数，在此没有进行输入异常检测
float n_sqrt;
if(n==2 || n==3) return 1;
if(n%6!=1 && n%6!=5) return 0;
n_sqrt=floor(sqrt((float)n));
for(int i=5;i<=n_sqrt;i+=6)
{
if(n%(i)==0 | n%(i+2)==0) return 0;
}
return 1;
}
int main()
{
ll n;

while(scanf("%lld",&n)!=EOF)
{
if(n==0) break;
if(isPrime(n))  {cout<<1<<endl;continue;}
int ans;
for(int i=2;i<=n;i++)
{
ll m=n;
ans=0;
while(m%i==0)
{
m/=i;
ans++;
}
if(m==1)
{
cout<<ans<<endl;
break;
}
}
}
return 0;
}