Mysterious Bacteria
Description
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,p最大是多少?
算法思想:
x可以表示为:x = p1^e1 * p2^e2 * p3^e3 ....... * pn^en。
p = gcd (e1,e2,.......en);
坑点:当x是负数的时候,p的值不能为偶数。
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
int vis[maxn];
vector<int> prime;
void get_prime(){
memset(vis,false,sizeof(vis));
vis[1] = true;
for(int i = 2; i <= 1000000; i++){
int t = 1000000/i;
for(int j = 2; j <= t; j++)
vis[i*j] = true;
}
for(int i = 2; i <= 1000000; i++)
if(!vis[i])
prime.push_back(i);
}
int gcd(int a, int b){
if(a%b == 0)
return b;
else
return gcd(b,a%b);
}
int main (){
get_prime();
int T,t = 1;
scanf("%d",&T);
while(T--){
int ans = 0,flag = 0;
ll n;
scanf("%lld", &n);
if(n < 0){
n = -n;
flag = 1;
}
int len = prime.size();
for(int i = 0; i < len && prime[i] < n; i++){
int num = 0;
if(n%prime[i] == 0){
while(n%prime[i] == 0){
n /= prime[i];
num++;
}
if(ans == 0)
ans = num;
else
ans = gcd(ans,num);
}
}
if(n != 1)
ans = 1;
if(flag){
while(ans%2 == 0)
ans /= 2;
}
printf("Case %d: %d\n",t++,ans);
}
return 0;
}