Matrix fast power -QuickPow

Rapid introduction of matrix power :


 

  1. The fast integer powers:

  In order to elicit rapid power matrix, and a description of the benefits of fast power algorithm, we can first exponentiation integers.
If you now want to calculate X ^ 8: it is the X- the X- the X- the X- the X- the X- the X- the X-follow unusual ideas, take one by one to the above, the multiplication seven times.
(X-
X-) (X- X-) (X- X-) (X- X-)

This method for finding, obtained by multiplying the first X ^ 2, then X ^ 2 then perform three multiplications, so to calculate, multiplication is performed four times. We have less than seven times. So in order to quickly count integer power, we will consider this combination of ideas.
Now consider how points should make computing faster. Next, calculate the fast integer power. For example: X ^ 19 th.
Binary is 19: 10011.
A (^ X-m) (n-X-^) = X-^ (n-m +)
is ^. 19 = X-(^ X-16)
(X-^ 2) * (X-^. 1)

then the fast power how to solve it. Consider the following code:
Solving value of X ^ N.
/// fast integer powers, calculating x ^ N

. 1  int QuickPow ( int x, int N) // incoming index and base x N
 2  {
 . 3      int RES = x;
 . 4      int ANS = . 1 ;
 . 5      the while (N)
 . 6      {
 . 7          IF (N & . 1 ) is // N odd
 . 8          {
 . 9              ANS = ANS * RES;
 10          }
 . 11          RES = RES * RES;
 12 is          N = N >> . 1 ; N // right shift
 13 is      }
 14      return ANS;
    }

 So let's look at the following code right in the end:
For X ^ 19 terms:
binary 19 is: 10011
Initial:

So let's look at the following code right in the end:
For X ^ 19 terms:
binary 19 is: 10011
Initial:

years = 1 ; res = x;

 

10011 1 is the last one, it is odd.

years = res * years = x;
nothing = nothing nothing * = x ^ 2 ;

 

Then right one, 1001
Ze 1001 last bit is 1, it is odd

 
years = res = x * years * (x ^ 2 ) = x ^ 3
 
res = res*res = x^2*x^2 = x^4

 

Then right one, 100

The last one is 0, the current number is an even number.

 res = res*res = x^4*x^4 = x^8

 

Then right one, 10
the last bit is 0, the current number is even.

res = res*res =x^8*x^8= x^16

 

Then right one, one
last bit is 1, the current number is odd

year = years * res = (x ^ 3 ) * (x ^ 16 ) = x ^ 19
 
nothing = nothing = nothing * x ^ 32

 

2. Matrix Quick Power Algorithm articles

Quick thinking and integer arithmetic algorithm similar powers:

. 1  struct Mat
 2  {
 . 3      LL m [ 101 ] [ 101 ];
 . 4 }; // memory structure 
. 5 Mat A, e; // A is a matrix input, e is the output matrix 
. 6  Mat adder Mul (X Mat, Mat Y)
 . 7  {
 . 8      Mat C;
 . 9      for ( int I = . 1 ; I <= n-; ++ I) {
 10          for ( int J = . 1 ; J <= n-; ++ J) {
 . 11              cm & lt [I] [ J] = 0 ;
 12 is          }
 13 is      }
14     for(int i=1;i<=n;++i){
15         for(int j=1;j<=n;++j){
16             for(int k=1;k<=n;++k){
17                 c.m[i][j] = c.m[i][j]%mod + x.m[i][k]*y.m[k][j]%mod;
18             }
19         }
20     }
21     return c;
22 }
23 Mat pow(Mat x,LL y)//矩阵快速幂
24 {
25     Mat ans = e;
26     while(y){
27         if(y&1) ans = Mul(ans,x);
28         x = Mul(x,x);
29         y>>=1;
30     }
31     return ans;
View Code

 

Guess you like

Origin www.cnblogs.com/swijtu-lawrenceD-1425123490/p/Algorithm-01-MatrixQuickPow.html