Fast power-take modulus algorithm

Source: Internet
Author: User
Tags modulus

On the website has not found a detailed description and explanation of the fast power algorithm, here, I give a full explanation of the fast power algorithm, with the C language, the readers of different languages have to change bits, after all, read c more people ~

The so-called fast power, in fact, is the abbreviation of fast Power modulus, simply speaking, is to quickly find a power mode (residual ). In the process of programming, it is often necessary to find some large numbers for the remainder of a number, in order to get a faster, more computational scope of the algorithm, resulting in a fast power-taking modulus algorithm. [Some readers are reflected in the fast power of the part of the vague, so the article was modified here, made a more detailed supplement, to make more readers at a glance]

Let's start with a simple example: Beg = few.

Algorithm 1. First, directly to design this algorithm:

int ans = 1;

for (int i = 1;i<=b;i++)

{

Ans = ans * A;

}

Ans = ans% c;

The time complexity of this algorithm is reflected in the for Loop, O (b). There are obvious problems with this algorithm, and if A and B are too large, it can easily overflow.

So, let's take a look at the first improvement scenario: Before you talk about this scenario, there's a formula:

This formula should be learned in discrete mathematics or number theory, but here in order to facilitate everyone's reading, or to give proof:

Lemma 1:

The above formula is the lemma of the formula below, that is, the remainder of the product is equal to the remainder of the product.

After proving the above formula, we can first let a about C to take the remainder, this can greatly reduce the size of a,

So there is no need to think about the improvement:

Algorithm 2:

int ans = 1;

A = a% c; Add this sentence

for (int i = 1;i<=b;i++)

{

Ans = ans * A;

}

Ans = ans% c;

Smart readers should be able to think of, since a factor after the remainder of the multiplication and then take the rest of the remaining constant, then the new ans can also take redundancy, so get a better version of the improved.

Algorithm 3:

int ans = 1;

A = a% c; Add this sentence

for (int i = 1;i<=b;i++)

{

Ans = (ans * A)% c;//here to take more than one more time

}

Ans = ans% c;

This algorithm has no improvement in time complexity, still O (b), but it is much better, but in the case of C too large, it is still possible to time out, so we have introduced the following fast power algorithm.

The fast power algorithm relies on the following obvious formulas, which I will not prove.

With the above two formulas, we can draw the following conclusions:

1. If B is an even number, we can remember k = A2 mod C, then ask (k) B/2 mod c to do it.

2. If B is odd, we can also remember k = A2 mod C, then ask

((k) B/2 MoD cx a) mod C = ((k) B/2 MoD c * a) mod C is available.

Then we can get the following algorithm:

Algorithm 4:

int ans = 1;

A = a% c;

if (b%2==1)

Ans = (ans * a) mod C; If it's odd, take one more step, and you can calculate the ans in advance.

K = (a*a)% C; We take A2, not a .

for (int i = 1;i<=b/2;i++)

{

Ans = (ans * k)% c;

}

Ans = ans% c;

We can see that we turn the complexity of time into O (B/2). Of course, this kind of symptom is not the cure. But we can see that when we make k = (A * a) mod C, the state has changed and the final result we ask for is (k) B/2 mod C instead of the original AB MoD C, so we find that the process can be iterated. Of course, for odd cases there will be a more a mod C, so in order to complete the iteration, when B is odd, we pass

Ans = (ans * a)% C; To make up for the extra, the rest of the section can be iterated.

When the b=0 is down, all the factors are multiplied and the algorithm ends. You can then complete the time in O (log b). So, with the final algorithm: the Fast Power algorithm.

Algorithm 5: Fast Power algorithm

int ans = 1;

A = a% c;

while (b>0)

{

if (b% 2 = = 1)

Ans = (ans * a)% C;

b = B/2;

A = (A * a)% C;

}

Structure the above code, which is written 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;

}

The time complexity of this algorithm is O (logb), which can be passed in almost all program Design (contest) process, and is one of the most commonly used algorithms at present.

The following information is for reference only:

Extension: There is a derivation of the algorithm for fast power, which can also be thought from another angle.

= To solve this problem, we can also consider from the conversion of the binary:

Converts a 10-binary B into an 2-binary expression:

Well, actually,.

So

Note here that either the0, either 1, if an item, Then this is 1, which corresponds to the above algorithm process b is an even number for 1 corresponds to b is an odd case [do not reverse, the reader to analyze their own, you can contact 10 into 2 the way to go ] and we multiply from the. For each item's calculation, the result of the last item is calculated using the square of the result of the previous item. For the result of the request, the last ans don't have to multiply it. [because this value is 1 ", for 1 to multiply this item and then take the remainder. This algorithm and the above algorithm in essence is the same, the reader can self-analysis, here I said not much to say, I hope this article will help readers grasp the fast power algorithm of the knowledge point, of course, to real mastery, not much practice is not possible.

Fast power modulo algorithm

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.