-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix Power.cpp
More file actions
57 lines (49 loc) · 1.26 KB
/
Matrix Power.cpp
File metadata and controls
57 lines (49 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define sz(c) int(c.size())
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define vlong long long
const int md = 1e9 + 7;
typedef vector<long long> vl;
typedef vector<vl> matrix;
matrix initial(int n, int m) {
return matrix(n, vl(m, 0));
}
matrix identity(int n) {
matrix ret = initial(n, n);
for (int i = 0; i < n; i++)
ret[i][i] = 1;
return ret;
}
matrix multiply(const matrix &x, const matrix &y) {
matrix ret = initial(x.size(), y[0].size());
for (int i = 0; i < sz(x); i++) {
for (int k = 0; k < sz(x[0]); k++) {
for (int j = 0; j < sz(y[0]); j++) {
ret[i][j] += (((x[i][k] * y[k][j]) % md) + md) % md;
ret[i][j] = ((ret[i][j] % md) + md) % md;
}
}
}
return ret;
}
matrix pow(const matrix &x, long long k) {
if (k == 0)
return identity(sz(x));
if (k & 1)
return multiply(x, pow(x, k - 1));
return pow(multiply(x, x), k / 2);
}
long long fib(int n) {
if (n < 3)
return 1;
matrix init = initial(1, 2), t = initial(2, 2);
t[0][1] = t[1][1] = t[1][0] = 1;
init[0][0] = init[0][1] = 1;
matrix moves = pow(t, n - 2);
matrix ans = multiply(init, moves);
return ans[0][1];
}