Mysterious Bacteria(唯一分解定理,素数打表)

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

题意: 给出细菌寿命X,b^p=x,求最大p是多少。
根据唯一分解定理,每一个大于1的自然数都可唯一的分解成一些素数的乘积:
n=p1^a1 * p2^a2 . . .pn^an

实例:
48=2^3 * 3^1 ,maxp=gcd(3,1)=1;
50=2 * 5^2, maxp=gcd(5,2)=1;
144=2^4 * 3^2, maxp=gcd(4,2)=2;—>(2^2 * 3)^2

所以,本题所求实质上就是求 :a1,a2,a3,a4…an的最大公约数。

注意题中給的数据包含负数,输入的时候要注意处理。负数的情况下求出来的p必须是奇数,保证负号仍然活着。如果是偶数,就除二,直到变为奇数为止。
代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1000500
#define ll long long
int f[N],prim[N],k=0;  //f[N]用来记录是否是素数,是为0,否为1,prime[N]用来记录素数
int gcd(int a,int b)
{
    return a%b==0?b:gcd(b,a%b); //递归求a,b最大公约数(递(jing)归(jian))
}
void find_prim()    //素数打表,参考《挑战程序设计第二版》119页
{
    k=0;
    memset(f,0,sizeof(f));
    f[0]=f[1]=1;
    for(ll i=2; i<=N; i++)
    {
        if(!f[i])
        {
            prim[k++]=i;   
            for(ll j=i<<1; j<=N; j+=i)
                f[j]=1;
        }
    }
}
int main()
{
    ll n;  //最好定义为:long long型,有人定义成int型,下面处理负数时卡住了
    int t,cas=1,x,y;
    find_prim();   
    scanf("%d",&t);
    while(t--)
    {
        int flag=0;
        scanf("%lld",&n);
        if(n<0)
        {
            n=-n;  //<----就是这里!
            flag=1;
        }
        long long  ans=0,num,i=0;
        for(i=0;prim[i]*prim[i]<=n&&i<k;i++)  //唯一分解定理的应用
        {
            num=0;
            if(n%prim[i]==0)
            {
                while(n%prim[i]==0)
                {
                    num++;
                    n=n/prim[i];
                }
                if(ans==0)  //第一次
                    ans=num;
                else
                    ans=gcd(ans,num);
            }
        }
        if(n>1)  //说明最后处理完有质因子,没法搞,只能出1
            ans=gcd(ans,1);
        if(flag==1)
        {
            while(ans%2==0) //n为负数的情况,保证符号的正确性。
                ans/=2;
        }
        printf("Case %d: %d\n",cas++,ans);
    }
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值