Test instructions
Give 4*n (n<10^9) a large rectangle and ask how many schemes are filled with small rectangles of 1*2.
Analysis:
Tile again, but this time n is too large to be a one-grid DP. We can calculate the state transfer of two adjacent lines first, and then use the matrix to accelerate the state transfer of n rows.
Code:
POJ 3420//sep9#include <iostream>using namespace Std;const int maxn=16;struct matrix{__int64 M[maxN][maxN];} Mat;int n,mod;void dfs (int l,int now,int pre) {if (l>4) return;if (l==4) {++mat.m[pre][now];return;} DFS (l+1, (now<<1) |1,pre<<1);d FS (l+1,now<<1, (pre<<1) | |);d FS (l+2, (now<<2) |3, (pre <<2) |3);} Matrix Mul (Matrix A,matrix b) {matrix C; memset (c.m,0,sizeof (C.M)); for (int i=0;i<16;++i) for (int j=0;j<16;++j) for (int k=0;k<16;++k) {C . M[i][j]+=a.m[i][k]*b.m[k][j]; C.m[i][j]%=mod;} return C; }matrix Expo (MATRIX a,int k) {if (k==1) return A; MATRIX e; memset (e.m,0,sizeof (E.M)); for (int i=0;i<16;++i) {e.m[i][i]=1;} if (k==0) return e; while (k) {if (k&1) E=mul (a,e); A=mul (A,a); k>>=1; } return E; } int main () {memset (mat.m,0,sizeof (MAT.M)); DFS (0,0,0); while (~SCANF ("%d%d", &n,&mod)) {if (!n&&!mod) break; if (mod==1) {printf ("0\n"); continue;} MATRIX Ans=expo (mat,n); printf ("%i64d\n", ans.m[15][15]); } return 0; }
POJ 3420 Quad Tiling-like pressure DP