HIHO42: Domino cover problem • Two

Source: Internet
Author: User

Describe

Last week we studied the 2xN Domino problem, this week we might as well increase the difficulty, study the 3xN Domino problem?
So our question is: for the 3xN chessboard, how many different coverage methods are used to cover a 1x2 Domino?
First of all we can be sure that the odd length must not be covered, for even lengths, such as 2, 4, we have the following types of coverage:

Hint: 3xN Domino Overlay

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 12357

Sample input

62247088

Sample output

4037


Hint: 3xN Domino Overlay

In the 2xN Domino coverage problem, we have the recursive formula (0,1) xm^n= (F[n-1],f[n]).
We consider whether we can find the same formula in the 3xN case.
But in the actual derivation process can be found that for 3xN coverage, the corresponding F-value formula is more complex than 2xN. We need to think about the derivation formula in a different way.

In the process of placing the dominoes, we must put a line before placing the next line. Depending on how they are placed, there may be many different shapes, and are there some recursive relationships between these shapes?
If they have a certain recursion relationship, then we can deduce the number of i+1 of the line according to the number of scenarios in line I. This line of reasoning, until the nth row does not get the number of programs we asked for?
So, let's see if there's a formula for this derivation.

Assuming we've put some dominoes in place, there are 8 possible scenarios for the current last column (column i) Dominoes:

For the above 8 states, we use numbers to mark them. A lattice with a domino placed is 1, not placed as 0, and converted to a 2 binary number.
With the bottom line as 1, there are:

Next, consider how to place the dominoes, and we 'll rotate the board a bit . Assuming that we are placing the domino of line I, there are 3 ways:

Gray indicates the existing dominoes, and Green indicates the newly placed dominoes.
Each placement method is interpreted as follows, assuming that when the status of line I is X, the state of the i-1 line is y:

    • Line I is not placed, the previous line must have a placed Domino. x corresponds to bits for 0,y corresponding to bits for 1.
    • Line I vertical dominoes, the previous line must be empty. x corresponds to bits for 1,y corresponding to bits for 0.
    • Row I horizontal dominoes, the previous line must have two positions have dominoes, otherwise it will produce empty. x corresponds to bits for 1,y corresponding to bits for 1.

As an example:

For line I state 1, we can reach state 6 after we put two dominoes in line i+1.
However, it is important to note that the following situation occurs:

This seems to have changed from State 1 to state 0, which is actually wrong. It does not satisfy our agreed placement method, the essence is that the state of line I is 1 into the status of line I 7, and actually we should place the line i+1.
So be careful when enumerating the recursive relationships.
By enumerating the transitions of 8 states to 8 states, we can get an 8x8 matrix M (where all blanks are 0):

M[I][J] represents the number of schemes that change from state I to status J.

Now that we have the M matrix, let's consider the boundary condition.
In a 2xN Domino overlay, there is (0, 1) as the initial vector a, so what is the initial vector a in 3xN?
Let's first think about what the a vector represents. The M-matrix represents the state-to-state transition, and the a vector should represent the number of scenarios for each state in line No. 0.
Similarly, the result of a * m^n should be expressed as the number of scenarios for the various states of the nth row.
So what should a vector be? Obviously, the No. 0 line in our recursive process must be considered as state 7 is reasonable. So a vector is represented as:
{0, 0, 0, 0, 0, 0, 0, 1}
And for the answer we seek, it is also natural that the nth row is placed as state 7 of the number of scenarios.
#include <iostream>using namespace Std;typedef long long ll;const int M = 12357;struct Matrix{int m[8][8]; Matrix () {for (Int. i=0; i<8; i++) {for (int j=0; j<8; J + +) {m[i][j]=0;}} for (int i=0; i<8; i++) {m[i][7-i]=1;} M[3][7]=1;m[7][3]=1;m[6][7]=1;m[7][6]=1;} Matrix operator* (matrix& a) {matrix res;for (int i=0; i<8; i++) {for (int j=0; j<8; J + +) {res.m[i][j]=0;for (int k= 0; k<8; k++) {res.m[i][j]= (Res.m[i][j] + (LL) m[i][k]*a.m[k][j])%M;}}} return res;}}; Matrix pow (matrix m, int n) {matrix res;if (1==n) return m;res = Pow (M, N/2); if (n%2==1) res = Res*res*m;elseres = Res*res;retu RN Res;} int main () {int n;cin>>n; Matrix Mat;mat = Pow (mat, N); Cout<<mat.m[7][7];return 0;}

  



HIHO42: Domino cover problem • Two

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.