题意
在M*N棋盘上放置任意数量的马,1≤M≤4,1≤N≤109
题解
矩阵快速幂。以两列(M*2)的放置情况作为状态,求出状态间转移的01矩阵,再快速幂求答案。
代码
/****************************************\
* Author : ztx
* Title : F - Knights
* ALG :
* CMT :
* Time :
\****************************************/
#include <cstdio>
#define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define rep(i,l,r) for(i=(l);i< (r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
#define rev(i,r,l) for(i=(r);i> (l);i--)
typedef long long ll ;
typedef double lf ;
int CH , NEG ;
template <typename TP>inline void read(TP& ret) {
ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;
if (CH == '-') NEG = true , CH = getchar() ;
while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;
if (NEG) ret = -ret ;
}
template <typename TP>inline void readc(TP& ret) {
while (ret=getchar() , ret<'!') ;
while (CH=getchar() , CH>'!') ;
}
template <typename TP>inline void reads(TP *ret) {
ret[0]=0;while (CH=getchar() , CH<'!') ;
while (ret[++ret[0]]=CH,CH=getchar(),CH>'!') ;
ret[ret[0]+1]=0;
}
#define maxs 260LL /// 2^(2*4)=256
#define mod 1000000009LL
struct Mat {
int n , m ;
int a[maxs][maxs] ;
inline void init(int i,int j) {
n = i , m = j ;
Rep (i,1,n) Rep (j,1,m) a[i][j] = 0 ;
}
Mat operator * (const Mat&B) const {
Mat ret ; ret.init(n,B.m) ; int i , j , k ;
Rep (i,1,n) Rep (j,1,B.m) Rep (k,1,m)
ret.a[i][j] = ((ll)a[i][k]*B.a[k][j]+ret.a[i][j])%mod ;
return ret ;
}
Mat operator ^ (const int&b) const {
/// p > 0
Mat ret=*this , x=*this ;
for (int p=b-1;p>0;p>>=1,x=x*x) if (p&1) ret=ret*x ;
return ret ;
}
inline void print() {
printf("<Mat %d,%d>:\n",n,m) ;
int i , j ;
Rep (i,1,n) {
Rep (j,1,m) {
printf("%d ",a[i][j]) ;
}
puts("") ;
}
}
} A , B ;
int n , m , sta ;
inline void dfs(int a,int b,int c,int dep) {
if (dep > m) {
A.a[a*(1<<m)+b+1][b*(1<<m)+c+1] = 1 ;
// 0->255 ---> 1->256
return ;
}
int aa , bb , cc , sta ;
rep (sta,0,8) {
aa = a<<1 , bb = b<<1 , cc = c<<1 ;
if ((sta&1) && ((aa&2)||(bb&4))) continue ;
if ((sta&2) && ((aa&4)||(cc&4))) continue ;
if ((sta&4) && ((bb&4)||(cc&2))) continue ;
if (sta&1) cc ++ ;
if (sta&2) bb ++ ;
if (sta&4) aa ++ ;
dfs(aa,bb,cc,dep+1) ;
}
}
int main() {
int T , i ;
ll ans ;
// #define READ
#ifdef READ
freopen(".in" ,"r",stdin ) ;
freopen(".out","w",stdout) ;
#endif
for (scanf("%d", &T) ; T -- ; ) {
read(m) , read(n) ;
sta = 1 << (m << 1) ;
A.init(sta,sta) , B.init(1,sta) ;
B.a[1][1] = 1 ;
dfs(0,0,0,1) ;
A = A^n ;
B = B*A ;
ans = 0 ;
Rep (i,1,sta) ans += B.a[1][i] , ans %= mod ;
printf("%I64d\n", ans) ;
}
#ifdef READ
fclose(stdin) ; fclose(stdout) ;
#else
getchar() ; getchar() ;
#endif
return 0 ;
}