-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_exponential.cpp
More file actions
49 lines (44 loc) · 1.04 KB
/
matrix_exponential.cpp
File metadata and controls
49 lines (44 loc) · 1.04 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
#include <bits/stdc++.h>
using namespace std;
const int mod = 1234;
struct matrix {
int row , col;
int mat[50][50];
matrix () {}
matrix operator * (const matrix &b) const {
assert (col == b.row);
matrix res;
res.row = row;
res.col = b.col;
for (int i = 0; i < res.row; i++) {
for (int j = 0; j < res.col; j++) {
int sum = 0;
for (int k = 0; k < col; k++) {
sum += mat[i][k] * b.mat[k][j];
sum %= mod;
}
res.mat[i][j] = sum;
}
}
return res;
}
};
matrix expo (matrix a , int p) {
assert (p >= 1);
if (p == 1) return a;
if (p % 2 == 0) {
matrix ret = expo (a , p / 2);
return ret * ret;
}
else return a * expo (a , p - 1);
}
int main () {
matrix bm;
bm.row = bm.col = 2;
bm.mat[0][0] = bm.mat[0][1] = 1;
bm.mat[1][0] = 1; bm.mat[1][1] = 0;
int n; cin >> n;
matrix ans = expo (bm , n - 2);
cout << (ans.mat[0][0] + ans.mat[0][1]) % mod << endl;
main ();
}