问题:
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,n=b的p次方,问p最大是多少。
这个题的数据范围给的不明确,就默认int型,我就用暴力试了下,他还真测试过了。
代码:
#define N 200001
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long poww(int a,int b)
{
long long c=a;
for(int i=1; i<b; i++)
c*=a;
return c;
}
int main()
{
int T,Case=1;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int sum=1,f=0;//sum就是最大的p,p最小是1
int s=n;
if(n<0)s=-n;//测试数据存在负数
for(int i=2; i<=sqrt(s); i++)
{
int ii=i;
if(n<0)ii=-i;//n是负数的情况
int j=2;//p从2开始遍历
while(1)
{
if(n<0)j++;//当n为负数时n的偶数次方肯定是不对的,跳过
long long a=poww(ii,j);//求ii的j次方
if(abs(a)>abs(n))break;//如果所得结果超过了n就结束
if(a==n)
{
sum=j;//如果找到ii^j==n,则j就是最大的。
f=1;
break;
}
j++;
}
if(f)break;
}
if(n==-2147483648)sum=31;/*当n为整型最小的情况是要特判,因为整形范围是-2147483648~2147483648-1,,上面运行s=-n的时后,会数出现错误*/
printf("Case %d: %d\n",Case++,sum);
}
return 0;
}