poj 2109 Power of Cryptography

简介:

今天懒了,这道题直接去看了大家的帖子,三行搞定。。。

题目大意: K ^ N = P, 给N 和 P, 求K。数据规模 :1<=n<= 200, 1<=p<10101 and there exists an integer k, 1<=k<=109 。


看到1<=p<10101 我就去想大数操作了,后来发现原来double完全可以放。

类型          长度 (bit)           有效数字          绝对值范围
float             32                      6~7                  10^(-37) ~ 10^38
double          64                     15~16               10^(-307) ~10^308
long double   128                   18~19                10^(-4931) ~ 10 ^ 4932

接下来就是直接用pow(n,1/p)就可以了,这个也可以开方。


一般思路:二分+高精度算法

 

但是本题还有一个更加巧妙的办法去处理:

首先需要明确:double类型虽然能表示10^(-307)   ~   10^308, (远大于题意的1<=p<10101这个范围),但只能精确前16位,因此必须慎用!

那么为了避免double对输入的数在运算过程中进行精确,那么我们必须让double的运算第一步就得到一个int(即小数点尾数全为0),这个不难理解。

 

然后根据题意,是求指数k,一般人自然想到利用 对数log,即k=lognp。但是不要忘记使用对数最大的问题就是没有lognp函数,只有log()函数(底数为e),为此要计算lognp就必须使用换底公式lognp=log(p)/log(n),即k= log(p)/log(n),由于这使得double的运算变为了3次,而且执行除法前的两次对数运算log的结果未必都是int,很显然k是一个被精确了的double

 

很多人到这里就放弃了使用double,转换方向到正常思路(二分+高精度算法),但是不要忘记求指数k除了使用对数log,还能使用指数的倒数开n次方,这时就可以用pow函数了

k=pow(p,1.0/n),double的运算一步到位,k自然也是一个int


#include<iostream>
#include<math.h>

using namespace std;

int main(void)
{
	double n,p;

	while(cin>>n>>p)
		cout<<pow(p,1.0/n)<<endl;  //指数的倒数就是开n次方

	return 0;
}


还看到一种二分法,由p最大值最小值的中间值开始猜k,通过比较pow(k,n)与p的大小来进一步精确k,直到找到。

[cpp]  view plain copy
  1. #include<stdio.h>  
  2. #include<math.h>  
  3. #define eps 0.0000000001  
  4. void init(), work();  
  5. double n, m, k;  
  6. int main()  
  7. {  
  8.     init();  
  9.     return 0;  
  10. }  
  11. void init()  
  12. {  
  13.     while(scanf("%lf %lf", &n, &m) != EOF)  
  14.         work();  
  15. }  
  16. void work()  
  17. {  
  18.     long long left, right, mid;  
  19.     left = 0;  
  20.     right = 1000000002;  
  21.     while(left + eps < right){  
  22.         mid = (left + right) / 2;  
  23.         if(pow(mid, n) - m > 0)  
  24.             right = mid;  
  25.         else  
  26.             if(pow(mid, n) - m < 0)  
  27.                 left = mid;  
  28.             else{  
  29.                 printf("%.0ld\n", mid) ;  
  30.                 break;  
  31.             }  
  32.     }  
  33. }  




相关文章
|
4月前
750mA Linear Charger with Power Path Management
​ 一、General Description YHM2711 is a highly integrated, single-cell Li-ion battery charger with system power path management for space-limited portable applications. The full charger function features Trickle-charge, constant current fast charge and constant voltage regulation, charge termination,
24 0
LeetCode 342. Power of Four
给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。
56 0
LeetCode 342. Power of Four
[POJ 1236] Network of Schools | Tarjan缩点
Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”).
115 0
[POJ 1236] Network of Schools | Tarjan缩点
|
Java C++
LeetCode之Power of Two
LeetCode之Power of Two
91 0
POJ 1018 Communication System
Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28182   Accepted: 10049 Description We have received an order from Pizoor Communications Inc.
1350 0
[LeetCode] Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it
1450 0
|
算法 数据建模
【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点
题目链接:http://poj.org/problem?id=1236 题意:给定一个表示n所学校网络连通关系的有向图。现要通过网络分发软件,规则是:若顶点u,v存在通路,发给u,则v可以通过网络从u接收到。
1150 0
POJ-1004-Finanical Management
Description Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough.
866 0