-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrix.cpp
More file actions
61 lines (55 loc) · 1.44 KB
/
Copy pathMatrix.cpp
File metadata and controls
61 lines (55 loc) · 1.44 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
58
59
60
61
// Matrix exponentiation (square matrix, modular)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7; // change to 998244353 if needed
struct mat {
int n;
vector<vector<ll>> a;
mat(int n) : n(n), a(n, vector<ll>(n, 0)) {}
static mat I(int n) {
mat r(n);
for (int i = 0; i < n; i++) r.a[i][i] = 1;
return r;
}
mat operator*(const mat& b) const {
mat r(n);
for (int i = 0; i < n; i++) {
for (int k = 0; k < n; k++) {
if (!a[i][k]) continue;
ll aik = a[i][k];
for (int j = 0; j < n; j++) {
r.a[i][j] = (r.a[i][j] + aik * b.a[k][j]) % mod;
}
}
}
return r;
}
mat operator+(const mat& b) const {
mat r(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) r.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
return r;
}
mat pow(ll p) const {
mat r = I(n), base = *this;
while (p) {
if (p & 1) r = r * base;
base = base * base;
p >>= 1;
}
return r;
}
};
int main() {
// Fibonacci: F(1) = F(2) = 1
// M = [[1, 1],
// [1, 0]]
// M^(n-1)[0][0] = F(n) for n >= 1
ll n;
while (cin >> n) {
mat M(2);
M.a[0][0] = M.a[0][1] = M.a[1][0] = 1;
cout << M.pow(n - 1).a[0][0] << '\n';
}
}