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 |
Output for Sample Input |
|
3 17 1073741824 25 |
Case 1: 1 Case 2: 30 Case 3: 2 |
题意:让求x = b^p的最大的p。
这个x是这个细菌能活的天数,而且还能是负的?!有点神奇
就是把x用算数基本定理化成质因数的幂乘积的形式,然后找出所有幂次的最大公约数,就是答案了。
要注意是如果x是负数的话,那么幂次就不能是偶数了,因为一个数的偶数次方肯定是正的嘛,就要把幂次变成奇数再求最大公约数。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
int f[maxn],p[maxn],cnt = 0;
void prime()
{
LL i,j;
f[1] = 1;
for(i=2;i<maxn;i++)
{
if(f[i])
continue;
p[cnt++] = i;
for(j=i*i;j<maxn;j+=i)
{
f[j] = 1;
}
}
}
int gcd(int a,int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
int main(void)
{
int T,i,j;
LL n;
prime();
scanf("%d",&T);
int cas = 1;
while(T--)
{
scanf("%lld",&n);
int flag = 0;
if(n < 0)
{
flag = 1;
n = -n;
}
i = 0;
int g = 0;
while(p[i]*p[i] <= n && i < cnt)
{
int t = 0;
while(n % p[i] == 0)
{
n /= p[i];
t++;
}
i++;
if(t == 0)
continue;
if(flag == 1)
{
while(t % 2 == 0)
{
t /= 2;
}
}
g = gcd(g,t);
}
if(n > 1)
g = 1;
printf("Case %d: %d\n",cas++,g);
}
return 0;
}

本文介绍了一种关于细菌RC-01繁殖周期的算法问题,该细菌的生命时长为x天,并能产生p个新细菌,其中x为p的整数次幂。文章通过质因数分解的方法求解x的最大p值,特别考虑了x可能为负数的情况。
236

被折叠的 条评论
为什么被折叠?



