poj1730 - Perfect Pth Powers(完全平方数)(水题)
/*
以前做的一道水题,再做精度控制又出了错///。。。
*/
题目大意:
求最大完全平方数,一个数b(不超过int范围),n=b^p,使得给定n,p最大;
题目给你一个数n,求p ;
解题思路:
不需要遍历b,只需要从31开始遍历p就好了。这个方法涉及到我以前过分逃避的精度控制问题:本题会使用函数pow
而pow的返回值是double转化成int 会有损失,比如4的double表示可以使4.00000000或者3.99999999999;而我们使用
强制类型转换会截取整数部分结果就可能是3,因此只需要对强制转换的数据进行+0.1操作,可以解决这问题。
具体做法见代码。
代码:
#include<iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include<set>
#include <cstdio>
#include<iterator>
#include <cmath>
using namespace std; int main()
{
int a;
while (cin>>a&&a)
{
if(a>)
{
for (int i=; i>=; i--)
{
int x=(int )(pow(a*1.0,1.0/i)+0.1);
int y=(int )(pow(x*1.0,1.0*i)+0.1);
if (y==a)
{
cout<<i<<endl;
break;
}
}
}
else
{
a=-a;
for (int i=;i>=;i-=)
{
int x=(int )(pow(a*1.0,1.0/i)+0.1);
int y=(int )(pow(x*1.0,i*1.0)+0.1);
if (y==a)
{
cout<<i<<endl;
break;
}
}
}
}
}
ps:这题的n我不能设成long long ,具体原因暂时还不知道,日后更新。
poj1730 - Perfect Pth Powers(完全平方数)(水题)的更多相关文章
- [暑假集训--数论]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 ...
- Perfect Pth Powers poj1730
Perfect Pth Powers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16383 Accepted: 37 ...
- UVA 10622 - Perfect P-th Powers(数论)
UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...
- POJ 1730 Perfect Pth Powers(暴力枚举)
题目链接: https://cn.vjudge.net/problem/POJ-1730 题目描述: We say that x is a perfect square if, for some in ...
- 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 1730 Perfect Pth Powers
这个有2种方法. 一种是通过枚举p的值(p的范围是从1-32),这样不会超时,再就是注意下精度用1e-8就可以了,还有要注意负数的处理…… #include<iostream> #incl ...
- UVa 10622 (gcd 分解质因数) Perfect P-th Powers
题意: 对于32位有符号整数x,将其写成x = bp的形式,求p可能的最大值. 分析: 将x分解质因数,然后求所有指数的gcd即可. 对于负数还要再处理一下,负数求得的p必须是奇数才行. #inclu ...
- uva10622 Perfect P-th Powers
留坑(p.343) 完全不知道哪里有问题qwq 从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊 #include<cstdio> #include<cstring&g ...
- UVA 10622 Perfect P-th Powers
https://vjudge.net/problem/UVA-10622 将n分解质因数,指数的gcd就是答案 如果n是负数,将答案除2至奇数 原理:(a*b)^p=a^p*b^p #include& ...
随机推荐
- 6 Easy Steps to Learn Naive Bayes Algorithm (with code in Python)
6 Easy Steps to Learn Naive Bayes Algorithm (with code in Python) Introduction Here’s a situation yo ...
- git提示error setting certificate verify locations解决办法
先打开git bash窗口 执行命令: git config --system http.sslcainfo "C:\Program Files (x86)\git\bin\curl-ca- ...
- Windows系统环境下Solr之Java实战(二)配置从MySQL数据库批量导入索引
1.将D:\JavaWeb\Solr\solr-6.2.0\dist下面的solr-dataimporthandler-6.2.0.jar和solr-dataimporthandler-extras- ...
- 选择Memcached还是Redis?
两者相似之处 Memcached与Redis都属于内存内.键值数据存储方案.它们都从属于数据管理解决方案中的NoSQL家族,而且都基于同样的键值数据模型.双方都选择将全部数据保存在内存当中,这自然也就 ...
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
- (一)利用 mdb 调试获取 nvlist_t 中 nvpair_t(name/value) 对
服务器:192.168.2.122 root@2236:~# mdb -k> ::spaADDR STATE NAME ...
- ORB_SLAM2 源码阅读 ORB_SLAM2::Initializer::ComputeF21 (OpenCV 细节)
ORB_SLAM2 计算 F21 的代码是这样的. cv::Mat Initializer::ComputeF21(const vector<cv::Point2f> &vP1,c ...
- 2016.5.15——leetcode:Number of 1 Bits ,
leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...
- elasticsearch6.5集群环境搭建的一些坑
都说el配置很简单,确实比solr简单多了,不用手动配置一大堆,不过第一次配置也不轻松,因为马虎老是漏掉了许多地方 配置一个半小时才启动成功: 这里主要记录一下一些遇到的坑: 一 不能用root启动, ...
- bzoj 1607 Patting Heads 轻拍牛头
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1607 题解: 题目似乎出错,应为“同时拍打所有所持纸条上的数字能被此牛所持纸条上的数字整除 ...