LightOJ1220 Mysterious Bacteria 菜鸡式打表

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为负数,然而菜鸡的暴力是无所畏惧的,(分正负打表/大哭)
代码:

#include<cstdio>
#include<cmath>
#define M 100000
#define ll long long
using namespace std;
struct Pw
{
 ll x;
 int p;
}num[M],w[2000];
int pos1 = 0,pos2 = 0;
void init()
{
 ll max = pow(2,32);
 int i,j;
 for (i = 2;i <= 65536;i ++)
 {
  j = 2;
  while (pow(i,j) <= max)
  {
   num[pos1].x = pow(i,j);
   num[pos1].p = j;
   ++ pos1,++ j;   
  }
 }
 for (i = -2;i >= -1300;i --)
 {
  j = 3;
  while (pow(i,j) >= -max)
  {
   w[pos2].x = pow(i,j);
   w[pos2].p = j;
   ++ pos2,j += 2;
  }
 }
}
int main()
{
 init();
 int t,i,j;
 scanf("%d",&t);
 for (j = 0;j < t;j ++)
 {
  ll a;
  scanf("%lld",&a);
  int flag = 1;
  if (a > 0)
  {
  for (i = 0;i < pos1;i ++)
   if (a == num[i].x)
   {
    printf("Case %d: %d\n",j + 1,num[i].p);
    break;
   }
  if (i == pos1)
   printf("Case %d: %d\n",j + 1,1);
  }
  else
  {
   for (i = 0;i < pos2;i ++)
   if (a == w[i].x)
   {
    printf("Case %d: %d\n",j + 1,w[i].p);
    break;
   }
   if (i == pos2)
   printf("Case %d: %d\n",j + 1,1);
  }
 }
 return 0;
}
盗一波数据/狗头

sample input:

8
2147483647
-2147483648
32
-32
64
-64
4
-4

Output:

Case 1: 1
Case 2: 31
Case 3: 5
Case 4: 5
Case 5: 6
Case 6: 3
Case 7: 2
Case 8: 1

猜你喜欢

转载自blog.csdn.net/cloudy_happy/article/details/80144817