Quick power understanding for beginners

Source: Internet
Author: User

Quick power understanding for beginners

My blog has been suspended for almost three months. Although I have been learning algorithms for some time, I have never written a blog. Today, I saw the quick power of the morning. I suddenly wanted to write a blog and increase my memory! This blog briefly introduces the reason for the remainder in the algorithm.

 

 1The concept of "quick power" is not detailed. When we want to obtain the remainder of the B power pair c of a, we will directly think of using this algorithm:

int ans = 1;  for( i  = 1; i <= b; i++)  {    ans = ans * a;  }  ans %= c;

The time complexity of this algorithm is reflected in the for loop, which is O (B). This algorithm has obvious problems. If a and B are too large, it will easily overflow. Therefore, we need to use discrete math knowledge (I have never learned this knowledge point, du Niang's ^_^)

 

 

  Theorem 1:

  

 

 

2The above formula can be used to introduce:

Consider a as a * 1, so the formula above can be obtained from Theorem 1. The improved version is obtained as follows:

Int ans = 1; a = a % c; // Add this sentence for (int I = 1; I <= B; I ++) {ans = (ans * a) % c; // the remainder is retrieved here.} ans = ans % c;

This algorithm has not been improved in terms of time complexity. It is still O (B), but it is much better. However, it is likely to time out when c is too large, we have introduced the following fast power algorithms.

 

3The quick power algorithm relies on the following obvious formulas. I won't prove it, so it's easy to understand.

  

4The improved version is obtained as follows:

Int ans = 1; a = a % c; if (B % 2 = 1) ans = (ans * a) mod c; // if it is an odd number, to take one more step, calculate k = (a * a) % c in ans in advance; // We take a2 instead of a for (int I = 1; I <= B/2; I ++) {ans = (ans * k) % c;} ans = ans % c;

 

5We can see that we have changed the time complexity to O (B/2). Of course, this is a permanent cure. But we can see that when we set k = (a * a) mod c,

The status has changed. The final result we requested is (k) ^ B/2 mod c instead of the original a ^ B mod c, therefore, we find that this process can be iterated.

(Iteration is to take the value of this calculation as the initial value of the next loop, so this algorithm can be simplified ))

Of course, there will be one more a mod c for the odd number case, so to complete the iteration, when B is an odd number, we use ans = (ans * a) % c; to make up for this extra item,

The remaining parts can be iterated. After iteration of the formula above, when B = 0, all the factors are multiplied and the algorithm ends. Therefore, it can be completed in O (log B) time.

So we have the final algorithm: quick power algorithm.

(To be honest, I am a little confused about this simplification. I can complete it in O (log B) Time. I just pretend to understand it, and I will understand it later ))

 6

 int ans = 1;   a = a % c;  while(b>0)  {       if(b % 2 == 1)     ans = (ans * a) % c;    b = b/2;     a = (a * a) % c;   } 

 

 

 

7Structure the code above, that is, write it as a function:

int PowerMod(int a, int b, int c)  {     int ans = 1;    a = a % c;    while(b>0)     {         if(b % 2 = = 1)       ans = (ans * a) % c;       b = b/2;       a = (a * a) % c;       }  return ans;  }

 

  

 

 

This is the final optimization code. The above content is plagiarized, but I wrote it again. If you cannot understand what I wrote, click the following URL:

Http://wenku.baidu.com/link? Url = PdtuRuYTZSIfAC3TZCiHSx3fk7cEn3yuZBiYwbx-b7h_TOxNyOQtNOaUepEmxw56jhnPePqAdebOH6QL-pvmhfcyydzghyrnor_oztpxwo

You can also think about the derivation of a fast power algorithm from another perspective. I will not introduce it.

Related Article

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.