Mysterious Bacteria(唯一质因子解+素数筛)

博客探讨了一种名为RC-01的神秘细菌,其生命周期遵循特定的指数增长规律。文章提出了一个数学问题,即给定细菌的生存天数x,找出最大的整数p,使得x可以表示为某个整数b的p次方。为了解决这个问题,博主使用了素数筛方法来找出x的所有质因子,并通过计算这些质因子的次方数来确定最大可能的p值。样例输入和输出展示了该算法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题目:
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 = b的p次方 (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次方(b,p未知整数)

输入:
第一行一个整数T
接下来T行,每行一个x。

思路:
求x的唯一质数因子,因为是求质数因子,所以先用素数筛,将质数筛选出来,然后对筛选出来的质因子进行求次方数。
最后,求x各质因子的最大公因数(gcd)即可。

AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int N = 100010;
typedef long long ll;
int prime[N],ans=0;
bool ver[N];

void Prime()
{
    memset(ver,true,sizeof(ver));
    ver[1]=false;
    for(int i=2;i<N;i++)
    {
        if(ver[i])
        {
            prime[ans++]=i;
            for(int j=i;1LL*i*j<N;j++)
                ver[i*j]=false;
        }
    }
}

int gcd(int a,int b)
{
    return a%b==0?b:gcd(b,a%b);
}

int main()
{
    int t,p=0;
    ll n;
    Prime();
    cin>>t;
    while(t--)
    {
        p++;
        cin>>n;
        int f=0;
        if(n<0)
        {
            n=-n;
            f=1;
        }
        int x,k=0;
        for(int i=0;i<ans&&prime[i]*prime[i]<=n;i++)
        {
            if(n%prime[i]==0)
            {
                x=0;
                while(n%prime[i]==0)
                {
                    x++;
                    n/=prime[i];
                }
                if(k==0)
                    k=x;
                else
                    k=gcd(k,x);
            }
        }
        if(n>1)
            k=gcd(k,1);
        if(f==1)
        {
            while(k%2==0)
               k=k/2;
        }
        printf("Case %d: %d\n",p,k);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值