-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFibonacci Matrix.cpp
70 lines (59 loc) · 1.34 KB
/
Fibonacci Matrix.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
//
// main.cpp
// practice
//
// Created by Mahmud on 04/04/18.
// Copyright © 2018 Mahmud. All rights reserved.
//
/// Calculating Fibonacci(N) in O(log(N)) by matrix exponentiation
/// Note, here fib(0) = fib(1) = 1
#include <iostream>
using namespace std;
const int mod = 1000000007;
struct matrix{
int a[2][2];
matrix() {
for(int i = 0; i < 2; i ++)
for(int j = 0; j < 2; j ++)
a[i][j] = 0;
}
};
matrix mul(matrix a, matrix b){
matrix c;
for(int i = 0; i < 2; i ++)
for(int j = 0; j < 2; j ++)
c.a[i][j] = 0;
for(int i = 0; i < 2; i ++)
for(int j = 0; j < 2; j ++)
for(int k = 0; k < 2; k ++)
c.a[i][j] = (c.a[i][j] + 1LL * a.a[i][k] * b.a[k][j] % mod) % mod;
return c;
}
matrix power(matrix a, long long b){
if(b == 1)
return a;
if(b & 1)
return mul(power(a, b - 1), a);
matrix half = power(a, b >> 1);
return mul(half, half);
}
int T;
long long N;
matrix F;
int main() {
cin >> T;
while (T --) {
cin >> N;
if (N < 2) {
cout << 1 << endl;
continue;
}
F.a[0][0] = 1;
F.a[0][1] = 1;
F.a[1][0] = 1;
F.a[1][1] = 0;
F = power(F, N);
cout << F.a[0][0] << endl;
}
return 0;
}