Quad Tiling
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 3495 |
|
Accepted: 1539 |
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 4x n (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 100003 100005) 100000 0
Sample Output
11195
Source
POJ monthly--2007.10.06, Dagger,,, drunk,, the game has not introduced the formula, alas, the weak is not the focus on the recursive formula, and then the matrix fast power can be. SCU 4430 Change the input to just a little bit.
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#definell Long Long#defineN 10intMOD;structmatric{intsize; intA[n][n]; Matric (ints=0) {size=s; Memset (A,0,sizeof(a)); } matricoperator* (ConstMatric &t) {matric res=matric (size); for(intI=0; i<size;i++) { for(intk=0; k<size;k++) { if((* This). A[i][k]) for(intj=0; j<size;j++) {Res.a[i][j]+ = (ll) (* This). a[i][k]*t.a[k][j]%MOD; RES.A[I][J]= (res.a[i][j]+mod)%MOD; } } } returnRes; } matricoperator^ (intN) {matric ans=matric (size); for(intI=0; i<size;i++) ans.a[i][i]=1; while(n) {if(n&1) ans=ans* (* This); (* This)=(* This)*(* This); N>>=1; } returnans; } voidDebug () { for(intI=0; i<size;i++) { for(intj=0; j<size;j++) {printf ("%d", A[i][j]); } printf ("\ n"); } }};intMain () {intN; while(SCANF ("%d%d", &n,&mod), n| |MOD) {Matric a=matric (4); Matric B=matric (4); a.a[0][0]=1; a.a[0][1]=1; a.a[0][2]=5; a.a[0][3]= One; b.a[0][3]=-1; b.a[2][3]=5; b.a[1][0]=b.a[2][1]=b.a[3][2]=b.a[1][3]=b.a[3][3]=1; b=b^N; A=a*b; printf ("%d\n", (a.a[0][0]+MOD)%MOD); } return 0;}
[POJ 3420] Quad Tiling