2017ACM/ICPC广西邀请赛 Covering【矩阵快速幂】

Covering

http://acm.hdu.edu.cn/showproblem.php?pid=6185

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2775    Accepted Submission(s): 1029


 

Problem Description

Bob's school has a big playground, boys and girls always play games here after school.To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets.Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more.He has infinite carpets with sizes of 1×2 and 2×1, and the size of the playground is 4×n.Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping?

 

Input

There are no more than 5000 test cases. Each test case only contains one positive integer n in a line. 1≤n≤10^18

 

Output

For each test cases, output the answer mod 1000000007 in a line.

 

Sample Input

1
2

Sample Output

1
5

Source

2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

题意

用1*2或2*1的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠,结果对1000000007取模

思路

使用DFS暴力求出n的前1-10的结果,分别为1 5 11 36 95 281 781 2245 6336 18061,然后用高斯消元法求出递推方程为f_{n}=f_{n-1}+5*f_{n-2}+f_{n-3}-f_{n-4},最后使用矩阵快速幂求解即可。

C++程序

#include<iostream>
#include<vector>

using namespace std;

typedef long long ll;

typedef vector<ll>vec;
typedef vector<vec>mat;

const int mod=1000000007;

mat mul(const mat &a,const mat &b)
{
	mat c(a.size(),vec(b[0].size()));
	for(int i=0;i<a.size();i++)
	  for(int j=0;j<b[0].size();j++)
	    for(int k=0;k<a[0].size();k++)
	      c[i][j]=(c[i][j]+a[i][k]*b[k][j]+mod)%mod;
	return c;
}

mat qpow(mat a,ll n)
{
	mat res(a.size(),vec(a[0].size()));
	for(int i=0;i<a.size();i++) res[i][i]=1;
	while(n)
	{
		if(n&1) res=mul(res,a);
		a=mul(a,a);
		n>>=1;
	}
	return res;
}

ll solve(ll n)
{
	mat res(4,vec(1));
	res[0][0]=36;
	res[1][0]=11;
	res[2][0]=5;
	res[3][0]=1;
	//fn=fn-1 + 5 * fn-2 + fn-3 - fn-4
	if(n<=4) return res[4-n][0];
	
	mat tmp(4,vec(4));
	tmp[0][0]=1,tmp[0][1]=5,tmp[0][2]=1,tmp[0][3]=-1;
	tmp[1][0]=1,tmp[1][1]=0,tmp[1][2]=0,tmp[1][3]=0;
	tmp[2][0]=0,tmp[2][1]=1,tmp[2][2]=0,tmp[2][3]=0;
	tmp[3][0]=0,tmp[3][1]=0,tmp[3][2]=1,tmp[3][3]=0;
	tmp=qpow(tmp,n-4);
	res=mul(tmp,res);
	return res[0][0];
}

int main()
{
	ll n;
	while(~scanf("%lld",&n))
	{
		printf("%lld\n",solve(n));
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值