Skip to content

Commit 8e238e1

Browse files
authored
Update n-th-tribonacci-number.cpp
1 parent 61ef2f5 commit 8e238e1

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

C++/n-th-tribonacci-number.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
class Solution {
55
public:
66
int tribonacci(int n) {
7-
vector<vector<int>> T = {{1, 1, 1},
8-
{1, 0, 0},
9-
{0, 1, 0}};
10-
return matrixExpo(T, n)[1][0];
7+
vector<vector<int>> T = {{1, 1, 0},
8+
{1, 0, 1},
9+
{1, 0, 0}};
10+
return matrixMult({{1, 0, 0}}, matrixExpo(T, n))[0][1]; // [a1, a0, a(-1)] * T^n
1111
}
1212

1313
private:
@@ -28,7 +28,7 @@ class Solution {
2828
}
2929

3030
vector<vector<int>> matrixMult(const vector<vector<int>>& A, const vector<vector<int>>& B) {
31-
vector<vector<int>> result(A.size(), vector<int>(A.size()));
31+
vector<vector<int>> result(A.size(), vector<int>(B[0].size()));
3232
for (int i = 0; i < A.size(); ++i) {
3333
for (int j = 0; j < B[0].size(); ++j) {
3434
int64_t entry = 0;

0 commit comments

Comments
 (0)