贪心 POJ 2109 Power of Cryptography

题目地址:http://poj.org/problem?id=2109

 1 /*
 2     题意:k ^ n = p,求k
 3     1. double + pow:因为double装得下p,k = pow (p, 1 / n);
 4     基础知识: 类型    长度 (bit)    有效数字    绝对值范围
 5             float    32             6~7         10^(-37) ~ 10^38
 6             double    64            15~16        10^(-307) ~ 10^308
 7        long double    128              18~19         10^(-4931) ~ 10 ^ 4932
 8     2. 二分查找:和1类似
 9     3. 取对数:n*ln(k)=ln(p)    ln(k)=ln(p)/n    k=exp(ln(p)/n)
10 */
11 #include <cstdio>
12 #include <iostream>
13 #include <algorithm>
14 #include <cstring>
15 #include <cmath>
16 #include <string>
17 #include <map>
18 #include <queue>
19 #include <vector>
20 using namespace std;
21 
22 const int MAXN = 1e6 + 10;
23 const int INF = 0x3f3f3f3f;
24 
25 int main(void)        //POJ 2109 Power of Cryptography
26 {
27     //freopen ("D.in", "r", stdin);
28     double n, p, k;
29 
30     while (~scanf ("%lf%lf", &n, &p))
31     {
32         printf ("%.0f
", pow (p, 1 / n));
33     }
34 
35     return 0;
36 }
37 
38 /*
39 #include <cstdio>
40 #include <cmath>
41 #include <algorithm>
42 #include <iostream>
43 using namespace std;
44 
45 void BinarySearch(int l, int r, double n, double p)
46 {
47     int mid;
48 
49     while (l <= r)
50     {
51         mid = l + (r - l) / 2;
52         double tmp = pow (mid, n);
53         if (tmp == p)
54         {
55             printf ("%d
", mid);    return ;
56         }
57         else if (tmp < p)    l = mid + 1;
58         else    r = mid - 1;
59     } 
60 }
61 
62 int main(void)
63 {
64     //freopen ("D.in", "r", stdin);
65 
66     double n, p;
67 
68     while (~scanf ("%lf%lf", &n, &p))
69     {
70         BinarySearch (1, 1e9, n, p);
71     }
72 
73     return 0;
74 }
75 */
76 
77 /*
78 #include <cstdio>
79 #include <cmath>
80 #include <algorithm>
81 #include <iostream>
82 using namespace std;
83 
84 int main(void)        //POJ 2109 Power of Cryptography
85 {
86     //freopen ("D.in", "r", stdin);
87 
88     double n, p;
89 
90     while (~scanf ("%lf%lf", &n, &p))
91     {
92         printf ("%.0f
", exp (log (p) / n));
93     }
94 
95     return 0;
96 }
97 */
编译人生,运行世界!
原文地址:https://www.cnblogs.com/Running-Time/p/4372429.html