Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3009 Accepted Submission(s): 1098
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
Recommend
liuyiding | We have carefully selected several similar problems for you: 6521 6520 6519 6518 6517
Statistic | Submit | Discuss | Note
这道题和 宣传墙那题一样,但是数据加强了,所以要用矩阵乘法优化
递推式子https://blog.csdn.net/weixin_43731933/article/details/90237090
系数矩阵为
1 1 0 1 1 1
1 0 0 1 0 0
0 0 0 0 1 0
1 1 0 0 0 0
1 0 1 0 0 0
1 0 0 0 0 0
#include<bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
typedef long long ll;
struct M {
ll m[10][10];
M()
{
memset(m, 0, sizeof m);
}
};
M mul(M a, M b)
{
M ans;
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
{
for (int k = 0; k < 6; k++)
ans.m[i][j] = (ans.m[i][j] + a.m[i][k] * b.m[k][j] % mod) % mod;
}
return ans;
}
void fpow(ll n)
{
M a, b;
a.m[0][0] = a.m[0][1] = a.m[0][3] = a.m[0][4] = a.m[0][5] = 1;
a.m[1][0] = a.m[1][3] = 1;
a.m[2][4] = 1;
a.m[3][0] = a.m[3][1] = 1;
a.m[4][0] = a.m[4][2] = 1;
a.m[5][0] = 1;
for (int i = 0; i < 6; i++)
b.m[i][i] = 1;
while (n)
{
if (n & 1)
b = mul(b, a);
n >>= 1;
a = mul(a, a);
}
cout << b.m[0][0] << endl;
}
int main(){
ll n;
while (cin >> n)
fpow(n);
return 0;
}