LightOj 1220

题目链接:http://lightoj.com/volume_showproblem.php?problem=1220

题意:已知 x=b中的 x 求最大的 p,其中 x b p 都为整数

x = (p1a1*p2a2*p3a3*...*pkak), pi为素数;则结果就是gcd(a1, a2, a3,...,ak);

当x为负数时,要把ans缩小为奇数;

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
typedef long long LL;
const int oo = 0xfffffff;
const int N = 1e6+5;
const double eps = 1e-8;


int f[N], p[N], k = 0;
void Init()
{
    for(int i=2; i<N; i++)
    {
        if(!f[i]) p[k++] = i;
        for(int j=i; j<N; j+=i)
            f[i] = 1;
    }
}

int gcd(int a, int  b)
{
    return b == 0 ? a : gcd(b, a%b);
}

int cnt[N];

int main()
{
    Init();

    int T, t = 1;

    scanf("%d", &T);

    while(T--)
    {
        LL n;
        int flag = 0;

        scanf("%lld", &n);

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

        int ans = 0;

        memset(cnt, 0, sizeof(cnt));

        for(int i=0; i<k && (LL)p[i]*p[i]<=n; i++)
        {
            while(n%p[i] == 0)
            {
                cnt[i]++;
                n /= p[i];
            }
            if(ans == 0 && cnt[i])
                ans = cnt[i];
            else if(cnt[i])
                ans = gcd(cnt[i], ans);
        }

        if(n > 1 || ans == 0)
            ans = 1;

        if(flag)///负数,结果一定是奇数;
        {
            while(ans%2 == 0)
                ans /= 2;
        }
        printf("Case %d: %d
", t++, ans);
    }
    return 0;
}
/*
-1000000
1

3
1
*/
View Code
原文地址:https://www.cnblogs.com/zhengguiping--9876/p/6016425.html