LightOJ - 1220-Mysterious Bacteria(gcd+分解质因数)

题目链接:http://lightoj.com/login_main.php?url=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

 

题目大意:对于一个x,他可以写成一个数的次幂形式:x = b^p (where b, p are integers),例:10=10^1,5=5^1,9=3^2

意思为:大细胞会在x天内生出p个小细胞,题目问的是给出一个x,输出p,x是32位有符号整数(可能为负),虽然x可能为负放在题目中不是很理解(   -x天 生出小细胞???!!!),但是就这把。。。

一个整数可以分解成若干个素数之积,例:10=2^1 * 5^1=10^1,5=5^1,12=2^2 * 3^1=12^1;

观察上述式子不难发现,素数还是本身,合数分解成素数之后,取决于最小的幂次的幂次,12=12^1;

在尝试写几个数:72=2^3 * 3^2=72^1; 144=2^4 * 3^2=12^2  ;((2^2*3^2)^2),发现只要分解出的素因子的次幂之间能够同时成一个数变成目标次幂,那么这个数就是答案了,简单来说就是GCD(x1,x2,...,xn);

如果一个数分解的若干个素数n=a1^x1 * a2^x2 * a3^x3 * .... * an^xn;

那么他们的因子可以分成n=(a1^y1*a2^y2*a3^y3*...*an^yn)^gcd(x1,x2,x3,...,xn);

如果n是负数的话,p只能是奇数(偶数是正数),将p一直除二即可;

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

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

int main()
{
	int t,num=1;
	cin>>t;
	while(t--)
	{
		ll n;
		int f=0;
		cin>>n;
		if(n<0)
		{
			n=-n;
			f=1;
		}
		int ans=0;
		for(ll i=2;i*i<=n;++i)
		{
			int x=0;
			while(n%i==0)
			{
				x++;
				n=n/i;
			}
			if(ans==0)
				ans=x;
			else
				ans=gcd(ans,x);
		}
		if(n>1)
			ans=gcd(ans,1);
		if(f)
		{
			while(ans%2==0)
				ans=ans/2;
		}
		cout<<"Case "<<num++<<": "<<ans<<endl;
	}
}

 

 

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81556833