-
Notifications
You must be signed in to change notification settings - Fork 522
/
Copy pathCode04_TribonacciNumber.java
65 lines (60 loc) · 1.32 KB
/
Code04_TribonacciNumber.java
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
package class098;
// 第n个泰波那契数
// t(0) = 0, t(1) = 1, t(2) = 1
// t(i) = t(i-1) + t(i-2) + t(i-3)
// 求t(n)
// 测试链接 : https://leetcode.cn/problems/n-th-tribonacci-number/
// 这个测试的数据量太小,并且不牵扯取模的事情
// 所以矩阵快速幂看不出优势
public class Code04_TribonacciNumber {
public static int tribonacci(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
if (n == 2) {
return 1;
}
int[][] start = { { 1, 1, 0 } };
int[][] base = {
{ 1, 1, 0 },
{ 1, 0, 1 },
{ 1, 0, 0 }
};
int[][] ans = multiply(start, power(base, n - 2));
return ans[0][0];
}
// 矩阵相乘
// a的列数一定要等于b的行数
public static int[][] multiply(int[][] a, int[][] b) {
int n = a.length;
int m = b[0].length;
int k = a[0].length;
int[][] ans = new int[n][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;
}
// 矩阵快速幂
public static int[][] power(int[][] m, int p) {
int n = m.length;
int[][] ans = new int[n][n];
for (int i = 0; i < n; i++) {
ans[i][i] = 1;
}
for (; p != 0; p >>= 1) {
if ((p & 1) != 0) {
ans = multiply(ans, m);
}
m = multiply(m, m);
}
return ans;
}
}