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 ...
随机推荐
- python3使用ip地址代理
第一种IP地址代理方式from urllib import request if __name__ == "__main__": # 访问网址 url = 'http://www. ...
- UITabBarItem title 和self.title设置不同的标题
self.navigationItem.title = @"my title"; //sets navigation bar title. self.tabBarItem.titl ...
- HyperServer 中的 SSL 支持
HyperServer 中的 SSL 支持 DLL 模式不需要 SSL 配置, 因为 web 服务器 (如 IIS) 将承担 ssl 配置和 ssl 证书的责任. 对于独立和服务模式, ssl 配置是 ...
- 行人检测(Pedestrian Detection)资源
一.论文 综述类的文章 [1]P.Dollar, C. Wojek,B. Schiele, et al. Pedestrian detection: an evaluation of the stat ...
- WPF自定义TabControl样式
WPF自定义TabControl,TabControl美化 XAML代码: <TabControl x:Class="SunCreate.Common.Controls.TabCont ...
- AngularJs的MVC模式
在AngularJs也有带有MVC模式.此时你完全可以把html的js中的Controller写到一个外部的js文件中. Ok,在ASP.NET MVC项目,打开Content目录,创建一个新文件夹A ...
- 【react】当react框架遇上百度地图
百度地图官方文档的使用指导是这样说的:在页面中引入<script type="text/javascript" src="http://api.map.baid ...
- [Leetcode]44.跳跃游戏Ⅰ&&45.跳跃游戏Ⅱ
跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出 ...
- POJ 2845
#include <iostream> #include <string> #include <algorithm> #define MAXN 350 using ...
- [LeetCode] 206. Reverse Linked List_Easy tag: Linked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...