-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacciOptimized.cpp
83 lines (49 loc) · 1.32 KB
/
fibonacciOptimized.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
#include <iostream>
#include <vector>
using namespace std;
#define mod 1000000007;
vector< vector<long long> > multiplicar( vector< vector<long long> > a , vector< vector<long long> > b){
vector< vector<long long> > temp( 2, vector<long long>(2) );
temp[0][0] = a[0][0]*b[0][0] + a[0][1]* b[1][0];
temp[0][1] = a[0][0]*b[0][1]+ a[0][1]* b[1][1];
temp[1][0] = a[1][0]*b[0][0]+ a[1][1]* b[1][0];
temp[1][1] = a[1][0]*b[0][1]+ a[1][1]* b[1][1];
return temp;
}
void sacarModulo(vector<vector<long long> > &m){
for(int i =0; i<m.size(); i++){
for(int j =0; j < m.size(); j++){
m[i][j]%=mod;
}
}
}
long long fib(long long n){
if(n == 0 ) return 0;
if(n == 1) return 1;
vector< vector<long long> > x(2, vector<long long>(2,1));
x[1][1] = 0;
vector< vector<long long> >result (2,vector<long long>(2,0));
result[0][0] =1;
result[1][1] =1;
n--;
while(n>0){
if(n%2 != 0){
result = multiplicar(result, x);
sacarModulo(result);
}
x = multiplicar(x,x);
sacarModulo(x);
n/=2;
}
return result[0][0];
}
int main(){
int t; cin >> t;
long long temp;
while(t--){
cin >> temp;
temp+=2;
cout << fib(temp)<< endl;
}
return 0;
}