-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrix.cpp
More file actions
58 lines (57 loc) · 1.44 KB
/
Matrix.cpp
File metadata and controls
58 lines (57 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
// Matrix exponentiation
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
using ll = long long;
struct mat {
vector<vector<long long>>a;
int x;
mat() {
a.resize(2,vector<ll>(2,0));
x = 2;
}
mat(int n) {
a.resize(n,vector<ll>(n,0));
x = n;
}
mat operator * ( const mat &b ) const {
mat ret(x);
ret.x = x;
for( int i = 0; i < x; ++i )
for( int j = 0; j < x; ++j )
for( int k = 0; k < x; ++k )
ret.a[i][j] = (( ret.a[i][j] + a[i][k] * b.a[k][j] ) % mod + mod) % mod;
return ret;
}
mat operator + ( const mat &b ) const {
mat ret(x);
ret.x = x;
for( int i = 0; i < x; ++i )
for( int j = 0; j < 1; ++j )
for( int k = 0; k < x; ++k )
ret.a[i][j] = (( ret.a[i][j] + a[i][k] * b.a[k][j] ) % mod + mod) % mod;
return ret;
}
};
int main() {
// Matrix power fib
//{1,1,2,3,5,8};
ll n;
while (cin >> n) {
mat ans(2);
mat mul(2);
ans.a[0][0] = 1; // fib 1
ans.a[1][0] = 1; // fib 2
ll left = n - 1;
mul.a[0][1] = 1;
mul.a[1][0] = mul.a[1][1] = 1;
while (left) {
if (left & 1) {
ans = mul + ans;
}
mul = mul * mul;
left >>= 1;
}
cout << ans.a[0][0] << '\n'; // fib n
}
}