【LightOJ - 1220 】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,让求 满足 x=b^p的p的最大值 (注意x可正可负数)
分析: 设
x=p1^x1 p2 ^x2 …. pk^xk
b=p1^b1 p2^b2…..pk^bk
由题意可得:

 b1*p=x1 
 b2*p=x2 
 . ..  .  . .
 bk*p=xk 

 然后我们可以知道 想要让式子成立,p | gcd(x1,x2,x3...xk)   。
 所以 ,现在让求最大,如果x为正数,p=gcd(x1,x2,x3...xk) 
否则 p 就是gcd(x1,x2,x3,,,xk) 的最大奇数因子。
代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 1e5+11;
const int M = 1e6+11;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3;

bool su[N+1]={1,1,0};int prm[N],sz=0;
void init(){
    for(int i=2;i<N;i++){
        if(!su[i]) prm[sz++]=i;

        for(int j=0;j<sz;j++){
            LL t=prm[j]*i;
            if(t>N) break;
            su[t]=1;
            if(i%prm[j]==0) break;
        }
    }
}

LL gcd(LL a,LL b){
    return !b?a:gcd(b,a%b);
}
vector<int>ve;
LL solve(LL x){
    int f=1; if(x<0) f=0;
    x=abs(x);
    int flag=1; LL d;
    for(int i=0;i<sz && prm[i]*1ll*prm[i]<=x;i++){
        if(x%prm[i]==0){
            int ge=0;
            while(x%prm[i]==0) {ge++; x/=prm[i]; }
            if(flag )  { d=ge; flag=0; }
            else   d=gcd(d,ge);
        }
    }
    if(x!=1) d=1;

    if(f) return d;
    else {
        ve.clear(); LL y=d;
        for(LL i=1;i*1ll*i<=y;i++) {
            if(y%i==0){
                ve.push_back(i);
                if(y/i!=i) ve.push_back(y/i);
            }
        }
        sort(ve.begin(),ve.end());
        for(int i=ve.size()-1;i>=0;i--){
            int v=ve[i];
            if(v&1) {
                return v;
            }
        }
    }
}

int main(){
    init();
    int t;scanf("%d",&t);
    int cas=1;
    while(t--){
        LL x;scanf("%lld",&x);
        printf("Case %d: %lld\n",cas++,solve(x));
    }
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值