Learning experience: "10 classic problems solved by matrix multiplication" from Matrix67

Source: Internet
Author: User

This article from: HTTP://WWW.MATRIX67.COM/BLOG/ARCHIVES/TAG/POJ
Daniel's blog study and study

Excerpt from the following sections:
Two important properties of matrix multiplication: first, matrix multiplicationnot satisfiedCommutative law; second, matrix multiplication satisfies the binding law
Classic Topic 1 given n points, M operations, constructs O (m+n) algorithm outputs the position of each point after M operation. Operations have pan, zoom, flip, and rotate
The operation here is done at the same time for all points. Where the flip is to rotate the axis of symmetry (two cases), the rotation is centered on the origin. If you simulate each point separately, the M operation takes a total of O (MN). By using matrix multiplication, you can combine all operations into a single matrix at O (m) time, and then multiply each point with that matrix to directly derive the final position of that point, which is a total time-consuming O (m+n). Assuming that the coordinates of a point at the beginning are x and Y, the following 5 matrices can be translated, rotated, flipped, and rotated respectively. By multiplying all the matrices corresponding to M operations in advance, multiply them by (x,y,1), you can get the final point position in one step.

Classic Topic 2 given matrix A, quickly calculate the result of A^n (n a multiplication), the output of each number is mod p.
Since matrix multiplication has a binding law, a^4 = A * A * a * = (a*a) * (a*a) = a^2 * a^2. We can conclude that when n is even, a^n = a^ (N/2) * a^ (N/2), and when n is odd, a^n = a^ (N/2) * a^ (N/2) * A (where N/2 rounding). This tells us that the calculation of a^n can also be used to calculate the power of the two-point fast method. For example, in order to calculate the value of a^25, we only need to calculate the value of a^12, A^6, a^3 recursively. According to some of the results here, we can constantly take the model in the process of calculation, avoiding the high precision operation.

Classic Topic 3 POJ3233(Thank RMQ)
Title: Given matrix A, the result of a + a^2 + a^3 + ... + a^k (two matrices added is the corresponding position added separately). Output of the data mod m. K<=10^9.
This problem is two times two points, quite classic. First of all we know that a^i can be divided into two points to find out. Then we need to make two points for the data size K of the whole topic. For example, when k=6, there are:
A + a^2 + a^3 + a^4 + a^5 + a^6 =(A + a^2 + a^3)+ a^3*(A + a^2 + a^3)
After applying this formula, the size of k decreases by half. We can get the answer to the original question after we find out the a^3 and then calculate a + a^2 + a^3 recursively.

Classic Topic 4 VOJ1049
The main idea: in order to give m permutation, repeated use of the m permutation of the initial sequence of operations, asked K-replacement sequence. m<=10, k<2^31.
First the M permutation is "merged" (the product of this m permutation is calculated), and then we need to perform this permutation k/m (rounding, if the remainder is a few steps to simulate). Note that any permutation can be expressed in the form of a matrix. For example, replacing 1 2 3 4 with 3 1 2 4 is equivalent to the following matrix multiplication:


Permutation k/m Times is the equivalent of multiplying the k/m by a matrix such as the preceding one. We can calculate the k/m of the matrix by two points, then multiply the initial sequence. Don't be too busy to be happy, when you are dead, don't forget that there may be several permutations that need to be simulated at the end.

Classic Topic 5 "Algorithmic Arts and Informatics Competition" 207 pages (2.1 Algebraic methods and models, [Example 5] bacteria, the release of different may have a deviation of the page number)
Let's go and see for yourselves, the book is very detailed. The problem-solving method is similar to the previous one, that is, the matrix is used to represent the operation, and then the final state is obtained by two points.

Classic Topic 6 given N and p, for the nth Fibonacci number mod p value, n not more than 2^31
Based on some of the previous ideas, we now need to construct a 2 x 2 matrix so that it is multiplied (a, b) to get the result (b,a+b). Each time this matrix is multiplied, the two numbers are iterated once more. So, we can get the nth Fibonacci number by multiplying the 2 x 2 matrix by squared n times (0,1). Without much thought, this 2 x 2 matrix is easy to construct:


Classic Topic 7 VOJ1067
We can use the above method to find out any linear recursion of the nth term, the corresponding matrix is constructed by: in the upper right corner (n-1) * (n-1) in the small matrix of the main diagonal fill 1, the Matrix n line fill the corresponding coefficient, the other places are filled 0. For example, we can use the matrix multiplication below to calculate the K for f (n) = 4f (n-1)-3f (n-2) + 2f (n-4):

Solving the problem of linear recurrence relation by matrix multiplication I can make up a truck. The example given here is the case where the coefficients are all 1.

Classic Topic 8 Given a forward graph, ask the number of scenarios from point A to step K (allow repetition to pass through the edge) to reach point B mod p values
Turns the given graph into an adjacency matrix, a (I,J) =1 when and only if there is an edge i->j. The c=a*a, then C (i,j) =σa (i,k) *a (k,j), is actually equal to the number of paths from point I to J that pass exactly 2 edges (enumeration K is the transit point). Similarly, the first row J of C*a indicates the number of paths from I to J through 3 edges. Similarly, if the number of paths through the K-step is required, we only need two points to find out the a^k.

Classic Topic 9 How many schemes to fill M x N rectangles with 1 x 2 dominoes, m<=5,n<2^31, output answer mod p results


