[HIHO1143]骨牌覆盖问题·一(矩阵快速幂,递推)
题目链接:http://hihocoder.com/problemset/problem/1143
这个递推还是很经典的,结果是斐波那契数列。f(i) = f(i-1) + f(i-2)。数据范围太大了,应该用快速幂加速下。
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rll(a) scanf("%lld", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 53 #define lrt rt << 1 54 #define rrt rt << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 #define lowbit(x) x & (-x) 58 #define onenum(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef pair<LL, LL> pll; 65 typedef map<string, int> msi; 66 typedef vector<int> vi; 67 typedef vector<LL> vl; 68 typedef vector<vl> vvl; 69 typedef vector<bool> vb; 70 71 const int mod = 19999997; 72 const int maxn = 5; 73 LL n; 74 75 typedef struct Matrix { 76 LL m[maxn][maxn]; 77 int r; 78 int c; 79 Matrix(){ 80 r = c = 0; 81 memset(m, 0, sizeof(m)); 82 } 83 } Matrix; 84 85 Matrix mul(Matrix m1, Matrix m2, int mod) { 86 Matrix ans = Matrix(); 87 ans.r = m1.r; 88 ans.c = m2.c; 89 for(int i = 1; i <= m1.r; i++) { 90 for(int j = 1; j <= m2.r; j++) { 91 for(int k = 1; k <= m2.c; k++) { 92 if(m2.m[j][k] == 0) continue; 93 ans.m[i][k] = ((ans.m[i][k] + m1.m[i][j] * m2.m[j][k] % mod) % mod) % mod; 94 } 95 } 96 } 97 return ans; 98 } 99 100 Matrix quickmul(Matrix m, int n, int mod) { 101 Matrix ans = Matrix(); 102 for(int i = 1; i <= m.r; i++) { 103 ans.m[i][i] = 1; 104 } 105 ans.r = m.r; 106 ans.c = m.c; 107 while(n) { 108 if(n & 1) { 109 ans = mul(m, ans, mod); 110 } 111 m = mul(m, m, mod); 112 n >>= 1; 113 } 114 return ans; 115 } 116 117 int main() { 118 // FRead(); 119 while(cin >> n) { 120 Matrix p, q; 121 p.r = p.c = 2; 122 p.m[1][1] = 1; p.m[1][2] = 1; 123 p.m[2][1] = 1; p.m[2][2] = 0; 124 q.r = 2; q.c = 1; 125 if(n <= 2) { 126 cout << n << endl; 127 continue; 128 } 129 q = quickmul(p, n-1, mod); 130 cout << (q.m[1][1] + q.m[1][2]) % mod << endl; 131 } 132 RT 0; 133 }