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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值