Reprinted please indicate the source: Thank you http://user.qzone.qq.com/289065406/blog/1299228474
Tip:
General idea: Binary + High PrecisionAlgorithm
However, there is a more clever way to deal with this question:
First, make it clear that although the double type can represent 10 ^ (-307 )~ 10 ^ 308, (far greater than the range of 1 <= P <10101), but only the first 16 digits can be accurate. Therefore, use it with caution!
To avoid the accuracy of the input number in the double operation, we must obtain an int (that is, the end number of the decimal point is all 0) in the first step of the double operation.
Then, according to the question, I want to calculate the INDEX K. Most people naturally want to use it.Log, that is, K = lognp.But do not forget that the biggest problem with using the logarithm is that noLognpFunction, only the log () function (the base number is E ).Lognp must use the bottoming formula lognp = Log (P)/log (N), That isK = Log (P)/log (N ),This makes the double operation three times, and the log result of the two logarithm operations before the division operation may not be all Int. Obviously, K is a precise double.
Many people give up using double here and convert the direction to the normal idea (Binary + high-precision algorithm). However, do not forget to use the logarithm log in addition to the index K, you can also use the reciprocal of the index to open the nth power. Then you can use the POW function.
K = POW (p,1.0/N), the double operation is in place in one step, and K is naturally an int
// Memory time // 280 K 0 Ms # 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; // the reciprocal of the index is to open the n return 0 ;}
Haha, don't be surprised!ProgramThis is the difference between "skill" and "algorithm ".
Using Double to avoid high-precision algorithms can be said to be the biggest bug o
If you are interested, you can also challenge the second point + high precision.