Fast power and fast power modulo

Source: Internet
Author: User

First of all, the purpose of the fast power is to do a quick exponentiation, assuming we ask a^b, according to the naïve algorithm is to connect A to B, so that the time complexity is O (b) is also the O (n) level, fast power to do O (Logn), a lot faster. It has the following principles:

Suppose we ask for a^b, then in fact B is can be split into binary, the second number I bit the right to 2^ (i-1), such as when b==11,a^11=a^ (2^0+2^1+2^3)

11 of the binary is 1011,11 = 23x1 + 22x0 + 21x1 + 2ox1, so we will A11 converted to calculate a^ (2^0) *a^ (2^1) *a^ (2^3), see much faster than the original count 11 times, now count three times, but these three looks bad To look like .... No hurry, there will be a detailed explanation below.
because it is binary, it is natural to think of this powerful tool with bit operations: the & and >>,& operations are typically used for binary bitwise operations, such as the result of a number & 1, which is the lowest of binary binary. It can also be judged that the odd and even x&1==0 are even, x&1==1 is odd. >> Operations Relatively simple, the binary system to remove the last one, not much to say, first put the code to explain.
intPOWW (intAintb) {    intans=1,Base=A;  while(b!=0){        if(b&1!=0) ans*=Base; Base*=Base; b>>=1; }    returnans;}

Code is very short, rote is also feasible, but it is better to understand, in fact, also very good understanding, take b==11 as an example, b=>1011, binary from right to left, but the order is a^ (2^0) * a^ (2^1) * a^ (2^3), is from left to right. We constantly make base*=base the purpose of the ride, so that we can contribute to ans at any time.

One to understand base*=base this step, see:: base*base==base^2, Next multiply, is base^2*base^2==base^4, and then the same base^4 * Base4 = base^8,,,,, seeing? is not done base-->base^2-->base^4-->base^8-->base^16-->base^32 ... index is 2^i ah, look at the above example, A11 = a^ (2^0) * a^ (2^1) * a^ (2^3), these three items are not perfect solved,, well, fast power is like this.

By the way, because the exponential function is the function of explosion growth, so it is likely to explode the range of int, according to test instructions decide whether to use a long long AH or unsigned int ah or mod a number ah himself look at the office.

Also, the only difference between the fast-power of matrices is the multiplication in the matrix, the substitution of a function, the thought of a hair.

Fast power to take the mold: (Million change from which!!!) )

intPowerintAintBintMoD) {    intAns =1,Base= a%MoD;  while(b!=0)    {        if(B &1!=0) ans= (ans*Base)%MoD; Base=(Base*Base)%MoD; b>>=1; }    returnRes;}

Fast power and fast power modulo

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.