forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main4.cpp
57 lines (43 loc) · 1.35 KB
/
main4.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
/// Source : https://leetcode.com/problems/fibonacci-number/
/// Author : liuyubobobo
/// Time : 2019-01-09
#include <iostream>
#include <vector>
using namespace std;
/// Binets Method
/// | F(n+1) F(n) | = | 1 1 |^n
/// | F(n) F(n-1) | | 1 0 |
///
/// Time Complexity: O(logn)
/// Space Complexity: O(logn)
class Solution {
public:
int fib(int N) {
if(N <= 1) return N;
vector<vector<int>> base = {{1, 1},
{1, 0}};
return matrix_pow(base, N - 1)[0][0];
}
private:
vector<vector<int>> matrix_pow(const vector<vector<int>> &m, int n) {
if (n == 1)
return m;
vector<vector<int>> t = matrix_pow(m, n / 2);
vector<vector<int>> res = matrix_multiply(t, t);
if (n % 2)
return matrix_multiply(res, m);
return res;
}
vector<vector<int>> matrix_multiply(const vector<vector<int>> &m1,
const vector<vector<int>> &m2) {
vector<vector<int>> res(2, vector<int>(2, 0));
res[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0];
res[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1];
res[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0];
res[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1];
return res;
}
};
int main() {
return 0;
}