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
思路:对各个素因子进行求最大公约数,当n<0时,n的每个因子必须化为奇数
#include <stdio.h>
#include <cmath>
const int maxn = 1e6 + 5000;
bool vis[maxn];
int p[maxn], tol = 0;
void init()
{
for (int i = 2; i < maxn; i++)
if (!vis[i])
{
p[tol++] = i;
for (int j = 2 * i; j < maxn; j += i)
vis[j] = true;
}
}
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x%y);
}
int main()
{
init();
int T, cc = 1;
scanf("%d", &T);
while (T--)
{
int n, e[40] = { 0 }, le = 0;
scanf("%d", &n);
if (n == 1 || n == -1 || n == 0)
{
printf("Case %d: %d\n", cc++, 1);
continue;
}
int tmp = n, ans = n;
bool ispos = true;
if (n < 0)
{
ispos = false;
tmp = 0 - n;
}
for (int i = 0; i < tol&&tmp>1; i++)
{
int d = 0;
while (tmp%p[i] == 0)
{
d++;
tmp /= p[i];
}
if (d)
e[le++] = d;
}
if (tmp > 1) e[le++] = 1;
for (int i = 0; i < le&&!ispos; i++)
{
while (e[i] % 2 == 0) e[i] /= 2;
}
ans = e[0];
for (int i = 0; i < le; i++)
ans = gcd(ans, e[i]);
printf("Case %d: %d\n", cc++, ans);
}
return 0;
}