Mysterious Bacteria (唯一分解定律)

Dr. Mob has just discovered a Deathly Bacteria. He named it RC-01. RC-01 has a very strange reproduction system. RC-01 lives exactly x days. Now RC-01 produces exactly p new deadly Bacteria where x = bp (where b, p are integers). More generally, x is a perfect pth power. Given the lifetime x of a mother RC-01 you are to determine the maximum number of new RC-01 which can be produced by the mother RC-01.

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case starts with a line containing an integer x. You can assume that x will have magnitude at least 2 and be within the range of a 32 bit signed integer.

Output

For each case, print the case number and the largest integer p such that x is a perfect pthpower.

Sample Input

3

17

1073741824

25

Sample Output

Case 1: 1

Case 2: 30

Case 3: 2

分析:

n = p1^x1 * p2^x2 * p3^x3 ……

故找到 gcd(x1,x2,x3……)即可;

特别注意n为负,即只能为次幂!

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
int prime[maxn],cnt = 0;
bool vis[maxn];

void is_prime()
{
    memset(prime,0,sizeof prime);
    memset(vis,0,sizeof vis);
    for(int i = 2; i < maxn; i++)
    {
        if(!vis[i])
        {
            prime[cnt++] = i;
            for(int j = 2*i; j < maxn; j += i)
                vis[j] = 1;
        }
    }
}
int gcd(int a,int b)
{
    if(a%b == 0)
        return b;
    return gcd(b,a%b);
}
int main()
{
    is_prime();
    int T,Case = 1;
    scanf("%d",&T);
    while(T--)
    {
        ll n;
        scanf("%lld",&n);
        bool judge = true;
        if(n < 0)
        {
            n = -n;
            judge = false;
        }
        int sum = 0;
        for(int i = 0; i < cnt && prime[i]*prime[i] <= n; i++)
        {
            if(n%prime[i] == 0)
            {
                int cou = 0;
                while(n%prime[i] == 0)
                {
                    cou++;
                    n /= prime[i];
                }
                if(sum == 0) //凑到幂一样就行了
                    sum = cou;
                else
                    sum = gcd(sum,cou);
            }
        }
        if(n > 1)
            sum = gcd(sum,1);
        if(!judge)
            while(sum%2 == 0)
                sum /= 2;
        printf("Case %d: %d\n",Case++,sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Nothing_227/article/details/82902985