1220

http://lightoj.com/volume_showproblem.php?problem=1220

题目大意: 给你一个x,求出满足 x=b^p, p最大是几。

分析:x=p1^a1*p2^a2*...*pn^an;

p最大是gcd(a1,a2,...,an)。

///他该诉你x,b,p都是整数,所以x,b有可能是负数。当x是负数时,p不能是偶数。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>

using namespace std;
typedef long long LL;
#define N 100000
#define ESP 1e-8
#define INF 0x3f3f3f3f
#define memset(a,b) memset(a,b,sizeof(a))

LL prime[N], k;
bool vis[N];

void Prime()
{
    memset(vis, false);
    k = 0;
    for(int i=2; i<N; i++)
    {
        if(vis[i] == 0)
        {
            prime[k ++] = i;
            for(int j=i+i; j<N; j+=i)
            {
                vis[j] = 1;
            }
        }
    }
}

LL gcd(LL a, LL b)
{
    LL r;
    while(b)
    {
        r = a%b;
        a = b;
        b = r;
    }
    return a;
}

LL solve(LL n)
{
    LL ans, sum;
    ans = 0;
    sum = 1;
    int flag = 0;

    for(int i=0; prime[i]*prime[i] <= n; i++)
    {
        if(n%prime[i] == 0)
        {
            ans = 0;
            while(n%prime[i] == 0)
            {
                n /= prime[i];
                ans ++;
            }
            if(flag == 0)
            {
                sum = ans;
                flag = 1;
            }
            else if(flag == 1)
                sum = gcd(sum, ans);
        }
    }
    if(n>1)
        sum = 1;

    return sum;
}

int main()
{
    int T, t=1;
    Prime();
    scanf("%d", &T);
    while(T --)
    {
        LL n;
        scanf("%lld", &n);

        int b = 0;

        if(n<0)
        {
            n = -n;
            b = 1;
        }

        LL sum = solve(n);

        if(b == 1 && sum%2 == 0)
        {
            while(sum%2 == 0)
                sum/=2;
        }
        printf("Case %d: %lld
", t++, sum);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/linliu/p/5549938.html