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 = p1^e1 * p2^e2 * p3^e3 ……. * pn^en。
p = gcd (e1,e2,…….en);
坑点:当x是负数的时候,p的值不能为偶数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1e6+10;
typedef long long LL;
int prime[maxn+1];
void getPrime()
{
memset(prime,0,sizeof(prime));
for(int i=2;i<=maxn;i++)
{
if(!prime[i])
{
prime[++prime[0]]=i;
}
for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++)
{
prime[prime[j]*i]=1;
if(i%prime[j]==0)
{
break;
}
}
}
}
vector<int> vec;
LL num;
int getfactor()
{
for(int i=1;i<=prime[0]&&prime[i]*prime[i]<=num;i++)
{
if(num%prime[i]==0)
{
int ans=0;
while(num%prime[i]==0)
{
num/=prime[i];
ans++;
}
vec.push_back(ans);
}
if(num==1) break;
}
if(num!=1) vec.push_back(1);
int siz = vec.size();
if(siz==1) return vec[0];
int gcd = vec[0];
for(int i=1;i<siz;i++)
{
gcd = __gcd(gcd,vec[i]);
}
return gcd;
}
int main()
{
getPrime();
int T;
scanf("%d",&T);
int ks=1;
while(T--)
{
bool flag=0;
vec.clear();
scanf("%lld",&num);
if(num<0) {num=-num;flag=1;}
LL ans = getfactor();
if(flag)
{
while(ans%2==0)
ans/=2;
}
printf("Case %d: %lld\n",ks++,ans);
}
return 0;
}