LightOj 1220 - Mysterious Bacteria 唯一分解定理+素數篩法

http://lightoj.com/volume_showproblem.php?problem=1220

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

Output for Sample Input

3

17

1073741824

25

Case 1: 1

Case 2: 30

Case 3: 2

 


PROBLEM SETTER: MUHAMMAD RIFAYAT SAMEE

SPECIAL THANKS: JANE ALAM JAN

 

任何一個大於1的自然數  ,都可以唯一分解成有限個質數的乘積  ,這裏  均爲質數,其諸指數  是正整數。
若x是負數,那麼所得答案一定要是奇數
這裏用ans不斷除2得到一個奇數,即答案

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn =1e5+10;
bool isPrime[maxn];
int prime[maxn];
int top;
void init()
{
    memset(isPrime,true,sizeof(isPrime));
    isPrime[0]=isPrime[1]=false;
    top=0;
    for(int i=2; i<maxn; i++)
    {
        if(isPrime[i])
        {
            prime[top++]=i;
            for(int j=i+i; j<maxn; j+=i)
            {
                isPrime[j]=false;
            }
        }
    }
}
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    init();
    int t,cas=0;
    cin>>t;
    while(t--)
    {
        ll n;
        int flag=0;
        int ans=0;
        cin>>n;
        if(n<0){
            flag=1;
            n=-n;
        }
        for(int i=0;prime[i]*prime[i]<=n&&i<top;i++){
            if(n%prime[i]==0){
                int cnt=0;
                while(n%prime[i]==0){
                    cnt++;
                    n/=prime[i];
                }
                ans=gcd(ans,cnt);
            }
            if(n==1)break;
        }
        if(n!=1)ans=gcd(ans,1);
        if(flag){
            while(ans%2==0){
                ans/=2;
            }
        }
        printf("Case %d: %d\n",++cas,ans);
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章