POJ2109-Power of cryptography

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.