-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBCfibo.cpp
More file actions
62 lines (58 loc) · 1.1 KB
/
BCfibo.cpp
File metadata and controls
62 lines (58 loc) · 1.1 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;
#define For(i, a, b) for(int i = a; i <= b; i++)
#define ll long long
ll const base = 1e9 + 7;
const ll size = 2;
struct Matrix{
ll a[size + 1][size + 1];
} I, a;
ll n, res;
// nhan ma tran
Matrix nhan(Matrix A, Matrix B){
Matrix C;
For(i,1,size){
For(j,1,size){
C.a[i][j] = 0;
For(k,1,size){
C.a[i][j] += (A.a[i][k] * B.a[k][j]) % base;
}
}
}
return C;
}
//luy thua ma tran
Matrix luythua(Matrix A, ll p){
Matrix RES;
if(p == 0){
return I; // ma tran don vi
}
if(p == 1) return A;
RES = luythua(A, p/2);
RES = nhan(RES, RES);
if(p%2 == 1) RES = nhan(RES, A);
return RES;
}
// ham khoi tao ban dau
void init(){
// ma tran don vi
I.a[1][1] = 1; I.a[1][2] = 0;
I.a[2][1] = 0; I.a[2][2] = 1;
//ma tran ban dau
a.a[1][1] = 1; a.a[1][2] = 1;
a.a[2][1] = 1; a.a[2][2] = 0;
}
void solve(){
cin >> n;
if(n == 0 || n == 1) cout << 1;
else {
ll f0 = 0, f1 = 1;
a = luythua(a, n - 1);
res = a.a[1][1] * f1 + a.a[1][2] * f0;
cout << res;
}
}
main(){
init();
solve();
}