题意 骨牌,一种古老的玩具。今天我们要研究的是骨牌的覆盖问题:
我们有一个2xN(1≤N≤100,000,000)的长条形棋盘,然后用1x2的骨牌去覆盖整个棋盘。对于这个棋盘,一共有多少种不同的覆盖方法呢?
举个例子,对于长度为1到3的棋盘,我们有下面几种覆盖方式:
输入
第1行:1个整数N。表示棋盘长度。1≤N≤100,000,000
输出
第1行:1个整数,表示覆盖方案数 MOD 19999997
思路
f(n) = f(n-1) + f(n-2) ,然后利用斐波那契数列的矩阵快速幂求解。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
const ll MOD = 19999997;
mat operator*(mat a,mat b){
if(a[0].size() != b.size())
return mat(0,vec(0));
mat c = mat(a.size(),vec(b[0].size()));
int matn = a.size();
int matm = b[0].size();
for(int i=0;i<matn;i++)
{
for(int j=0;j<matm;j++)
{
for(int k=0;k < b.size();k++)
{
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % MOD;
}
}
}
return c;
}
mat matpow(mat a,int p)
{
mat c(a.size(),vec(a.size()));
int matn = a.size();
for(int i=0;i<matn;i++)
{
c[i][i] = 1;
}
while(p > 0)
{
if(p&1)
c = c * a;
p>>=1;
a = a*a;
}
return c;
}
int n,m;
mat yuan,xi;
int main()
{
int i,j;
yuan.push_back(vec(1,2));
yuan.push_back(vec(1,1));
xi.push_back(vec(2,1));
xi.push_back(vec(2));
xi[1][0] = 1;
xi[1][1] = 0;
scanf("%d",&n);
yuan = matpow(xi,n-1) * yuan;
printf("%lld\n",yuan[1][0]);
return 0;
}