斐波那契数列

Desicription

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
n<=39

Solution

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
class Solution {
private:
typedef vector<vector<long long>> mat;
mat Mul(mat& a, mat& b) {
mat c(a.size(), vector<long long>(b[0].size(), 0));
for(int i = 0; i < a.size(); i++) {
for(int k = 0; k < a[0].size(); k++) {
for(int j = 0; j < b[0].size(); j++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
mat Pow(mat a, int n) {
mat b(a.size(), vector<long long>(a[0].size(), 0));
for(int i = 0; i < b.size(); i++) {
b[i][i] = 1;
}
while(n) {
if(n & 1) {
b = Mul(b, a);
}
a = Mul(a, a);
n >>= 1;
}
return b;
}
public:
long long Fibonacci(int n) {
mat a = {
{1, 1},
{1, 0}
};
a = Pow(a, n);
return a[1][0];
}
};