POJ 1730 Perfect Pth Powers(暴力枚举)
题目链接:
https://cn.vjudge.net/problem/POJ-1730
题目描述:
We say that x is a perfect square if, for some integer b, x = b 2. Similarly, x is a perfect cube if, for some integer b, x = b 3. More generally, x is a perfect pth power if, for some integer b, x = b p. Given an integer x you are to determine the largest p such that x is a perfect p th power.
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
th power.
Sample Input
17
1073741824
25
0
Sample Output
1
30
2
/*
题意描述
输入x,计算并输出满足x=b的p次方中最大的p 解题思路
因为x的大小为2到2的31次方,故可采取枚举次方的方法,计算出b,再计算出b的p次方为temp,看temp和x是否相等即可。
另外需要注意的是x可能为负数,首先需要将负数变为正数,另外枚举的时候只能枚举奇数次方,因为偶数次方不能的到负数。
另外是用pow开方和乘方时注意精度问题,比如4.999999直接取整误差很大,加上0.1即可避免此类问题。
*/
#include<cstdio>
#include<cmath> int main()
{
int x;
while(scanf("%d",&x),x != ){
if(x > ){
for(int i=;i>=;i--){
int b=(int)(pow(x*1.0,1.0/(i*1.0)) + 0.1);
int temp=(int)(pow(b*1.0,i*1.0) + 0.1);
if(x == temp){
printf("%d\n",i);
break;
}
}
}
else{
x *= -;
for(int i=;i>=;i-=){
int b=(int)(pow(x*1.0,1.0/(i*1.0)) + 0.1);
int temp=(int)(pow(b*1.0,i*1.0) + 0.1);
if(x == temp){
printf("%d\n",i);
break;
}
}
}
}
return ;
}
POJ 1730 Perfect Pth Powers(暴力枚举)的更多相关文章
- POJ 1730 Perfect Pth Powers(唯一分解定理)
http://poj.org/problem?id=1730 题意:给出一个n,a=b^p,求出最大p值. 思路: 首先利用唯一分解定理,把n写成若干个素数相乘的形势.接下来对于每个指数求最大公约数, ...
- poj 1730 Perfect Pth Powers
这个有2种方法. 一种是通过枚举p的值(p的范围是从1-32),这样不会超时,再就是注意下精度用1e-8就可以了,还有要注意负数的处理…… #include<iostream> #incl ...
- UVA 10622 - Perfect P-th Powers(数论)
UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...
- Perfect Pth Powers poj1730
Perfect Pth Powers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16383 Accepted: 37 ...
- [暑假集训--数论]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 ...
- 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, ...
- POJ 3279 - Fliptile - [状压+暴力枚举]
题目链接:http://poj.org/problem?id=3279 Sample Input 4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 Sample Output 0 ...
- uva10622 Perfect P-th Powers
留坑(p.343) 完全不知道哪里有问题qwq 从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊 #include<cstdio> #include<cstring&g ...
- POJ 1380 Equipment Box (暴力枚举)
Equipment Box 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/B Description There is a la ...
随机推荐
- postgresql 主从 patroni
1 安装基础包 1.1 postgres yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_ ...
- springboot2 生产部署注意事项【持续更新】
注意事项1. 去除不需要的 jar 开发工具 jar :springs-boot-devtools2. 监控一定要做好权限制或者去除 控制 jar :spring-boot-starter-actua ...
- 【062新题】OCP 12c 062出现大量新题-15
choose one In your Oracle 12c database, you plan to execute the command: SQL> CREATE TABLESPACE t ...
- Python-实现图表绘制总结
Numpy是Python开源的数值计算扩展,可用来存储和处理大型矩阵,比Python自身数据结构要高效: matplotlib是一个Python的图像框架,使用其绘制出来的图形效果和MATLAB下绘制 ...
- cad2015卸载/安装失败/如何彻底卸载清除干净cad2015注册表和文件的方法
cad2015提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装cad2015失败提示cad2015安装未完成,某些产品无法安装,也有时候想重新安装cad2015 ...
- storm安装以及简单操作
storm的安装比较简单,下面以storm的单节点为例说明storm的安装步骤. 1.storm的下载 进入storm的官方网站http://storm.apache.org/,点击download按 ...
- Swift 里 Array (三) Inspecting an Array
判断是否为空 使用的是Collection协议里isEmpty的判断. public var isEmpty: Bool { return startIndex == endIndex } start ...
- ASP.NETCore学习记录(二) —— ASP.NET Core 中间件
ASP.NET Core 中间件 目录: 什么是中间件 ? IApplicationBuilder 使用 IApplicationBuilder 创建中间件 Run.Map 与 Use 方法 实战中间 ...
- c++中的const和volatile知识自我总结
学习了下c++中的const关键字,总结如下. 1.const限制一个变量不能修改其内容,如果强行修改的话,如下面代码这样子,编译就会报错,“表达式必须是可修改的左值”. int main() { c ...
- Linux 学习错误点整理之网络配置
本人是一名实习生,最近在学习Linux,在实操的过程中还是遇到了一些问题,所以想记录下来,供自己以后复习,也希望能给跟我一样的菜鸟的人带来一点点帮助. 我用的是VMware Workstation P ...