Mysterious Bacteria LightOJ - 1220

题意:

给出一个数x 求 x = bp  的p的最大值

解析:

算术基本定理 分解质因数

任何一个数x都可以表示为  x = p1a1   *  p2a2  *  ````` * pnan

 则  根号下bp  ==  根号下p1a1   *  p2a2  *  ````` * pnan

所以  p  == gcd(a1,a2,·····,an);

如果x是一个负数  则p只能为奇数

先把x换成正数求出最大p之后 如果x 为负 则不断除2 直至p为奇数

代码如下:

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define maxn 1000100
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int LL_INF = 0x7fffffffffffffff,INF = 0x3f3f3f3f;
int primes[maxn];
bool vis[maxn];
int ans = 0;
LL gcd(LL a, LL b)
{
    return b==0?a:gcd(b,a%b);
}

void init()
{
    mem(vis,0);
    for(int i=2; i<maxn; i++)
        if(!vis[i])
        {
            primes[ans++] = i;
            for(LL j=(LL)i*i; j<maxn; j+=i)
                vis[j] = 1;
        }
}

int main()
{
    init();
    LL n;
    int T, kase = 0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        int flag = 0;
        int res = 0;
        if(n < 0)
            n = -n,flag = 1;
        for(LL i=0; primes[i] * primes[i] <= n && i < ans; i++)
        {
            int cnt2 = 0;
            while(n % primes[i] == 0)
            {
                n /= primes[i];
                cnt2++;
            }
            if(cnt2 > 0)
            {
                if(res == 0)
                    res = cnt2;
                res = gcd(res, cnt2);
            }
        }
        if(n > 1)
        {
            res = 1;
        }
        if(flag)
        {
            while(res % 2 == 0)
                res /= 2;
        }
            
        printf("Case %d: %d\n",++kase,res);


    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/WTSRUVF/p/9190087.html