Covering HDU - 6185

点击打开链接

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.

1n1018
Output For each test cases, output the answer mod 1000000007 in a line.
Sample Input
1
2
Sample Output
1
5

#include<cstdio>
#include<cstring>

/*
矩阵快速幂 是没有什么疑问的
1.打表推通项     应该知道,系数有可能是负值,---->注意计算的时候,过程中要加上mod
2.                 利用机子暴力我们的情况,不要用手去推,这样真的是很sb 
3.记住longlong   不要总是死在这一个地方,尽管死了很多次了,难受 
*/

typedef long long ll;
const ll mod=1e9+7;

struct  Martrix{		
	ll mar[4][4];
	Martrix operator*(Martrix& obj){
		Martrix ans;
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				ans.mar[i][j]=0;
				for(int k=0;k<4;k++){
					ans.mar[i][j]=(ans.mar[i][j]+mod+mar[i][k]*obj.mar[k][j]%mod)%mod;
				}
			}
		}
		return ans;
	}
	
}E,ST; 


void init(){
	memset(E.mar,0,sizeof(E.mar));
	E.mar[0][0]=E.mar[1][1]=E.mar[2][2]=E.mar[3][3]=1;
	
	memset(ST.mar,0,sizeof(ST.mar));
	ST.mar[0][0]=1,ST.mar[0][1]=5,ST.mar[0][2]=1,ST.mar[0][3]=-1;
	ST.mar[1][0]=ST.mar[2][1]=ST.mar[3][2]=1;
}

int ans[]={0,1,5,11,36};

Martrix pow_(Martrix base,ll n){
	Martrix ans=E;
	while(n){
		if(n&1)
			ans=ans*base;
		base=base*base;
		n>>=1;
	}
	return ans;
}

int main(){
	init();
	ll n;
	while(scanf("%I64d",&n)==1){
		if(n<4)printf("%d\n",ans[n]);
		else{
		   Martrix tmp=pow_(ST,n-4);
		   ll ans=(tmp.mar[0][0]*36%mod+tmp.mar[0][1]*11%mod+tmp.mar[0][2]*5%mod+tmp.mar[0][3]%mod+mod)%mod;
		   printf("%I64d\n",ans);
		}
	}	
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/81046123