Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add solutions to lc problem: No.1137 #1618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 1 commit into main from dev
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add solutions to lc problem: no.1137
No.1137.N-th Tribonacci Number
  • Loading branch information
yanglbme committed Sep 13, 2023
commit 70bbcd7ea64b5b557b394d55d258b7d05f878970
279 changes: 279 additions & 0 deletions solution/1100-1199/1137.N-th Tribonacci Number/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ T_4 = 1 +たす 1 +たす 2 = 4

时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为给定的整数。

**方法二:矩阵快速幂加速递推**

我们设 $Tib(n)$ 表示一个 1ドル \times 3$ 的矩阵 $\begin{bmatrix} T_n & T_{n - 1} & T_{n - 2} \end{bmatrix},ドル其中 $T_n,ドル $T_{n - 1}$ 和 $T_{n - 2}$ 分别表示第 $n$ 个、第 $n - 1$ 个和第 $n - 2$ 个泰波那契数。

我们希望根据 $Tib(n-1) = \begin{bmatrix} T_{n - 1} & T_{n - 2} & T_{n - 3} \end{bmatrix}$ 推出 $Tib(n)$。也即是说,我们需要一个矩阵 $base,ドル使得 $Tib(n - 1) \times base = Tib(n),ドル即:

$$
\begin{bmatrix}
T_{n - 1} & T_{n - 2} & T_{n - 3}
\end{bmatrix} \times base = \begin{bmatrix} T_n & T_{n - 1} & T_{n - 2} \end{bmatrix}
$$

由于 $T_n = T_{n - 1} + T_{n - 2} + T_{n - 3},ドル所以矩阵 $base$ 为:

$$
\begin{bmatrix}
1 & 1 & 0 \\
1 & 0 & 1 \\
1 & 0 & 0
\end{bmatrix}
$$

我们定义初始矩阵 $res = \begin{bmatrix} 1 & 1 & 0 \end{bmatrix},ドル那么 $T_n$ 等于 $res$ 乘以 $base^{n - 3}$ 的结果矩阵中所有元素之和。使用矩阵快速幂求解即可。

时间复杂度 $O(\log n),ドル空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand All @@ -67,6 +93,35 @@ class Solution:
return a
```

```python
class Solution:
def tribonacci(self, n: int) -> int:
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
m, n = len(a), len(b[0])
c = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
for k in range(len(a[0])):
c[i][j] = c[i][j] + a[i][k] * b[k][j]
return c

def pow(a: List[List[int]], n: int) -> List[List[int]]:
res = [[1, 1, 0]]
while n:
if n & 1:
res = mul(res, a)
n >>= 1
a = mul(a, a)
return res

if n == 0:
return 0
if n < 3:
return 1
a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
return sum(pow(a, n - 3)[0])
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->
Expand All @@ -86,6 +141,51 @@ class Solution {
}
```

```java
class Solution {
public int tribonacci(int n) {
if (n == 0) {
return 0;
}
if (n < 3) {
return 1;
}
int[][] a = {{1, 1, 0}, {1, 0, 1}, {1, 0, 0}};
int[][] res = pow(a, n - 3);
int ans = 0;
for (int x : res[0]) {
ans += x;
}
return ans;
}

private int[][] mul(int[][] a, int[][] b) {
int m = a.length, n = b[0].length;
int[][] c = new int[m][n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < b.length; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}

private int[][] pow(int[][] a, int n) {
int[][] res = {{1, 1, 0}};
while (n > 0) {
if ((n & 1) == 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
}
```

### **C++**

```cpp
Expand All @@ -104,6 +204,50 @@ public:
};
```

```cpp
class Solution {
public:
int tribonacci(int n) {
if (n == 0) {
return 0;
}
if (n < 3) {
return 1;
}
vector<vector<ll>> a = {{1, 1, 0}, {1, 0, 1}, {1, 0, 0}};
vector<vector<ll>> res = pow(a, n - 3);
return accumulate(res[0].begin(), res[0].end(), 0);
}

private:
using ll = long long;
vector<vector<ll>> mul(vector<vector<ll>>& a, vector<vector<ll>>& b) {
int m = a.size(), n = b[0].size();
vector<vector<ll>> c(m, vector<ll>(n));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < b.size(); ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}

vector<vector<ll>> pow(vector<vector<ll>>& a, int n) {
vector<vector<ll>> res = {{1, 1, 0}};
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
};
```

### **Go**

```go
Expand All @@ -116,6 +260,51 @@ func tribonacci(n int) int {
}
```

```go
func tribonacci(n int) (ans int) {
if n == 0 {
return 0
}
if n < 3 {
return 1
}
a := [][]int{{1, 1, 0}, {1, 0, 1}, {1, 0, 0}}
res := pow(a, n-3)
for _, x := range res[0] {
ans += x
}
return
}

func mul(a, b [][]int) [][]int {
m, n := len(a), len(b[0])
c := make([][]int, m)
for i := range c {
c[i] = make([]int, n)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
for k := 0; k < len(b); k++ {
c[i][j] += a[i][k] * b[k][j]
}
}
}
return c
}

func pow(a [][]int, n int) [][]int {
res := [][]int{{1, 1, 0}}
for n > 0 {
if n&1 == 1 {
res = mul(res, a)
}
a = mul(a, a)
n >>= 1
}
return res
}
```

### **JavaScript**

```js
Expand All @@ -137,6 +326,96 @@ var tribonacci = function (n) {
};
```

```js
/**
* @param {number} n
* @return {number}
*/
var tribonacci = function (n) {
if (n === 0) {
return 0;
}
if (n < 3) {
return 1;
}
const a = [
[1, 1, 0],
[1, 0, 1],
[1, 0, 0],
];
return pow(a, n - 3)[0].reduce((a, b) => a + b);
};

function mul(a, b) {
const [m, n] = [a.length, b[0].length];
const c = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
for (let k = 0; k < b.length; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}

function pow(a, n) {
let res = [[1, 1, 0]];
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
```

### **TypeScript**

```ts
function tribonacci(n: number): number {
if (n === 0) {
return 0;
}
if (n < 3) {
return 1;
}
const a = [
[1, 1, 0],
[1, 0, 1],
[1, 0, 0],
];
return pow(a, n - 3)[0].reduce((a, b) => a + b);
}

function mul(a: number[][], b: number[][]): number[][] {
const [m, n] = [a.length, b[0].length];
const c = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
for (let k = 0; k < b.length; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}

function pow(a: number[][], n: number): number[][] {
let res = [[1, 1, 0]];
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
```

### **PHP**

```php
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /