Covering
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 617 Accepted Submission(s): 271
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
Sample Output
Source
2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)
Recommend
liuyiding
题意:给一个4*n的矩形,用1*2的矩阵填满,问能有多少种填法
解题思路:把4当作列数,n当作行数。当第n行填满时,第(n+1)行会出现以下几种情况:
情况a为第n行刚好填满且没有突出到第(n + 1)行,即为所求答案,由图不难推出:
a[n] = a[n - 1] + b[n - 1] + c[n - 1] + dx[n - 1] + dy[n - 1]
b[n] = a[n - 1]
c[n] = a[n - 1] + e[n - 1]
dx[n] = a[n - 1] + dy[n - 1]
dy[n] = a[n - 1] + dx[n - 1]
e[n] = c[n - 1]
令d[n] = dx[n] + dy[n]
则 a[n] = a[n - 1] + b[n - 1] + c[n - 1] + d[n - 1]
b[n] = a[n - 1]
c[n] = a[n - 1] + e[n - 1]
d[n] = a[n - 1] * 2 + d[n - 1]
e[n] = c[n - 1]
根据这个构造出矩阵即可
推公式自己不是很擅长,这个过程是我盗的,原文链接为:http://blog.csdn.net/a664607530/article/details/77619554
最后能推出的公式为 f[n]=f[n-1]+5*f[n-2]+f[n-3]-f[n-4]
根据这一公式能推出需要构造的矩阵为:
1 5 1 -1 f[4]
1 0 0 0 * f[3]
0 1 0 0 f[2]
0 0 1 0 f[1]
f[4]=36,f[3]=11,f[2]=5,f[1]=1
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include