-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0509_fib.cc
151 lines (141 loc) · 2.38 KB
/
Problem_0509_fib.cc
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <vector>
using namespace std;
class Solution
{
public:
// 递归
int fib1(int n) { return f1(n); }
int f1(int i)
{
if (i == 0)
{
return 0;
}
else if (i == 1)
{
return 1;
}
else
{
return f1(i - 1) + f1(i - 2);
}
}
// 记忆化搜索
int fib2(int n)
{
vector<int> dp(n + 1, -1);
return f2(n, dp);
}
int f2(int i, vector<int>& dp)
{
if (i == 0)
{
return 0;
}
if (i == 1)
{
return 1;
}
if (dp[i] != -1)
{
return dp[i];
}
int ans = f2(i - 1, dp) + f2(i - 2, dp);
dp[i] = ans;
return ans;
}
// 动态规划
int fib3(int n)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
vector<int> dp(n + 1);
dp[1] = 1;
for (int i = 2; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
// 动态规划,空间优化
int fib4(int n)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
int lastLast = 0, last = 1;
for (int i = 2, cur; i <= n; i++)
{
cur = lastLast + last;
lastLast = last;
last = cur;
}
return last;
}
// 最优解,矩阵快速幂,O(log(n))
int fib5(int n)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
vector<vector<int>> start = {{1, 0}};
vector<vector<int>> base = {{1, 1}, {1, 0}};
vector<vector<int>> ans = multiply(start, power(base, n - 1));
return ans[0][0];
}
// 矩阵相乘
// a的列数一定要等于b的行数
vector<vector<int>> multiply(vector<vector<int>>& a, vector<vector<int>> b)
{
int n = a.size();
int m = b[0].size();
int k = a[0].size();
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
for (int c = 0; c < k; c++)
{
ans[i][j] += a[i][c] * b[c][j];
}
}
}
return ans;
}
// 矩阵快速幂
vector<vector<int>> power(vector<vector<int>>& m, int p)
{
int n = m.size();
vector<vector<int>> ans(n, vector<int>(n));
for (int i = 0; i < n; i++)
{
ans[i][i] = 1;
}
for (; p != 0; p >>= 1)
{
if (p & 1)
{
ans = multiply(ans, m);
}
m = multiply(m, m);
}
return ans;
}
};