hihoCoder1143 DP 斐波那契数列矩阵快速幂

题意 骨牌,一种古老的玩具。今天我们要研究的是骨牌的覆盖问题:
我们有一个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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值