Fast multiplication/fast power algorithm

Source: Internet
Author: User

The fast power algorithm can be said to be a kind of ACM competition is necessary, and is also a very basic type of algorithm, given that I have been learning more fragmented, so today with this post summed up

Fast multiplication usually has two types of applications: First, integer operation, calculation (A*B) mod C second, matrix fast multiplication

integer operations: (Fast multiplication, fast power)

Let's start with basic mathematical knowledge:

(a*b) MoD c = = ((a mod c) * (b mod c)) MoD C//This last mod C is to ensure that the result does not exceed C

For 2 binary, 2n can be used 1 followed by N Zero, for 8, the formula I+3*j = = N (wherein 0<= I <=2), for 16 binary, can be calculated by I+4*j==n (0 <= i <=3), the expression is 2i followed by J 0.

Next, let's describe the idea of fast multiplication as simple as possible:

A*b

The basic idea of fast multiplication is the combination of binary and multiply distribution laws, ( I can't help thinking that floating-point numbers do not meet the binding law, serious spit groove!!!) ╮ (╯-╰) ╭), for example, 13 = = (1101) 2, 4*13 equals to the (1101) 2, with the distribution law to expand to get 4*13 = = (1000+100+1), we are not difficult to observe, fast power can be passed Determines whether the current bit is 1 or 0, infers if a sum operation is required , and each time it moves to the next bit (bit), the ANS is manipulated , waiting for a sum. Since dividing by 2 is equivalent to the displacement operation, this can also be seen as the application of dichotomy, which divides B into two to reduce unnecessary operations, with the time complexity of log (n).

A^b

Fast power can be seen as a special case of fast multiplication, in the fast power, we no longer operate ans, because the meaning of B in a^b has changed from the multiplier to the exponent , but we can still write B binary, for example: At this time, we will 13 to 4^13,13= (1101) 2, binary 13 write open we get (1000+100+1), note that all binary here is exponential, the addition of the exponent means that the base is multiplied, so there is 4^13 = = ** *. Noting the twice-fold relationship between the indices, we can use a few variables to complete the algorithm. In this way, we will improve the algorithm to O (LOGN) by using an O (n) algorithm that would otherwise be used for loops.

As a rule, give a code implementation that is as concise and efficient as possible ( all the following int can be replaced with a long long)

First, the implementation of the fast multiplication is given:

1 //Fast multiplication2 intQmul (intAintb) {//Selectable long long based on data range3     intans=0;4      while(b) {5         if(b&1) Ans+=a;//the bitwise AND completion of the number of 1 judgment6b>>=1; a<<=1;//bit operation instead of */27     }8     returnans;9}

If fast multiplication is involved, you need to make some minor changes

The mathematical principles on which the changes are based, please refer to the mathematical knowledge shown in the red font

1 //Fast Multiply modulus2 intQmul_mod (intAintBintMoD) {3     intans=0;4      while(b) {5         if((B%=mod) &1) Ans+=a%=mod;//we need b%=mod and a%=mod here .6b>>=1; a<<=1;7     }8     returnAns%mod;//ans also needs to model MoD9}

Next is the implementation of the Fast power:

1 //Fast Power A^b2 intQpow (intAintb) {3     if(a==0)return 0;//This is a pit, the school game has been pit, many online implementations have not written this point4     intans=1;5      while(b) {6         if(b&1) Ans*=a;//and the difference between fast multiplication7b>>=1; a*=a;//difference, ibid.8     }9     returnans;Ten}

And a fast power that contains a modulo:

1 intQpow_mod (intAintBintMoD) {2     if(a==0)return 0;3     intans=1;4      while(b) {5         if(b&1) ans= (ans%mod) * (A%MOD);//If the data does not explode, this and the next sentence can only write a%mod, written two, is just in case6b>>=1; a= (a%mod) * (a%MoD);7     }8     returnans%MoD;9}

First update to this, there is time to update the matrix of the Strassen algorithm and matrix fast power, you see later ('? ') )

Fast multiplication/fast power 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.