Matrix Fast power. First, I want to work out the status of column I in each state, let the state fill up, the next column can appear. Because n is large, the matrix can be accelerated.
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<iostream>using namespaceStd;typedefLong LongLL;Const DoublePi=acos (-1.0), eps=1e-8;voidFile () {freopen ("D:\\in.txt","R", stdin); Freopen ("D:\\out.txt","W", stdout);} InlineintRead () {Charc = GetChar (); while(!isdigit (c)) C =GetChar (); intx =0; while(IsDigit (c)) {x = x *Ten+ C-'0'; c =GetChar ();} returnx;} LL N,mod;structmatrix{Long Longa[ -][ -]; intR, C; Matrixoperator*(Matrix b);}; Matrix X, Y, Z; Matrix Matrix::operator*(Matrix B) {matrix C; memset (C.A,0,sizeof(C.A)); intI, j, K; for(i =1; I <= R; i++) for(j =1; J <= B.C; J + +) for(k =1; K <= C; k++) C.a[i][j]= (C.a[i][j] + (a[i][k] * b.a[k][j])% MOD)%MOD; C.R= R; C.C =B.C; returnC;}voidDfsintAintBintTintc) { if(t==4) {x.a[c+1][b+1]=1;return; } if(a& (1<<t)) DFS (a,b,t+1, c); Else{DFS (a| (1<<t), b| (1<<t), t+1, c); if(t+1<4&& (a& (1<< (t+1)))==0) Dfs (A + (1<<T) + (1<< (t+1)), b,t+1, c); }}voidinit () {memset (x.a,0,sizeofx.a); memset (Y.A,0,sizeofy.a); memset (Z.A,0,sizeofz.a); Y.R= -; Y.C = -; for(inti =1; I <= -; i++) Y.a[i][i] =1; X.R= -; X.C = -; for(intI=0; i<= the; i++) Dfs (i,0,0, i); Z.R=1; Z.C = -; z.a[1][1]=1;}voidWork () { while(n) {if(n%2==1) Y = y*y; N= N >>1; X= x*y; } Z= z*Y; printf ("%lld\n", z.a[1][ -]);}intMain () { while(~SCANF ("%lld%lld",&n,&MOD)) { if(n==0&&mod==0) Break; N++; init (); Work (); } return 0;}
POJ 3420 Quad Tiling