-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFibonacci(MatrixExponentiation).cpp
More file actions
80 lines (71 loc) · 1.62 KB
/
Fibonacci(MatrixExponentiation).cpp
File metadata and controls
80 lines (71 loc) · 1.62 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define LD double
#define gc getchar_unlocked
#define pc putchar_unlocked
#define MOD 1000000007LL
#define MAXN 1000000
#define bitcount __builtin_popcount
#define INF 2000000000
#define EPS 1e-9
#define PI 3.14159265359
#define DEBUG 1
#define read(X) scanf("%lld",&X)
#define write(X) printf("%lld\n",&X)
template<typename T>T absll(T X)
{
if(X<0)
return -1*X;
else
return X;
}
void MatrixMultiplication(LL F[2][2],LL M[2][2])
{
LL x=((F[0][0]*M[0][0])%MOD+(F[0][1]*M[1][0])%MOD)%MOD;
LL y=((F[0][0]*M[0][1])%MOD+(F[0][1]*M[1][1])%MOD)%MOD;
LL z=((F[1][0]*M[0][0])%MOD+(F[1][1]*M[1][0])%MOD)%MOD;
LL w=((F[1][0]*M[0][1])%MOD+(F[1][1]*M[1][1])%MOD)%MOD;
F[0][0]=x%MOD;
F[0][1]=y%MOD;
F[1][0]=z%MOD;
F[1][1]=w%MOD;
}
void MatrixExponentiation(LL F[2][2],LL N)
{
if(N==0||N==1)
{
return;
}
LL M[2][2]={{1,1},{1,0}};
MatrixExponentiation(F,N/2);
MatrixMultiplication(F,F);
if(N%2!=0)
{
MatrixMultiplication(F,M);
}
}
LL Fibonacci(LL N)
{
LL F[2][2]={{1,1},{1,0}};
if(N==0)
{
return 0;
}
MatrixExponentiation(F,N-1);
return F[0][0]%MOD;
}
int main()
{
//std::clock_t start;
//double duration;
//start = std::clock();
//freopen("input.in","r",stdin);//redirects standard input
//freopen("output.out","w",stdout);//redirects standard output
//duration=(clock()-start)/(double)CLOCKS_PER_SEC;
//printf("\n\nDuration :- %0.9lf s",duration);
return 0;
}