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

扫描二维码关注公众号,回复: 5034401 查看本文章

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);
    }

}

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/86592501