HDU - 6185 Covering 矩阵快速幂

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≤1018

 

Output

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

Sample Input

1
2

Sample Output

1
5

题解:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
struct node{
	ll mat[20][20];
};
ll n;
ll b[4]={36,11,5,1};
node cul(node x,node y)
{
	node z;
	for(int i=0;i<=3;++i)
	{
		for(int j=0;j<=3;++j)
		{
			z.mat[i][j]=0;
			for(int k=0;k<=3;++k)
			{
				z.mat[i][j]=(z.mat[i][j]+x.mat[i][k]*y.mat[k][j]%mod)%mod;
			}
		}
	}
	return z;
}

void ksm(ll k)
{
	node ans,a;
	for(int i=0;i<=3;++i)
		for(int j=0;j<=3;++j)
		{
			if(i==j) ans.mat[i][j]=1;
			else ans.mat[i][j]=0;
			a.mat[i][j]=0;
		}
//	ll tk=k%(mod-1)+mod-1;
//	if(tk<k)
//		k=tk;
	a.mat[0][0]=1;a.mat[0][1]=5;a.mat[0][2]=1;a.mat[0][3]=-1;
	a.mat[1][0]=1;
	a.mat[2][1]=1;
	a.mat[3][2]=1;
	
	while(k)
	{
		if(k&1) ans=cul(ans,a);
		k>>=1;
		a=cul(a,a);
	}
	ll res=0;
	for(int i=0;i<=3;i++)
	{
	//	cout<<res<<endl;
		res=((res+ans.mat[0][i]*b[i]%mod)%mod+mod)%mod;
	}
	printf("%lld\n",res);
}


int main()
{
	while(~scanf("%lld",&n))
	{
		if(n<=4) printf("%lld\n",b[4-n]);
		else ksm(n-4);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值