Poj 1730 Perfect Pth Powers
2012-08-01 18:12
756 查看
Perfect Pth Powers
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
Sample Output
Source
Waterloo local 2004.01.31
//本题恶心之处在于居然有负数、、、
//找出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
;
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;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14512 | Accepted: 3263 |
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
Waterloo local 2004.01.31
//本题恶心之处在于居然有负数、、、
//找出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
;
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;
}
相关文章推荐
- POJ 1730 Perfect Pth Powers (分解素因子)
- POJ 1730 Perfect Pth Powers 解题报告(因式分解)
- zoj 2124 || poj 1730 Perfect Pth Powers
- POJ 1730 Perfect Pth Powers (分解素因子)
- poj 1730 Perfect Pth Powers
- poj 1730 Perfect Pth Powers
- poj - 1730 - Perfect Pth Powers - (分解质因数)
- POJ-1730 Perfect Pth Powers 解题报告(数论) 最大开方数
- POJ-1730-Perfect Pth Powers
- poj 1730 Perfect Pth Powers
- POJ 1730 Perfect Pth Powers (枚举||分解质因子)
- POJ 1730 Perfect Pth Powers
- POJ-1730 Perfect Pth Powers(思维:大数分解素因子)
- [TLE] POJ 1730 Perfect Pth Powers
- POJ_1730_Perfect Pth Powers
- poj 1730 Perfect Pth Powers
- poj1730 素数 Perfect Pth Powers
- poj1730——Perfect Pth Powers
- (Relax 1.12)POJ 1730 Perfect Pth Powers(在x=b^p的情况下,求最大的p)
- POJ 1730 Perfect Pth Powers(暴力枚举)