Covering
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total
Submission(s): 0 Accepted Submission(s): 0
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
题解:这个题目不是很好写 ,但是有很多人AC得特别快 大神的脚步完全跟不上
直接找规律 想公式 想了特别就都没有想出来
还是实验室有人使用dfs求了前面几个数的答案 然后使用循环求出去套出了规律
不过找到了规律 数据量也还是很大 10的18次方
所以这里还是得使用矩阵快速幂 才行
1 矩阵的算法 比赛中比较常见 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <algorithm> 7 #define ll long long 8 using namespace std; 9 const int maxn = 4; 10 ll Matrixsize = 4, mod = int(1e9)+7, n; 11 struct Matrix { 12 ll m[maxn][maxn]; 13 Matrix(ll i = 0) { 14 memset(m, 0, sizeof m); 15 if (i == 1) 16 for (ll I = 0; I < Matrixsize; I++) m[I][I] = 1; 17 } 18 Matrix operator * (const Matrix tmp) const { 19 Matrix ret; 20 long long x; 21 for(ll i=0 ; i<Matrixsize ; i++) 22 for(ll j=0 ; j<Matrixsize ; j++) { 23 x=0; 24 for(ll k=0 ; k<Matrixsize ; k++) 25 x+=(m[i][k] * tmp.m[k][j] + mod) % mod; 26 ret.m[i][j] = int(x % mod); 27 } 28 return ret; 29 } 30 Matrix qpow(long long n) { 31 Matrix ret = 1, tmp = *this; 32 while (n > 0) { 33 if (bool(n & 1)) ret = ret * tmp; 34 tmp = tmp * tmp; 35 n >>= 1; 36 } 37 return ret; 38 } 39 }; 40 41 int main() { 42 Matrix base1 = 0, base2 = 0; 43 base1.m[0][0] = base1.m[0][2] = base1.m[1][0] = base1.m[2][1] = base1.m[3][2] = 1; 44 base1.m[0][3] = -1, base1.m[0][1] = 5, base2.m[0][0] = 1; 45 base2.m[1][0] = 0, base2.m[2][0] = 1, base2.m[3][0] = 1; 46 while(~scanf("%lld",&n)) printf("%lld ",(base1.qpow(n)*base2).m[0][0]); 47 return 0; 48 }