LightOJ 1220

写出来发现自己思路跟网上的题解不太一样...应该算是比较投机的做法?

题意: 给出一个整数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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值