Mysterious Bacteria ( lightOJ 1220)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1220
题目大意:输入T组数据,每组一个整数 n (可能为负数),求满足ap=n的最大的p
思路:

当n>0, 对n进行质因数分解,然后最大的 p 就是所有质因子出现次数的最大公因数,即n=pe11pe22pe33...pemmans=gcd(e1,e2,e3...em)
当n<0, 易知 ans 必须为奇数, 若 ans 为偶数,由 pans1=(p1x)y(y) 考虑对 ans 分解质因数,则必为 ans=2e1pe22pe33... 形式,故对 ans 一直除以2直到它为奇数即求得 y <script type="math/tex" id="MathJax-Element-85">y</script>可。

代码:
//代码太简单,不打备注了。

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#define LL long long
using namespace std;
const int Max=1e5+10;
bool vis[Max];
int prime[Max], mem;
void get_prime(){
    for(int a=2; a<Max; a++){
        if(!vis[a]){
            prime[mem++]=a;
            for(int b=a<<1; b<Max; b+=a){
                vis[b]=1;
            }
        }
    }
}
int gcd(int a,int b){
    return b==0?a:gcd(b, a%b);
}
int main(){
    get_prime();
    //for(int a=0; a<10; a++)cout<<prime[a]<<' ';
    int T, cases=1;
    scanf("%d", &T);
    while(T--){
        LL n;
        scanf("%lld", &n);
        int flag=0;
        if(n<0){
            flag=1;
            n=-n;
        }
        int ans=-1;
        for(int a=0; a<mem; a++){
            if(prime[a]>n)break;
            int e=0;
            while(n%prime[a]==0){
                e++;
                n/=prime[a];
            }
            if(e){
               ans= (ans==-1?e:gcd(ans, e));
            }
        }
        if(n!=1)ans=1;
        if(flag){
            while(ans%2==0){
                ans/=2;
            }
        }
        printf("Case %d: %d\n", cases++, ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值