We take m=3 as an example to explain. Suppose we put the rectangle sideways on the computer screen and fill it from right to left column. The former n-2 column is filled, and column n-1 is uneven. Now all we have to do is fill out column n-1 and move the state to nth column. Because the status of Column n-1 is different (there are 8 distinct states), we need to discuss it in a separate situation. In the picture, I put the first 8 different states on the left, the transfer of 8 different states on the right, the left side of a state can be transferred to the right of a certain state in between them a line. Note In order to ensure that the scheme is not duplicated, we do not allow a domino to be placed vertically in column n-1 (for example, the 2nd state on the left cannot be transferred to the 4th State on the right), otherwise this will be repeated with the state before the other transfer. By drawing the transition relationship of these 8 states into a graph, the problem becomes this: from state 111, there are just a few options to go back to this state by N steps. For example, there are 3 scenarios for n=2,111-& gt;011->111, 111->110->111, and 111->000->111, which correspond to scheme one by one, which covers 3x2 rectangles with dominoes. So this topic is converted to our previous example 8.
Later I wrote a copy of the source code for this question. You can see the application of bit operation again.

1#include <cstdio>2 #defineSIZE (1&LT;&LT;M)3 #defineMax_size 324 using namespacestd;5  6 classCmatrix7 {8  Public:9 LongElement[max_size][max_size];Ten voidSetSize (int); One voidSetmodulo (int); ACmatrixoperator*(Cmatrix); -Cmatrix Power (int); - Private: the intsize; - Longmodulo; - }; -   + voidCmatrix::setsize (inta) - { +  for(intI=0; i<a; i++) A  for(intj=0; j<a; J + +) atelement[i][j]=0; -Size =A; - } -   - voidCmatrix::setmodulo (inta) - { inmodulo =A; - } to   +Cmatrix Cmatrix::operator*(Cmatrix param) - { the Cmatrix product; * product.setsize (size); $ Product.setmodulo (modulo);Panax Notoginseng  for(intI=0; i<size; i++) -  for(intj=0; j<size; J + +) the  for(intk=0; k<size; k++) + { Aproduct.element[i][j]+=element[i][k]*Param.element[k][j]; theproduct.element[i][j]%=modulo; + }  - returnproduct; $ } $   -Cmatrix Cmatrix::p ower (intexp) - { theCmatrix tmp = (* This) * (* This); - if(exp==1)return* This;Wuyi Else if(Exp &1)returnTmp.power (exp/2) * (* This); the Else returnTmp.power (exp/2); - } Wu   - intMain () About { $ Const intvalidset[]={0,3,6, A, the, -, -, -}; - LongN, M, p; - Cmatrix Unit; -scanf"%d%d%d", &n, &m, &p); A unit.setsize (SIZE); +  for(intI=0; i<size; i++) the  for(intj=0; j<size; J + +) - if((~i) &j) = = ((~i) & (size-1)) ) $ { the BOOLIsvalid=false; the  for(intk=0; k<8; k++) isvalid=isvalid| | (i&j) = =Validset[k]; theunit.element[i][j]=IsValid; the } -   in Unit.setmodulo (p); theprintf"%d", Unit.power (n). element[size-1][size-1] ); the return 0; About}



Classic Topic POJ2778
The main idea is to detect how many DNA strings of all possible n-bit DNA strings do not contain the specified viral fragments. Legitimate DNA can only be made up of ACTG four characters. The topic will give 10 or less virus fragments, each fragment length not more than 10. Data size n<=2 000 000 000.
In the following tutorial, we take the atc,aaa,ggc,ct four virus fragments as an example, how to use the same as the above topic through the composition of the problem into the example 8. We identified the prefixes of all virus fragments and divided the N-bit DNA into the following 7 categories: End With at, end with AA, ending with GG, with? A end, with? g End, with? C End and with?? End. Where the question mark denotes "Other case", it can be any letter, as long as the letter does not make its string a prefix for a virus. Obviously, these classifications are a division of the complete works (the intersection is empty and set to complete). Now, if we already know the number of DNA in each of the various DNA lengths of n-1, we need to find out the number of different types of DNA in the length n. We can construct a weighted graph on the edge based on the transfer between the types. For example, from at cannot transfer to AA, from at to?? There are 4 ways (plus any one letter), from? A transfer to AA has 1 options (add a later), from? A transfer to?? There are 2 kinds of programs (followed by G or C), from GG to?? There are 2 options (the following plus C will constitute a viral fragment, not legal, only A and T) and so on. The construction of this graph is similar to the use of finite state automata to do string matching. Then we turn this graph into a matrix and let the Matrix squared n times. The final output is from the?? The sum of the paths of the State to all other states.
The data size in the topic is guaranteed to be no more than 100 prefixes, and a matrix multiplication is three-party, with a total of log (n) times. So the total complexity of the problem is 100^3 * log (n), AC.


Summarize:
1. The matrix satisfies the binding law. Heuristic thinking: A^k can be used when solving a dichotomy.
2. Matrices can help us abstract the problem model.
For example, question one ~ question seven the repetitive operation establishes a matrix representing each operation.
The original problem is then solved by the operation of the matrix product representing the repeating row.
3. In the path of solving the problem can be in accordance with the pro-link matrix, a^k, from I to j results K-Path can be such problems.

Learning experience: "10 classic problems solved by matrix multiplication" from Matrix67

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.