这个有2种方法。

一种是通过枚举p的值(p的范围是从1-32),这样不会超时,再就是注意下精度用1e-8就可以了,还有要注意负数的处理……

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
int fun(double n)
{
int p=,f=;
if(n<)
{
p=;
f=;
n=-n;
}
for(;p>=;p-=f)
{
double a=pow(n,1.0/p);
long t1=a;
long t2=a+;
if(a-t1<=1e-||t2-a<=1e-)
break;
}
return p;
}
int main()
{
int x,p;
while(cin>>x&&x)
{
p=fun(x);
cout<<p<<endl;
}
return ;
}

第二种方法就是分解素数因子,求出因子的指数的最大公约数就是答案了,但是我这测试没错,提交超时……

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
int prime[],a[];
void init()
{
int i,j,k=;
for(i=;i<=;i++)
{
if(!a[i])
{
prime[k++]=i;
for(j=i+i;j<=;j+=i)
a[j]=;
}
}
}
int gcd(int a,int b)
{
int c;
if(a<b) swap(a,b);
while(b)
{
c=a;
a=b;
b=c%b;
}
return a;
}
int main()
{
int n,i,j;
bool flag;
init();
while(scanf("%d",&n),n)
{
flag=;
if(n<)
{
flag=;
n=-n;
}
vector<int> key;
for(i=;prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==)
{
int c=;
for(;n%prime[i]==;n/=prime[i],c++);
key.push_back(c);
}
}
if(n>) key.push_back();
int sum=key[];
for(i=;i<key.size();i++)
sum=gcd(sum,key[i]);
if(flag)
{
while(sum%==)
sum/=;
}
printf("%d\n",sum);
}
return ;
}

poj 1730 Perfect Pth Powers的更多相关文章

  1. POJ 1730 Perfect Pth Powers(暴力枚举)

    题目链接: https://cn.vjudge.net/problem/POJ-1730 题目描述: We say that x is a perfect square if, for some in ...

  2. POJ 1730 Perfect Pth Powers(唯一分解定理)

    http://poj.org/problem?id=1730 题意:给出一个n,a=b^p,求出最大p值. 思路: 首先利用唯一分解定理,把n写成若干个素数相乘的形势.接下来对于每个指数求最大公约数, ...

  3. UVA 10622 - Perfect P-th Powers(数论)

    UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...

  4. Perfect Pth Powers poj1730

    Perfect Pth Powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16383   Accepted: 37 ...

  5. [暑假集训--数论]poj1730 Perfect Pth Powers

    We say that x is a perfect square if, for some integer b, x = b 2. Similarly, x is a perfect cube if ...

  6. Kattis之旅——Perfect Pth Powers

    We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, ...

  7. UVa 10622 (gcd 分解质因数) Perfect P-th Powers

    题意: 对于32位有符号整数x,将其写成x = bp的形式,求p可能的最大值. 分析: 将x分解质因数,然后求所有指数的gcd即可. 对于负数还要再处理一下,负数求得的p必须是奇数才行. #inclu ...

  8. uva10622 Perfect P-th Powers

    留坑(p.343) 完全不知道哪里有问题qwq 从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊 #include<cstdio> #include<cstring&g ...

  9. UVA 10622 Perfect P-th Powers

    https://vjudge.net/problem/UVA-10622 将n分解质因数,指数的gcd就是答案 如果n是负数,将答案除2至奇数 原理:(a*b)^p=a^p*b^p #include& ...

随机推荐

  1. 《搭建DNS负载均衡服务》RHEL6

    搭建DNS负载均衡环境: 1.至少三台的linux虚拟机,一台主的DNS服务器,1台副的(可以N台),1台测试机. 负载均衡有很多种,apache那样的是为了缓解人们访问网站时给服务器造成太大的压力, ...

  2. Android String format 通过value 下的string.xml 文件

    <string name="format_coordinate" formatted="false">%s %d° %d\' %d\" % ...

  3. thymeleaf 局部变量、属性优先级、注释

    九.局部变量(local variable) 之前在th:each中遇到过局部变量 <tr th:each="prod : ${prods}"> ... </tr ...

  4. 如何修改 Discuz 门户文章页默认视频大小

    在 Discuz 系统中,论坛插入 Flash 等可以输入自定义的尺寸,但是门户文章页不可以修改.经过一番研究,找到了修改门户文章页默认视频大小的方法如下,希望对你有用:找到:/source/func ...

  5. 冒泡排序(Bubble Sort)

    常见的排序算法有Bubble Sort.Merge Sort.Quick Sort 等,所有排序算的基本法思想都是把一个无限大的数据规模通过算法一步步缩小,指导最后完成排序. 这里分享一下Buuble ...

  6. 【转】Linux Framebuffer

    全面的framebuffer详解 一.FrameBuffer的原理 FrameBuffer 是出现在 2.2.xx 内核当中的一种驱动程序接口. Linux是工作在保护模式下,所以用户态进程是无法象D ...

  7. Microsoft Visual Studio Ultimate 2015 Preview使用笔记

    1.内存好象存在泄露问题

  8. 飞行器的Pitch Yaw Roll概念图解

    前进方向为Z轴 Pitch 升降 绕X轴  对应常见Φ(phi)角: Yaw 偏航 绕Y轴 对应常见θ(theta)角: Roll 翻滚 绕Z轴 对应常见φ(psi)角: 对应表: 参考网址NASA: ...

  9. posix 消息队列

    注意 在涉及到posix消息的函数时, gcc 编译时要加-lrt参数, 如 gcc -lrt unpipc.c mqpack.c send.c -o send gcc -lrt unpipc.c m ...

  10. github客户端创建仓库

    1.在github上创建立自己项目仓库 登录后,在github首页,点击页面右下角“New Repository” 填写项目信息: project name: project description  ...