Http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 20851 # overview
A-H is Matrix Multiplication
There is a struct matrix in the top, which is convenient to write questions.
First, matrix multiplication satisfies the combination law, so we can use a rapid power method to calculate the power of the matrix.
Matrix expmod (int k) {matrix e; // unit matrix e. n =. n; e. init (); while (k) {if (k & 1) e = e * a; a = a * a; k >>=1;} return e ;}
Speaking of matrix multiplication, we have to mention the Fibonacci series, f [0] = 0, f [1] = 1, f [n] = f [n-1] + f [N-2].
Constructor A = [0 1
1]
Obviously, [f (n-1), f (n)] * A = [f (n), f (n + 1)] (set to B * A = C)
Now let's consider how to construct this matrix.
First, the first item of matrix C is the second item of matrix B, and C [1] is obtained from the first column of B multiplied by A. Then the second number of the first column of another A is 1, if the other values are 0, B [1] * 0 + B [2] * 1 can obtain C [1], C [2] = B [1] + B [2], then the second column of A is two columns, and B [1] * 1 + B [2] * 1 = C [2] is obtained.
This requires that item K be as long as [f [0], f [1] * A ^ (k-1) = (f [k-1], f [k])
【
Let's talk about it here. Many people ask me how to construct a matrix. Even if I show him the constructed matrix, I still don't understand it. In this case, my suggestion is to manually multiply the Matrix. After the multiplication, you will understand.
]
We perform a deformation on FIB, f [0] = 0, f [1] = 1, f [n] = f [n-1] + f [N-2] + C (C is a constant)
In this case, we need to add constants to the matrix. The constructor matrix is as follows:
[C, f [n-1], f [n] * [1 0 1
0 0 1
0 1 1] = [C, f [n], f [n + 1]
Question a hdu 1757
The structure matrix is very simple, and the recursive relationship is very obvious. The matrix is constructed directly according to the above method as follows:
0 0 0 0 0 0 0 a9
1 0 0 0 0 0 0 a8
0 1 0 0 0 0 0 a7
0 0 1 0 0 0 0 a6
0 0 0 1 0 0 0 a5
0 0 0 0 1 0 0 a4
0 0 0 0 1 0 a3
0 0 0 0 0 1 0 a2
0 0 0 0 0 0 1 a1
B. HDU 2157
[This question has an error of 3157 in the matrix topic, Summary]
Construct an adjacent matrix. Assume that the number of solutions from A to B in K step is x [A] [B] of the K power of the adjacent matrix.
This random discrete mathematics book should prove everything. I will not say much. [I have never proved it. I will not say anything about it?
Question C: POJ 3233
Http://blog.csdn.net/d891320478/article/details/8704903
I have written this question before.
D. ZOJ 3497
If you forget the meaning of the question, it seems to be giving a directed graph to determine whether K steps can be taken from the first point to the last point. The same method as question B is not explained.
To prevent overflow, you can change the plus operation for matrix multiplication to the | operation.
E. HDU 2807
This question is boring. It makes every city in N cities a K * K square matrix, if ABC has A * B = C in three cities, A to C has A directed path with A length of 1, and then asks A number of questions each time to find the shortest short circuit between two points. Bored and violent. [although the complexity is totally wrong]
Question f hdu 3483
The matrix structure of this question is more difficult, http://www.cppblog.com/Yuan/archive/2010/08/13/123268.html
I should post a link from someone else. At that time, I pushed the matrix for half a day, namely
In fact, the method for this question is to look at what N + 1 is more than N, and you will understand it after you make a difference.
G question HDU 2276
There is a circle of lights, where 0 and 1 respectively indicate the light is dark, the light changes the state in a regular way every second, the rule is that the current light changes the state, otherwise, the status will not change.
Then calculate the status at the k second.
Directly construct the relational matrix and multiply it, similar to the recursion of question.
H: HDU 2855
At the beginning, I wanted to use the method like question F to find the difference between the two adjacent items.
It's amazing to find a solution based on others' questions.
First, (x + 1) ^ n = sigma (c (n, k) x ^ k). Then, replace x with the recursive matrix of FIB, and 1 with the unit matrix, get
([0, 1; 1, 1] + [0, 1; 0]) ^ n
Then the matrix is multiplied.