HDU-6185-Covering(推递推式+矩阵快速幂)
Covering
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3078 Accepted Submission(s): 1117
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
题意:4xn的地面,用1x2或者2x1的地毯自由组合铺满,有几种方案(答案mod 1e9+7)
解题思路:(草稿纸冲冲冲)
有了递推式,矩阵快速幂就好了
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef unsigned long long ull; 5 #define INF 0X3f3f3f3f 6 const ll MAXN = 4; 7 const ll mod = 1e9 + 7; 8 //矩阵的大小 模数 9 ll n; 10 struct MAT 11 { 12 ll mat[MAXN][MAXN]; 13 MAT operator*(const MAT &a) const 14 { 15 //重载矩阵乘法 16 MAT b; 17 memset(b.mat, 0, sizeof(b.mat)); 18 for (int i = 0; i < MAXN; i++) 19 { 20 for (int j = 0; j < MAXN; j++) 21 { 22 for (int k = 0; k < MAXN; k++) 23 b.mat[i][j] = (b.mat[i][j] + mat[i][k] * a.mat[k][j]); 24 b.mat[i][j] += mod; 25 b.mat[i][j] %= mod; 26 } 27 } 28 return b; 29 } 30 } start, ans; 31 MAT Mqpow(MAT base, ll b) 32 { 33 MAT r; 34 memset(r.mat, 0, sizeof(r.mat)); 35 r.mat[0][0] = 1, r.mat[1][0] = 5, r.mat[2][0] = 11, r.mat[3][0] = 36; 36 //初始状态 37 while (b) 38 { 39 if (b & 1) 40 r = base * r; 41 base = base * base; 42 b >>= 1; 43 } 44 return r; 45 } 46 int main() 47 { 48 49 start.mat[0][0] = 0, start.mat[0][1] = 1, start.mat[0][2] = 0, start.mat[0][3] = 0; 50 start.mat[1][0] = 0, start.mat[1][1] = 0, start.mat[1][2] = 1, start.mat[1][3] = 0; 51 start.mat[2][0] = 0, start.mat[2][1] = 0, start.mat[2][2] = 0, start.mat[2][3] = 1; 52 start.mat[3][0] = -1, start.mat[3][1] = 1, start.mat[3][2] = 5, start.mat[3][3] = 1; 53 //建立转移矩阵 54 ll f[5] = {0, 1, 5, 11, 36}; 55 while (~scanf("%lld", &n)) 56 { 57 if (n <= 4) 58 printf("%lld\n", f[n] % mod); 59 else 60 printf("%lld\n", Mqpow(start, n - 4).mat[3][0]); 61 } 62 return 0; 63 }