Covering
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 317 Accepted Submission(s): 163
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?
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
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
题目大意:
给你一个4*N的矩阵,我们有小矩形1*2的和2*1的。问我们有多少种覆盖方式。
思路:
本地暴力打表找到前几项的答案,然后递推一下规律即可。
F【i】=F【i-1】+5*F【i-2】+F【i-3】-F【i-4】;
观察到数据范围很大,套一个矩阵快速幂即可。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll long long int
int const mod = 1000000007;
struct matrix
{
ll m[5][5];
};
matrix A,B,tmp;
matrix multiply(matrix x, matrix y) //矩阵乘法
{
matrix tmp;
memset(tmp.m, 0, sizeof(tmp.m));
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(x.m[i][j] == 0)
continue;
for(int k = 0; k < 4; k++)
{
if(y.m[j][k] == 0)
continue;
tmp.m[i][k] += x.m[i][j] * y.m[j][k];
tmp.m[i][k]=(tmp.m[i][k]%mod+mod)%mod;
}
}
}
return tmp;
}
matrix quickmod(matrix a,ll n) //矩阵快速幂
{
matrix res;
memset(res.m, 0, sizeof(res.m));
for(int i = 0; i < 4; i++)
res.m[i][i] = 1;
while(n)
{
if(n & 1)
res = multiply(res, a);
n >>= 1;
a = multiply(a, a);
}
return res;
}
int main()
{
ll n;
while(~scanf("%lld",&n))
{
if(n==1)printf("1\n");
else if(n==2)printf("5\n");
else if(n==3)printf("11\n");
else if(n==4)printf("36\n");
else
{
memset(A.m,0,sizeof(A.m));
A.m[0][0]=1;A.m[0][1]=5;A.m[0][2]=1;A.m[0][3]=-1;
A.m[1][0]=1;A.m[2][1]=1;A.m[3][2]=1;
B=quickmod(A,n-4);
ll ans=0;
ans+=36*B.m[0][0];ans=(ans%mod+mod)%mod;
ans+=11*B.m[0][1];ans=(ans%mod+mod)%mod;
ans+=5*B.m[0][2];ans=(ans%mod+mod)%mod;
ans+=1*B.m[0][3];ans=(ans%mod+mod)%mod;
printf("%lld\n",ans);
}
}
}