描述
上一週我們研究了2xN的骨牌問題,這一週我們不妨加大一下難度,研究一下3xN的骨牌問題?
所以我們的題目是:對於3xN的棋盤,使用1x2的骨牌去覆蓋一共有多少種不同的覆蓋方法呢?
首先我們可以肯定,奇數長度一定是沒有辦法覆蓋的;對於偶數長度,比如2,4,我們有下面幾種覆蓋方式:
輸入
第1行:1個整數N。表示棋盤長度。1≤N≤100,000,000
輸出
第1行:1個整數,表示覆蓋方案數 MOD 12357
62247088
樣例輸出
4037
/*上一週我們研究了2xN的骨牌問題,這一週我們不妨加大一下難度,研究一下3xN的骨牌問題?
所以我們的題目是:對於3xN的棋盤,使用1x2的骨牌去覆蓋一共有多少種不同的覆蓋方法呢?
首先我們可以肯定,奇數長度一定是沒有辦法覆蓋的;對於偶數長度,比如2,4,我們有下面幾種覆蓋方式:
*/
#define ll long long
#include <iostream>
using namespace std;
const int MAXN= 100000000;
const int constN = 8;
const ll MOD = 12357;
int n;
ll re = 0;
int c[constN][constN] ={
0,0,0,0,0,0,0,1,
0,0,0,0,0,0,1,0,
0,0,0,0,0,1,0,0,
0,0,0,0,1,0,0,1,
0,0,0,1,0,0,0,0,
0,0,1,0,0,0,0,0,
0,1,0,0,0,0,0,1,
1,0,0,1,0,0,1,0
};
typedef struct Matrix{
int (*a)[constN];
Matrix(int (*b)[constN]){
a = new int [constN][constN];
for(int i=0;i<constN;i++) {
for(int j=0;j<constN;j++){
a[i][j] = b[i][j];
}
}
};
Matrix operator*(const Matrix &m1) const{
Matrix temp(c);
for(int i=0;i<constN;i++) {
for(int j=0;j<constN;j++) {
int sum=0;
for(int k1=0;k1<constN;k1++)
sum+=a[i][k1]*m1.a[k1][j];
temp.a[i][j] = sum%MOD;
}
}
return temp;
};
}Matrix;
Matrix solve(const Matrix &a,int n){
Matrix tmp(c);
if(n==1||n==0) return tmp;
tmp = solve(a,n/2);
if(n&1) {
tmp = tmp*tmp*a;
}
else {
tmp = tmp*tmp;
}
return tmp;
}
int main(){
Matrix m(c);
while(cin>>n){
if(n%2==1) {
cout<<"0"<<endl;
}
else {
m = solve(m,n);
cout<<m.a[7][7]<<endl;
}
}
}