lightoj1220 唯一分解定理

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 pth power.

Sample Input

3

17

1073741824

25

Sample Output

Case 1: 1

Case 2: 30 

Case 3: 2

分解 质因子,如果是正数,直接输出因子的gcd-p,否则如果是奇数,分解--x,输出因子gcd,如果偶数,不断转化为奇数,因为负数的偶数次幂不可能是负数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1000005
using namespace std;
#define ll long long
ll num[maxn];
ll p[maxn];
bool vis[maxn];
int flag;
int k;
ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
void sp()
{
    k=0;
    memset(vis,0,sizeof(vis));
    vis[0]=vis[1]=1;
    for(int i=2;i<maxn;i++)
    {
        if(!vis[i])
            p[k++]=i;
            for(int j=0;j<k&&i*p[j]<maxn;j++)
           {
               vis[i*p[j]]=1;
            if(i%p[j]==0)
                break;
    }
}
}
ll getf(ll x)
{
    ll sum=0;
    for(int i=0;i<k&&p[i]*p[i]<=x;i++)
    {
        ll res=0;
        while(x%p[i]==0&&x)
        {
            x/=p[i];
            res++;
        }
        if(flag)
        while(res%2==0&&res)
        res/=2;
        if(res&&!sum)
            sum=res;
            else if(res)
                sum=gcd(res,sum);

    }
    if(x>1)
        sum=1;
    return sum;
}
int main()
{
    int t;
    scanf("%d",&t);
    int w=0;
    sp();
    while(t--)
    { ll x;
    w++;
        scanf("%lld",&x);
        flag=0;
        if(x<0)
        {
            x=-x;
            flag=1;
        }
    printf("Case %d: %lld\n",w,getf(x));

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/86592444