ARST打卡第118周[118/521]

Algorithm

lc1137_第N个泰波那契数

思路

  1. 想到的是直接本地运算,然后打表
  2. 打表找规律,然后直接用计算式做

然后发现是要用矩阵快速幂(看题解答案吧)

代码

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
class Solution {
public:
int tribonacci(int n) {
if (n == 0) {
return 0;
}
if (n <= 2) {
return 1;
}
vector<vector<long>> q = {{1, 1, 1}, {1, 0, 0}, {0, 1, 0}};
vector<vector<long>> res = pow(q, n);
return res[0][2];
}

vector<vector<long>> pow(vector<vector<long>>& a, long n) {
vector<vector<long>> ret = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
while (n > 0) {
if ((n & 1) == 1) {
ret = multiply(ret, a);
}
n >>= 1;
a = multiply(a, a);
}
return ret;
}

vector<vector<long>> multiply(vector<vector<long>>& a, vector<vector<long>>& b) {
vector<vector<long>> c(3, vector<long>(3));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
}
}
return c;
}
};

Review

SMB2: what is MsFteWds and Srvsvc files?

Named Pipes

Tips

如何在python中隐藏密码

Git分支-变基

Share

python变量是否在类中-普通常规类也可以判断