Poj 1730 Perfect Pth Powers

Perfect Pth Powers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14512   Accepted: 3263

Description

We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More generally, x is a perfect pth power if, for some integer b, x = bp. Given an integer x you are to determine the largest p such that x is a perfect pth power.

Input

Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.

Output

For each test case, output a line giving the largest integer p such that x is a perfect pth power.

Sample Input

17
1073741824
25
0

Sample Output

1
30
2

Source

//本题恶心之处在于居然有负数、、、
//找出X的不同素因子个数、找出最小的Min,如果其它因子个数是它的倍数,那么说明Min就是最大的p
//对于负数,找出Min后,要把Min里面的的因子2去掉、就可以了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <cmath>
#define N 50001
using namespace std;
bool h[N];
int rc[5555];
int nt;
void set()
{
    int i,j,k=1;
    for(i=4;i<N;i+=2)
      h[i]=1;
    for(i=3;i<=300;i+=2)
      if(!h[i])
      {
          for(j=i*i;j<N;j+=i)
            h[j]=1;
      }
    for(i=3;i<N;i+=2)
     if(!h[i])
      rc[k++]=i;
   rc[0]=2;
   nt=k;
}
int main()
{
    set();
    __int64 x;
    int i,a[20],k,Min;
    bool b,f;
    while(scanf("%I64d",&x),x)
    {   memset(a,0,sizeof(a));
        f=0;
        if(x<0) {x=-x,f=1;}
        k=0;
        for(i=0;i<nt;i++)
         {    b=0;
             while(x%rc[i]==0)
             {
                b=1;
                a[k]++;
                x=x/rc[i];
             }
             if(b) k++;
             if(x==1) break;
         }
         if(x>1) a[k]++,k++;
         for(Min=100,i=0;i<k;i++)
          Min=min(Min,a[i]);
         for(b=1,i=0;i<k;i++)
          if(a[i]%Min)
            {b=0;break;}
         if(Min==1){ printf("1\n");continue;}
         if(b)
         {
             if(f&&Min%2==0)
             {
                 while(Min%2==0)
                   Min/=2;
             }
             printf("%d\n",Min);
         }
         else
          printf("1\n");

    }
    return 0;
}

posted on 2012-08-01 18:12  江财小子  阅读(269)  评论(0编辑  收藏  举报