2017ACM/ICPC广西邀请赛-重现赛 1004.Covering

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

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

Sample Input

1
2

Sample Output
1
5

题目大意:用1×2和2×1的矩形填满N×4的方格,求方案数
解题报告:打表发现规律:\(f_n=f_{n-1}+5*f_{n-2}+f_{n-3}-f_{n-4}\)
然后矩阵快速幂求解即可:

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=5,mod=1000000007;
struct mat{
	ll a[N][N];
	mat(){memset(a,0,sizeof(a));}
  mat operator *(const mat p)const{
		mat tmp;
		for(int i=1;i<N;i++)
			for(int j=1;j<N;j++)
				for(int k=1;k<N;k++){
					tmp.a[i][j]+=(a[i][k]*p.a[k][j])%mod;
					tmp.a[i][j]=((tmp.a[i][j]%mod)+mod)%mod;
				}
		return tmp;
	}
};
ll n;int f[5];
void work()
{
	if(n<=4){
		printf("%d\n",f[n]);
		return ;
	}
	mat S,T;
	for(int i=1;i<=4;i++)S.a[1][i]=f[5-i];
	T.a[1][1]=1;T.a[2][1]=5;T.a[3][1]=1;T.a[4][1]=-1;
	for(int i=2;i<=4;i++)T.a[i-1][i]=1;
	n-=4;
	while(n){
		if(n&1)S=S*T;
		T=T*T;n>>=1;
	}
	printf("%lld\n",S.a[1][1]);
}

int main()
{
	f[1]=1;f[2]=5;f[3]=11;f[4]=36;
	while(~scanf("%lld",&n))work();
	return 0;
}
posted @ 2017-08-31 17:00  PIPIBoss  阅读(197)  评论(0编辑  收藏  举报