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=a^p这个式子的最大的p
思路:
分解质因子发现:x=p1e1*p2e2*…*pk^ek,满足上面式子的p为gcd(e1,e2,e3,…,ek),如果x为负数,那么p肯定不能为偶数,因为一个数的偶数次方不可能为负数,所以说进行消除
#include<queue>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e6+5;
bool p[maxn];
int prime[maxn/10];
int tot;
int num;
int a[1005];
int b[1005];
void Init()
{
tot = 0;
memset(p,true,sizeof(p));
p[0] = p[1] = false;
for(long long i=2;i<maxn;i++)
{
if(p[i]){
prime[tot++] = i;
for(long long j=i*i;j<maxn;j+=i) p[j] = false;
}
}
}
void ndec(long long x)
{
num = 0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;1LL*prime[i]*prime[i]<=x;i++)
{
if(!(x%prime[i]))
{
a[num] = prime[i];
while(!(x%prime[i]))
{
b[num]++;
x/=prime[i];
}
num++;
}
}
if(x!=1)
{
a[num] = x;
b[num++] = 1;
}
}
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int main()
{
Init();
int t,id = 1;
long long x;
scanf("%d",&t);
while(t--)
{
int flag = 1;
scanf("%lld",&x);
if(x<0) x=-x,flag = 0;
ndec(x);
int tmp = b[0];
if(!flag)
{
if(!(tmp%2)) while(!(tmp%2)) tmp/=2;
for(int i=0;i<num;i++)
{
if(!(b[i]%2))
{
while(!(b[i]%2)) b[i]/=2;
}
tmp = gcd(tmp,b[i]);
}
}
else{
for(int i=0;i<num;i++) tmp = gcd(tmp,b[i]);
}
printf("Case %d: %d\n",id++,tmp);
}
return 0;
}