Quad Tiling
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 1801 |
|
Accepted: 698 |
Description
Tired of the Tri Tiling game Finally, Michael turns to a more challengeable game, Quad Tiling:
In what many ways can you tile a 4xN (1≤n≤109) rectangle with 2x1 dominoes? For the answer would is very big, output the answer modulo M (0 < m≤105).
Input
Input consists of several test cases followed by a line containing double 0. Each test case consists of integers, N and M, respectively.
Output
For each test case, output the answer modules M.
Sample Input
1 10000
3 10000
5 10000
0 0
Sample Output
1
95
Source POJ monthly--2007.10.06, Dagger feeling: This problem I depressed for a long time. Because there is no good problem-solving report online ... And the standard is written. I can't read = = so I never understood. Now quietly think about it, in fact, everything is logical. Once AC, celebrate, although very cool, but depressed is, just blog out of the bug, write good problem-solving report, depressed = = must remember after the post to save a. The main idea: Give a 4*n matrix, ask to use the 1*2 dominoes to completely cover how many kinds of programs. Number of output scenarios mod m. Analysis: The traditional method is timed out because N has the largest number of 10^9. If the definition of f[i,k] means that the first i-1 line has been completely covered, the first line of the scheme number of K, note that F[i] is only related to f[i-1], and the relationship is linear, and F of the I domain is very large, but the K domain is very small (0~15), and the state transfer equation for each adjacent two rows is identical, Therefore, a matrix can be used to represent this transfer method. Constructs a 16*16 matrix G, which makes f[i]*g=f[i+1]. Here, F[i] is considered to be a 1*16 matrix. As long as the matrix multiplication is understood thoroughly, it is not difficult to use DFS to construct the matrix G. Then, as long as the first line of possible state org by the GN times, you can get f[n+1],f[n+1,0] that is the answer. Because matrix multiplication satisfies the binding law, g^n can be obtained by fast exponentiation, multiplied by the org. At this point, the problem is resolved. Codes:type arr=array[0..16,0..16] of Longint; Const org:array[0..15] of longint= (1,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1);//The first line may be placed out of the state var g,ans,temp:array[0..16,0..16 ] of Longint; N,m:longint; Procedure Dfs (X,old,new:longint); Begin IF X=4 then Inc (G[new,old]); If X>3 then exit; If old and (1 SHL x) =0 then DFS (x+1,old,new SHL 1 or 1); If old and (1 SHL x) >0 then BEGIN if old and (1 SHL (x+1)) >0 then DFS (x+2,old,new SHL 2 or 3); DFS (x+1,old,new SHL 1); End End Procedure Mul (A,b:arr;var C:arr); var i,j,k:longint; T:arr; Begin Fillchar (t,sizeof (t), 0); For I:=0 to, j:=0 to, k:=0 to-do t[i,j]:=t[i,j]+a[i,k]*b[k,j]; T[I,J]:=T[I,J] mod m; End c:=t; End Procedure cal (X:longint); Begin if X=1 then BEGIN ans:=g; Exit End Cal (x Div 2); Mul (ans,ans,temp); Ans:=temp; If x and 1>0 then BEGIN Mul (ANS,G,TEMP); Ans:=temp; End End procedure Print; var i:longint; An:int64; Begin an:=0; For i:=0 to An:= (An+ans[0,i]*org[i]) mod m; Writeln (an); End Procedure main; var i:longint; Begin for I:=0 to + do Dfs (0,i,0); READLN (N,M); While N+m<>0 does begin Fillchar (ans,sizeof (ans), 0); Cal (n); Print READLN (N,M); End End Begin main; End.