写出来发现自己思路跟网上的题解不太一样...应该算是比较投机的做法?
题意: 给出一个整数x(可能为负数), 找到使得 x = b^p 的最大p
思路: 感觉输入最大才2^32就考虑用暴力了 考虑边界情况 (2^16) ^2 = 2^32 所以暴力使i 从 2开始跑到 2^16 寻找是否存在 p使得 i^p==x 代码里用了快速幂优化
负数的话思路也是一样的
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int maxn = (1<<16)+100;
ll mod_pow(ll x,ll n)
{
if(n==0)
return 1;
ll res = mod_pow(x*x,n/2);
if(n&1)
res = res*x;
return res;
}
int main()
{
int T,cas=1;
scanf("%d",&T);
for(cas=1; cas<=T; cas++)
{
ll ans = -1;
ll n;
scanf("%lld",&n);
if(n>0)
{
for(ll i=2; i<=maxn; i++)
{
for(ll j=2; j<=32; j++)
{
if(mod_pow(i,j)>n)
break;
if(mod_pow(i,j)==n)
ans = max(ans,j);
}
}
printf("Case %d: %lld\n",cas,ans==-1?1:ans);
}
else
{
for(ll i=-2; i>=-maxn; i--)
{
for(ll j=1; j<=32; j+=2)
{
if(mod_pow(i,j)<n)
break;
if(mod_pow(i,j)==n)
ans = max(ans,j);
}
}
printf("Case %d: %lld\n",cas,ans==-1?1:ans);
}
}
return 0;
}
至于另一种用质因子分解的解法可以见别人的博客: http://blog.csdn.net/lxpaopao/article/details/45400301