Mysterious Bacteria 素因子分解(唯一分解定理)

題意:(題目在最後)

給你一個整數n,讓你求滿足b^p=x的最大的p,例如72=2^3*3^2;結果是1;因爲只有72^1=72;

216=2^3*3^3=6^3,結果是3;

利用 唯一分解定理x=p1^e1*p2^e2*......pi^ei,存下每一個指數,再求最大公約數,得到答案;

注意:兩個點

1: n有可能是負的,這時候先把n變成正數求,因爲只有b^奇數纔可能爲負數,(n=-4時,-2^2=4,顯然不可能=n)

所以要把所有的指數不斷除2轉化成奇數(n=-64時,-2^6=(-2^2)^3=-4^3,結果是3)

2:素數打表用long long ,不然平方時會炸。

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=100005;
int tot=0,d,a[100];
ll ans[N/10];
bool vis[N];

void Prime()
{
    memset(vis,true,sizeof(vis));
    for(int i=2;i<N;i++)
    {
        if(vis[i]) ans[++tot]=i;
        for(int j=1;(j<=tot)&&(i*ans[j]<N);j++)
        {
            vis[i*ans[j]]=false;
            if(i%ans[j]==0) break;
        }
    }
}
int gcd(int a,int b) { return b?gcd(b,a%b):a;}

void sbreak(ll x)
{
    d=0;
    memset(a,0,sizeof(a));
    for(int i=1;ans[i]*ans[i]<=x;i++)
    {
        int cnt=0;
        if(x%ans[i]==0) d++;
        while(x%ans[i]==0)
        {
            x/=ans[i];
            a[d]=++cnt;
        }
    }
    if(x>1) {d=1;a[1]=1;}
}

int main()
{
    Prime();
    int t,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        ll x;
         bool tag=false;
        scanf("%lld",&x);
        if(x<0) {x=-x;tag=true;}
        sbreak(x);
        int t=a[1];
        if(tag)
        {
            for(int i=1;i<=d;i++)
            {
                while(a[i]%2==0)
                    a[i]/=2;
                t=gcd(t,a[i]);
            }
        }
        else
            for(int i=1;i<=d;i++)
               t=gcd(t,a[i]);
        printf("Case %d: %d\n",++cas,t);
    }
    return 0;
}


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




發佈了45 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章