1220 - Mysterious Bacteria
Time Limit:500MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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=a^p这个式子的最大的p
思路:分解质因子发现:x=p1^e1*p2^e2*...*pk^ek,满足上面式子的p为gcd(e1,e2,e3,...,ek),如果x为负数,那么p肯定不能为偶数,因为一个数的偶数次方不可能为负数,所以说进行消除
总结:写完后一直RE,查半天没查到,最后注意到打的表有限,加上了一个判断条件,过了= =
ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1000100
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
int v[MAXN],prime[MAXN];
int k;
void isprime()
{
int i,j;
mem(v);
v[1]=1;
k=0;
for(i=2;i<MAXN;i++)
{
if(v[i]==0)
{
prime[k++]=i;
for(j=i*2;j<MAXN;j+=i)
v[j]=1;
}
}
}
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int main()
{
isprime();
LL n;
int t,i;
int cas=0;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
int bz=0;
if(n<0)
n=-n,bz=1;
int ans=0;
for(i=0;prime[i]*prime[i]<=n&&i<k;i++)//就是这,因为表的限制,所以i不能大于等于k
{
if(n%prime[i]==0)
{
int cnt=0;
while(n%prime[i]==0)
{
cnt++;
n/=prime[i];
}
if(ans==0)
ans=cnt;
else
ans=gcd(ans,cnt);
}
if(n==1)
break;
}
if(n>1)
ans=1;
if(bz)
{
while(ans%2==0)
ans/=2;
}
printf("Case %d: ",++cas);
printf("%d\n",ans);
}
return 0;
}