-
Notifications
You must be signed in to change notification settings - Fork 0
/
1722_Fibonacci_Numbers.cpp
85 lines (75 loc) · 2.01 KB
/
1722_Fibonacci_Numbers.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
using namespace std;
#define tcT template <class T
#define tcTU tcT, class U
template<typename... T>
void see(T&... args) { ((cin >> args), ...);}
template<typename... T>
void put(T&&... args) { ((cout << args << " "), ...);}
template<typename... T>
void putl(T&&... args) { ((cout << args << " "), ...); cout<<'\n';}
#define int long long
#define fi first
#define se second
#define str string
#define all(a) a.begin(), a.end()
#define pb push_back
#define ins insert
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define fu(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define f0u(i,b) for(int i=0;i<b;++i)
#define f0d(i,b) for(int i=(b)-1;i>=0;--i)
#define FU(i,a,b,x) for (int i=a;i<=b;i+=x)
#define FD(i,a,b,x) for (int i=a;i>=b;i-=x)
#define trav(a, x) for (auto &a : x)
#define pii pair<int,int>
const int MOD=1e9+7;
const int N=1e5;
const signed dx[4] = {1, 0, -1, 0},
dy[4] = {0, 1, 0, -1};
void power(int F[2][2],int n);
void multiply(int F[2][2], int M[2][2]);
int fibo(int n){
if (n==0) return 0;
int F[2][2]={{1 , 1},
{1 , 0}};
power(F,n);
return F[0][1]%MOD;
}
void power(int F[2][2], int n) {
if (n==1 || n==0) return;
int M[2][2]={{1 , 1}
,{1 , 0}};
power(F,n>>1);
multiply(F,F);
if (n&1) {
multiply(F,M);
}
}
void multiply(int F[2][2], int M[2][2]) {
int x = ( (F[0][0]*M[0][0])%MOD + (F[0][1]*M[1][0]) %MOD )%MOD;
int y = ( (F[0][0]*M[0][1])%MOD + (F[0][1]*M[1][1]) %MOD )%MOD;
int z = ( (F[1][0]*M[0][0])%MOD + (F[1][1]*M[1][0]) %MOD )%MOD;
int w = ( (F[1][0]*M[0][1])%MOD + (F[1][1]*M[1][1]) %MOD )%MOD;
F[0][0] = x; F[0][1] = y;
F[1][0] = z; F[1][1] = w;
}
void solve() {
int n;
see(n);
int res=fibo(n);
putl(res);
}
signed main(signed argc, char *argv[]) {
ios_base::sync_with_stdio(NULL);
cin.tie(nullptr); cout.tie(nullptr);
int t=1;
//see(t);
while (t--) {
solve();
}
return 0;
}