# An explanation of fast power and matrix fast Power algorithm for integer

Source: Internet
Author: User

In case the link fails to lose such a good blog, so copy one to prevent loss.

Explanation of Matrix Fast Power Foundation

1. Basic Knowledge Reserve Chapter

The related operations of matrices are then learned in linear algebra.
1.1 Definition of the matrix:
? N-order matrices (n-order matrix): a matrix with the same number of rows m as the number of columns N, as shown in a 4-4 Phalanx: *

Row matrix (Row vector): Only one row of the matrix is a row matrix:
?

Column matrix (column vector): Only one column of the matrix is a column matrix:
?

Matrix A and matrix B, matrix A and the number of columns and Matrix B are the same, then the matrix A, B is the same type matrix.

?

Unit matrix: In the multiplication of matrices, there is a matrix that plays a special role, like 1 of the Multiplication of numbers, which is called the unit matrix. It is a square, with 1 elements on the diagonal from the upper-left corner to the lower-right corner (called the main diagonal). All of them are 0. As shown is a 3-order unit matrix.
···

1.2 Related operations of the matrix:

Matrix multiplication

2. Matrix Fast Power Introduction Chapter

Integer Fast power:
To elicit the fast power of the Matrix, and to illustrate the benefits of a fast power algorithm, we can first find the power of integers.
If you want to calculate x^8 now: Then xx xx x x x x according to theusual idea, one by one, multiply the multiplication operation 7 times.
(X
x)(xx)(xx)(xx)
In this method, the multiplication is x^2 first, then the x^2 is performed three times, so the multiplication is performed 4 times. have been less than seven times. So in order to quickly calculate the power of an integer, it will consider this combination of thought.
Now consider how the calculations should be made faster. Next, calculate the integer fast power. For example: x^19 the second party.
The binary of 19 is: 1 0 0 1 1.
by (X^m)(x^n) = x^ (m+n)
Then x^19 = (x^16)
(x^2) * (x^1)
So how to solve the fast power. Take a look at the following code:
Solves the value of the x^n.
Integer fast power, calculate x^n

``Int Quickpow (int X, int N) {int res = x; int ans = 1; while (N) {if (N&< Span class= "DV" >1) {ans = ans * res;} res = res*res; N = N>>1;} return ans;}           ``

So let's see if the following code is right:
For x^19:
The 19 binary is: 1 0 0 1 1
Initial:

``1; res = x;``

Then 10011 the last one is 1, so it is odd.

`` ans = res*ans = x; res = res*res = x^2;``

Then move right one bit, 1 0 0 1
Then 1001 the last one is 1, so it's odd.

``    ans = res*ans = x*(x^2) = x^3     res = res*res = x^2*x^2 = x^4``

Then move right one bit, 1 0 0
The last digit is 0, so the current number is even.

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

Then move right one bit, 1 0
The last one is 0, and the current number is even.

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

Then move right one, 1.
The last one is 1, the current number is odd.

``    ans = ans*res = (x^3)*（x^16） = x^19 res = res*res = x^32``

You can see that res = X^m,m is always relative to the weights at the binary position. When bits is 0 o'clock, we only let Resres make the power exponent 2. Corresponds to the next bits weight, when bits is 1 o'clock, ans = ans*res. is multiplied by the X power of the multiplication.
2. Matrix Fast Power Algorithm Chapter

Looking at the fast power of an integer number, we now formally introduce the Matrix fast power algorithm. If there is now a n*n square a. The so-called Square is the number of rows and the number of columns equal to the matrix, first give a number m, let the calculation matrix A's M power, a^m. It is not necessary to delve into the meaning of this matrix. The above code can be converted to.

The above is just a simple calculation of the power of the Matrix, we will feel very abstract, because the above matrix does not have a specific meaning,
Now we illustrate the significance of the fast power of matrix in practical application:
Take the most common Fibonacci sequence as an example: it is well known that the Fibonacci number recursion formula is:
F[n] = f[n-1] + f[n-2]. By F=0,f=1, you can recursively push back all the numbers.
In the past, we would often use a for loop, which is the most straightforward algorithm.
POJ 3070, let the Fibonacci sequence, its n is up to 1 billion.
Limitations of direct recursion:
(1) The Fibonacci number n up to 1 billion that you're pushing. The test time is only 1 seconds, and the for loop causes a time-out with recursion formula recursion.
(2) It is impossible to make random access to a table, first to get the Fibonacci sequence to 1 billion, and then to go for random access. The topic does not give so much memory, the array is not open to 1 billion.
So it can be written with a fast power to the matrix.
Observe f[n] = f[n-1]+f[n-2] The nth phase is recursive by the n-1 and the n-2.
In the same vein, item n+1 is recursive by items N and n-1.
It can therefore be represented by a matrix:

Then, know f[n-1], f[n-2] then multiply the left matrix, you can get the equality matrix, the first position
That is the requirement of f[n].

An explanation of fast power and matrix fast Power algorithm for integer

Related Keywords:

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.