Mysterious Bacteria(唯一分解定理)

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=bp,p的最大值

思路: 由唯一分解定理,一个整数x=p1 a1 *p2a2*p3a3…,即对于一个x,我们先把它分解为n个质数相乘,则 所求p= gcd(a1,a2,a3,a4,a5,…,an),如果x为负,指数就不能为偶数(负数的偶次方为正数),如果算出答案为偶,就除2到为奇

AC代码:

#include<stdio.h>
#include<string.h>
#define ll long long
#include<algorithm>
using namespace std;
const int N=110000;
ll p[N],book[N],l,n;
void init()
{
	ll i,j;
	l=0;
	memset(book,0,sizeof(book));
	for(i=2;i<N;i++)
	{
		if(!book[i])
		{
			p[l++]=i;
			for(j=0;i*p[j]<=N&&j<l;j++)
			{
				book[i*p[j]]=1;
				if(i%p[j]==0)
				break;
			}
		}
	}
}
int gcd(ll a,ll b)
{
	if(b==0)
	return a;
	else
	return gcd(b,a%b);
}
int main()
{
	ll t,c=1;
	scanf("%lld",&t);
	init();
	while(t--)
	{
		int sum=0,f=0;
		scanf("%lld",&n);
		if(n<0)
		{
			f=1;
			n=-n;
		}
		for(ll i=0;i<l&&p[i]*p[i]<=n;i++)
		{
			if(n%p[i]==0)
			{
				int cnt=0;
				while(n%p[i]==0)
				{
					n/=p[i];
					cnt++;
				}
				if(cnt==0)
				sum=cnt;
				else
				sum=gcd(sum,cnt);
			}
		}
		if(n!=1)
		sum=1;
		if(f)
		{
			while(sum%2==0)
			sum/=2;
		}
		printf("Case %lld: %lld\n",c++,sum);
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值