(转) 二分法+高精度——Poj 2109 Power of Cryptography(double型开n次方的方法通过的原因)
来源:互联网 发布:视频转换软件 编辑:程序博客网 时间:2024/06/06 02:46
Description
This problem involves the efficient computation of integer roots of numbers.
Given an integer n>=1 and an integer p>= 1 you have to write a program that determines the n th positive root of p. In this problem, given such integers n and p, p will always be of the form k to the nth. power, for an integer k (this integer is what your program must find).
Input
Output
Sample Input
2 163 277 4357186184021382204544
Sample Output
431234
如果你只是想知道为什么double型开n次方法通过的原因,请跳过此部分。
题目大意:
求一个整数k,使得k满足kn=p。
思路:
由于p的值很大,超出了基本数据类型所表示的范围,所以采用大数乘法来计算kn,当kn=p时,得出结果(不知道能不能大数开n次方…)。那k值应该如何取呢?根据n和p的关系是可以确定出k的位数的,例如:n=7,p=4357186184021382204544,p的位数为22,用22/7的结果向上取整,得到4,即为k的位数,也就是说k的取值范围是1000~9999。在这个范围内进行二分查找,就可以找到满足条件的k值。(这狗屎题目中说“there exists an integer k, 1<=k<=109 , such that kn = p.”,其实…逗你玩!)
double型开n次方的方法通过的原因
下面这段程序也是可以通过此题的。
首先,题目中的数据强度并不弱,这一点确实如题目中所说:“For all such pairs 1<=n<= 200, 1<=p<10101,所以,double型是不能精确地表示出所给数据,但是却能表示出一个近似值。
当向double型变量中存入
4357186184021382204544
然后再输出,得到的是
4357186184021382000000
后六位的值变为了0,这一点和int型变量是有很大区别的。也就是说当存入double型变量的值超出了它的精度表示范围时,将低位的数据截断。(关于浮点数在计算机中的表示方法,百度吧…讲的蛮清楚的。)
在本题中,如果测试数据为:
7 4357186184021382204544
实际上所处理数据是:
7 4357186184021382000000
拿4357186184021382000000开7次方的结果自然就是1234。
为什么不是1233或者1235呢?
12337=4332529576639313702577
12347=4357186184021382204544
12357=4381962969567270546875
可以看出在double型所能表示的精度范围内,它们三个值已经不同了。
所以,此题中的测试数据也都是类似于上述情况,所以才能使用double型开n次方的方法。
原博主地址:点击打开链接
- (转) 二分法+高精度——Poj 2109 Power of Cryptography(double型开n次方的方法通过的原因)
- 二分法+高精度——Poj 2109 Power of Cryptography(double型开n次方的方法通过的原因)
- POJ:2109-Power of Cryptography(关于double的误差)
- poj 2109 Power of Cryptography(用double避开高精度)
- POJ 2109 Power of Cryptography (用double避免高精度)
- poj 2109 Power of Cryptography(pow() 和 double 的妙用)
- POJ 2109 Power of Cryptography double的运算技巧问题
- poj2109 Power of Cryptography —— 技巧性(高精度+二分的方法还没写)
- Power of Cryptography(double可以表示到10的-307次方到10的308次方)
- POJ 2109 Power of Cryptography(我的水题之路——k^n=p)
- POJ 2109-Power of Cryptography(double乘方)
- POJ 2109 Power of Cryptography【高精度+二分 Or double水过~~】
- POJ2109 Power of Cryptography (高精度+二分) (可以double水過去)
- Power of Cryptography(高精度)
- POJ 2109 Power of Cryptography 二分+高精度 【未AC】
- UVA 113 --- Power of Cryptography 【double避开高精度】
- UVA (POJ 2109) Power of Cryptography
- POJ 2109 Power of Cryptography(水~)
- 屏幕适配
- 关于List<T>.Add()方法添加的所有项都一样的个人解释,解决方法
- iOS像素和点的转换
- 完成端口IOCP详解
- LeetCode-53-Maximum Subarray(最大和子串)
- (转) 二分法+高精度——Poj 2109 Power of Cryptography(double型开n次方的方法通过的原因)
- java 解析接口返回xml类型值
- css3 animation和background-position制作动画效果(animation steps)
- Ubuntu 重启apache2未成功,AH00558错误
- 查看linux版本
- python报错:module has no Attribute'***'
- PAT编程题:A除以B (20)
- 文件操作
- FixedCapacityStackOfString( 1.3.2.1 P82)