Covering
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 414 Accepted Submission(s): 206
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
Source
题意:有1*2和2*1的方格,问有多少种方法能将4*n给矩形铺满
解题思路:这题和POJ3420-Quad Tiling一样,南宁邀请赛居然出了原题,具体可以看http://blog.csdn.net/a664607530/article/details/77619554
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
LL n;
struct Matrix
{
LL v[6][6];
Matrix()
{
memset(v, 0, sizeof v);
}
} dan;
Matrix mul(Matrix a, Matrix b, int d)
{
Matrix ans;
for (int i = 0; i < d; i++)
for (int j = 0; j < d; j++)
for (int k = 0; k < d; k++)
ans.v[i][j] = (ans.v[i][j] + a.v[i][k] * b.v[k][j] % mod) % mod;
return ans;
}
Matrix pow(Matrix a, LL k, int d)
{
Matrix ans = dan;
while (k)
{
if (k & 1) ans = mul(ans, a, d);
k >>= 1;
a = mul(a, a, d);
}
return ans;
}
int main()
{
while (~scanf("%lld", &n))
{
dan.v[0][0] = dan.v[0][1] = dan.v[0][2] = 1;
dan.v[0][3] = 2;
Matrix a, ans;
a.v[0][0] = a.v[0][1] = a.v[0][2] = 1, a.v[0][3] = 2;
a.v[1][0] = 1;
a.v[2][0] = a.v[2][4] = 1;
a.v[3][0] = a.v[3][3] = 1;
a.v[4][2] = 1;
ans = pow(a, n - 1, 5);
printf("%lld\n", ans.v[0][0]);
}
return 0;
}