lightoj1220——线筛+唯一分解定理(有坑)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1220

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

题目翻译:

求满足条件的最大的指数p,使得a^p = x(a是整数)

Input

输入包含多组样例T(T<=50),每个样例包括一个整数x,x的范围在32位二进制数以内。

Output

对于每个样例输出最大的指数p。

 

题意很好理解,而且也很容易想到用唯一分解定理+线筛来做,其实懂得代码实现的过程就可以知道最后的结果就是所有因子个数的最大公约数。

这个题的坑点在于会有负数,因此对负数需要特殊处理(转换成正数)和判断,如果求出的p是奇数的话保持不变,否则的话对其不断的除2,使其变成奇数。

#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int N = 1e6 + 5;
bool visited[N];//visited[i]表示i是不是质数 
int prime[N], tot;//prime[N]用来存质数 
void init(){
    for(int i = 2; i < N; i ++) visited[i] = true;//初始化为质数 
    for(int i = 2; i < N; i++){
        if(visited[i]) prime[tot ++] = i;//把质数存起来 
        for(int j = 0; j < tot && i * prime[j] < N; j++){
            visited[i * prime[j]] = false;
            if(i % prime[j] == 0) break;//保证每个合数被它最小的质因数筛去 
        }
    }    
}
int count(ll n){
	int temp;
	int first=1;
	for(int i=0;prime[i]*prime[i]<=n&&i<tot;++i){
		if(n%prime[i]==0){
			int cnt=0;
			while(n%prime[i]==0){
				n/=prime[i];
				cnt++;
			}
			if(first){
				first=0;
				temp=cnt;
				continue;
			}
			temp=__gcd(temp,cnt);
		}
	}
	if(n>1) temp=__gcd(temp,1);
	return temp;
}
int main(){
	int T;
	ll x;
	scanf("%d",&T);
	init();
	for(int kcase=1;kcase<=T;kcase++){
		int flag=0;
		scanf("%lld",&x);
		if(x<0){
			x=-x;
			flag=1;
		}
		int num=count(x);
		if(flag)
			while(num%2==0) 
				num/=2;
		printf("Case %d: %d\n",kcase,num);
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值