Covering
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1789 Accepted Submission(s): 683
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
Source
问题链接:HDU6185 Covering
问题简述:(略)
问题分析:
这个问题的关键是找出递推式。递推式构成一个数列,然后用矩阵快速幂来解。
先求初始项(需要手工计算),再找出递推式(需要找规律):
f[1] = 1
f[2] = 5
f[3] = 11
f[4] = 36
......
f[n] = f[n-1]+5*f[n-2]+f[n-3]-f[n-4]
程序说明:(略)
题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* HDU6185 Covering */
#include <iostream>
#include <string.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
const int N = 4;
struct Matrix
{
LL m[N][N];
Matrix()
{
memset(m, 0, sizeof(m));
}
// 矩阵相乘
Matrix operator * (const Matrix& y)
{
Matrix z;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
for(int k=0; k<N; k++) {
z.m[i][j] += m[i][k] * y.m[k][j] % MOD;
z.m[i][j] %= MOD;
}
return z;
}
};
// 矩阵快速幂
Matrix Matrix_Powmul(Matrix x, LL m)
{
Matrix z;
for(int i = 0; i < N; i++)
z.m[i][i] = 1;
while(m) {
if(m & 1)
z = z * x;
x = x * x;
m >>= 1;
}
return z;
}
int main()
{
Matrix a, b;
a.m[0][0] = 1;
a.m[0][1] = 1;
a.m[1][0] = 5;
a.m[1][2] = 1;
a.m[2][0] = 1;
a.m[2][3] = 1;
a.m[3][0] = -1;
b.m[0][0] = 36;
b.m[0][1] = 11;
b.m[0][2] = 5;
b.m[0][3] = 1;
LL n;
while(cin >> 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 {
Matrix t = Matrix_Powmul(a, n - 4);
Matrix ans = b * t;
printf("%lld\n", (ans.m[0][0] + MOD) % MOD);
}
}
return 0;
}