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 ...
随机推荐
- 设计模式:visitor
拜访者模式(visitor)适用于对复杂结构体进行解析的场景. 所谓复杂结构体,是指包含多个子元素的对象,比如集合,树,图,或者组合对象--.结构体中的每个元素,包括结构体本身实现接口: Elemen ...
- 实验5 IIC通讯与AD/接DA接口
1.利用单片机控制PCF8591的AD转换,控制AD0和AD1电位器,在数码光上显示DA转换的值. 2.利用单片机控制PCF8591的DA转换,让发光二极管D1由暗到亮变化,整个过程时间差不多2s左右 ...
- asp.net core 健康检查
asp.net core 健康检查 ASP.NET Core 2.2 开始,提供了健康检查中间件和库,用来报告应用基础结构组件的运行状况.官方文档在此 运行状况检查由应用程序作为 HTTP 终结点公开 ...
- jQuery---ajax---error函数及其参数详解
使用jquery的ajax方法向服务器发送请求的时候,常常需要使用到error函数进行错误信息的处理,本文详细说明了ajax中error函数和函数中各个参数的用法. 一般error函数返回的参数有三个 ...
- Entity Framework Core 生成跟踪列-阴影属性
摘自:https://www.cnblogs.com/tdfblog/p/entity-framework-core-generate-tracking-columns.html Ef Core 官方 ...
- Java-String类型的参数传递问题
刚才看见一个兄弟在为Java的String传值/传引用问题困惑,翻箱倒柜找到了这篇我很久以前写的文章,发在这里,希望能对迷惑的朋友有些帮助. 提要:本文从实现原理的角度上阐述和剖析了:在Java语言中 ...
- 使用Squid部署代理服务
Squid是Linux系统中最为流行的一款高性能代理服务软件,通常用作Web网站的前置缓存服务,能够代替用户向网站服务器请求页面数据并进行缓存.简单来说,Squid服务程序会按照收到的用户请求向网站源 ...
- Git 本地操作
版权声明:数学是研究世界的本质,自然科学是研究上帝的意志,而计算机则是揣摩屌丝人类的意志 目录(?)[-] 命令 git config 增删改查 init clone add commit sta ...
- Tomcat 启动成功后报 INFO [Timer-0] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access:
Tomcat 启动成功后报 INFO [Timer-0] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceL ...
- centos7 安装cmake
安装cmake之前,记得升级gcc,请参考centos7 升级GCC版本到7.3.0 #shell 太简单,懒得解释 wget https://cmake.org/files/v3.11/cmake- ...