Original problem: Domino coverage problem
Time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description
Dominoes, an ancient toy. Today we are going to look at the problem of Domino coverage:
We have a 2xN strip board and then use the 1x2 dominoes to cover the entire board. How many different coverage methods are there for this chessboard?
For example, for a board with a length of 1 to 3, we have the following types of coverage:
Hint: Domino Overlay
Tip: How to quickly calculate results
Input
Line 1th: an integer n. Represents the board length. 1≤n≤100,000,000
Output
Line 1th: An integer representing the number of coverage scenarios MOD 19999997
-
-
Sample input
-
-
62247088
-
-
Sample output
-
17748018
Hint: Domino Overlay
We consider how to place the new Domino (blue) in the case where a partial domino (gray) has been placed:
The right side of the situation is impossible, otherwise there will always be more than one lattice no way to place dominoes. Or the gray portion of the lattice is an odd number, it can not be placed through a 1x2 dominoes.
So by looking at the above, we can find:
At the end of any placement scenario, the first two scenarios must be met. The gray part corresponds to the placement scheme of the length N-1 and N-2. Thus, we can get a recursive formula:
F[n] = f[n-1] + f[n-2];
Does this formula look familiar? That's right, that's our Fepolaci series.
f[0]=1,f[1]=1,f[2]=2,...
Tip: How to quickly calculate results
When n is very small, we can calculate it directly by the recursive formula. When n is very large, as long as our computer is good enough, we can still calculate it directly from the recursive formula.
But we learn the algorithm, always such a direct enumeration is not very low, so we have to use a good algorithm to speed up (loaded x).
In fact, for this linear recursion, we can use matrix multiplication to find the nth term. For the Fibonacci sequence of the subject, we want to find a 2x2 matrix M that makes (a, b) x M = (b, a+b), where (A, b) and (b, a+b) are all 1x2 matrices.
Obviously, just take m = [0, 1; 1, 1] on it:
Further get:
So the next question is, can you quickly calculate the m^n? Let's start by analyzing the power operation. Since the multiplication is to satisfy the binding law, we have:
You may wish to k[1]. K[J] A better division?
where (K[1],k[2]...k[j]) 2 means that n is represented as a number for each digit after the binary number. The above formula satisfies such a property:
Combining the two we can get an algorithm:
1. First calculate all {a^1, a^2, a^4 ... a^ (2^j)}, because the sequence satisfies the recursive formula, the time complexity is O (LOGN)
2. The exponent n binary, and then use the formula to multiply the corresponding a^j to calculate the a^n, the time complexity is still O (logn)
The total time complexity is O (LOGN)
This algorithm, because it can find a power in a short time, we call the "fast power" algorithm.
1#include <iostream>2 3 using namespacestd;4 5typedefLong Longll;6 7 Const intM =19999997;8 structMatrix9 {Ten intm[2][2]; OneMatrixoperator* (matrix&a) A { - Matrix Res; -res.m[0][0] = (LL) m[0][0]*a.m[0][0]+ (LL) m[0][1]*a.m[1][0])%M;//(ll) Prevent data overflow theres.m[0][1] = (LL) m[0][0]*a.m[0][1]+ (LL) m[0][1]*a.m[1][1])%M; -res.m[1][0] = (LL) m[1][0]*a.m[0][0]+ (LL) m[1][1]*a.m[1][0])%M; -res.m[1][1] = (LL) m[1][0]*a.m[0][1]+ (LL) m[1][1]*a.m[1][1])%M; - returnRes; + } - }; + A atMatrix pow (Matrix M,intN) - { - Matrix Res; - if(1==N) - returnm; -res = POW (M, n/2); in if(n%2==1) -res = res*res*m; to Else +res = res*Res; - returnRes; the } * $ Panax Notoginseng intMain () - { the intN; +Cin>>N; A the Matrix Mat; +mat.m[0][0]=0; -mat.m[0][1]=1; $mat.m[1][0]=1; $mat.m[1][1]=1; -Mat =Pow (MAT, N); - thecout<<mat.m[1][1]; - Wuyi return 0; the}
HIHO41: Domino Coverage problem • One