-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix Fibonacci
More file actions
62 lines (55 loc) · 1.04 KB
/
Matrix Fibonacci
File metadata and controls
62 lines (55 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
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
struct matrix
{
long long x[2][2];
matrix()
{
memset(x , 0 , sizeof(x));
}
};
matrix operator * (const matrix& a, const matrix& b)
{
matrix c;
for (int i = 0; i < 2 ; ++i)
for(int j = 0 ; j < 2 ; ++j)
for(int k = 0 ; k < 2 ; ++k)
{
(c.x[i][j] += a.x[i][k] * b.x[k][j] % MOD) % MOD;
}
return c;
}
matrix Pow(const matrix& T , long long n)
{
if(n == 0)
{
matrix I;
for (int i = 0 ; i < 2 ; ++i)
{
I.x[i][i] = 1;
}
return I;
}
matrix tmp = Pow(T, n / 2);
tmp = tmp * tmp;
if(n & 1) tmp = tmp * T;
return tmp;
}
long long n;
main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
matrix a;
cin >> n;
if(n <= 1)
{
return cout << n,0;
}
matrix T;
T.x[0][0] = T.x[0][1] = T.x[1][0] = 1;
T = Pow(T, n - 1);
matrix u;
u.x[0][0] = 1;
matrix ans = T * u;
cout << ans.x[0][0];
}