Skip to content

Commit 3ceba83

Browse files
committed
modify code
1 parent 88269d1 commit 3ceba83

7 files changed

+164
-23
lines changed

Diff for: src/class098/Code02_FibonacciNumber.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ public static int fib2(int n) {
3131
if (n == 1) {
3232
return 1;
3333
}
34+
int[][] start = { { 1, 0 } };
3435
int[][] base = {
3536
{ 1, 1 },
3637
{ 1, 0 }
3738
};
38-
int[][] m = power(base, n - 1);
39-
return m[0][0];
39+
int[][] ans = multiply(start, power(base, n - 1));
40+
return ans[0][0];
4041
}
4142

4243
// 矩阵快速幂

Diff for: src/class098/Code03_ClimbingStairs.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ public static int climbStairs(int n) {
1717
if (n == 1) {
1818
return 1;
1919
}
20+
int[][] start = { { 1, 1 } };
2021
int[][] base = {
2122
{ 1, 1 },
2223
{ 1, 0 }
2324
};
24-
int[][] m = power(base, n - 1);
25-
return m[0][0] + m[1][0];
25+
int[][] ans = multiply(start, power(base, n - 1));
26+
return ans[0][0];
2627
}
2728

2829
// 矩阵快速幂

Diff for: src/class098/Code04_TribonacciNumber.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ public static int tribonacci(int n) {
1919
if (n == 2) {
2020
return 1;
2121
}
22+
int[][] start = { { 1, 1, 0 } };
2223
int[][] base = {
2324
{ 1, 1, 0 },
2425
{ 1, 0, 1 },
2526
{ 1, 0, 0 }
2627
};
27-
int[][] m = power(base, n - 2);
28-
return m[0][0] + m[1][0];
28+
int[][] ans = multiply(start, power(base, n - 2));
29+
return ans[0][0];
2930
}
3031

3132
// 矩阵快速幂

Diff for: src/class098/Code05_DominoTromino.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,14 @@ public static int f2(int n) {
5454
if (n == 2) {
5555
return 5;
5656
}
57+
int[][] start = { { 5, 2, 1 } };
5758
int[][] base = {
5859
{ 2, 1, 0 },
5960
{ 0, 0, 1 },
6061
{ 1, 0, 0 }
6162
};
62-
int[][] m = power(base, n - 2);
63-
long ans = 5L * m[0][0] % MOD;
64-
ans = (ans + 2L * m[1][0]) % MOD;
65-
ans = (ans + m[2][0]) % MOD;
66-
return (int) ans;
63+
int[][] ans = multiply(start, power(base, n - 2));
64+
return ans[0][0];
6765
}
6866

6967
// 矩阵快速幂

Diff for: src/class098/Code06_CountVowelsPermutation.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,20 @@ public class Code06_CountVowelsPermutation {
1818
public static int MOD = 1000000007;
1919

2020
public static int countVowelPermutation(int n) {
21+
int[][] start = { { 1, 1, 1, 1, 1 } };
2122
int[][] base = {
2223
{ 0, 1, 0, 0, 0 },
2324
{ 1, 0, 1, 0, 0 },
2425
{ 1, 1, 0, 1, 1 },
2526
{ 0, 0, 1, 0, 1 },
2627
{ 1, 0, 0, 0, 0 }
2728
};
28-
int[][] m = power(base, n - 1);
29-
long ans = 0;
30-
for (int i = 0; i < 5; i++) {
31-
for (int j = 0; j < 5; j++) {
32-
ans = (ans + m[i][j]) % MOD;
33-
}
29+
int[][] ans = multiply(start, power(base, n - 1));
30+
int ret = 0;
31+
for (int a : ans[0]) {
32+
ret = (ret + a) % MOD;
3433
}
35-
return (int) ans;
34+
return ret;
3635
}
3736

3837
// 矩阵快速幂

Diff for: src/class098/Code07_StudentAttendanceRecordII.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Code07_StudentAttendanceRecordII {
2121
public static int MOD = 1000000007;
2222

2323
public static int checkRecord(int n) {
24+
int[][] start = { { 1, 0, 0, 0, 0, 0 } };
2425
int[][] base = {
2526
{ 1, 1, 0, 1, 0, 0 },
2627
{ 1, 0, 1, 1, 0, 0 },
@@ -29,12 +30,12 @@ public static int checkRecord(int n) {
2930
{ 0, 0, 0, 1, 0, 1 },
3031
{ 0, 0, 0, 1, 0, 0 }
3132
};
32-
int[][] m = power(base, n);
33-
int sum = 0;
34-
for (int j = 0; j < m[0].length; j++) {
35-
sum = (sum + m[0][j]) % MOD;
33+
int[][] ans = multiply(start, power(base, n));
34+
int ret = 0;
35+
for (int a : ans[0]) {
36+
ret = (ret + a) % MOD;
3637
}
37-
return (int) (sum % MOD);
38+
return ret;
3839
}
3940

4041
// 矩阵快速幂

Diff for: src/class098/MatrixPowerMultiplyShow.java

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package class098;
2+
3+
public class MatrixPowerMultiplyShow {
4+
5+
public static void main(String[] args) {
6+
System.out.println("矩阵乘法展示开始");
7+
int[][] a = { { 1, 3 }, { 4, 2 } };
8+
int[][] b = { { 2, 3 }, { 3, 2 } };
9+
// 2 3
10+
// 3 2
11+
//
12+
// 1 3 ? ?
13+
// 4 2 ? ?
14+
int[][] ans1 = multiply(a, b);
15+
print(ans1);
16+
System.out.println("======");
17+
int[][] c = { { 2, 4, 1 }, { 3, 2, 2 } };
18+
int[][] d = { { 2, 3, 2 }, { 3, 2, 3 }, { 1, 1, 4 } };
19+
// 2 3 2
20+
// 3 2 3
21+
// 1 1 4
22+
//
23+
// 2 4 1 ? ? ?
24+
// 3 2 2 ? ? ?
25+
int[][] ans2 = multiply(c, d);
26+
print(ans2);
27+
System.out.println("======");
28+
int[][] e = { { 3, 1, 2 } };
29+
int[][] f = { { 1, 2, 1 }, { 3, 2, 1 }, { 4, 2, -2 } };
30+
// 1 2 1
31+
// 3 2 1
32+
// 4 2 -2
33+
//
34+
// 3 1 2 ? ? ?
35+
int[][] ans3 = multiply(e, f);
36+
print(ans3);
37+
System.out.println("矩阵乘法展示结束");
38+
System.out.println();
39+
System.out.println("求斐波那契数列第n项");
40+
System.out.println("用矩阵乘法解决");
41+
System.out.println("展示开始");
42+
f1();
43+
System.out.println("展示结束");
44+
System.out.println();
45+
System.out.println("求斐波那契数列第n项");
46+
System.out.println("用矩阵快速幂解决");
47+
System.out.println("展示开始");
48+
f2();
49+
System.out.println("展示结束");
50+
System.out.println();
51+
}
52+
53+
// 打印二维矩阵
54+
public static void print(int[][] m) {
55+
for (int i = 0; i < m.length; i++) {
56+
for (int j = 0; j < m[0].length; j++) {
57+
if (m[i][j] < 10) {
58+
System.out.print(m[i][j] + " ");
59+
} else if (m[i][j] < 100) {
60+
System.out.print(m[i][j] + " ");
61+
} else {
62+
System.out.print(m[i][j] + " ");
63+
}
64+
}
65+
System.out.println();
66+
}
67+
}
68+
69+
// 矩阵快速幂
70+
public static int[][] power(int[][] m, int p) {
71+
int n = m.length;
72+
int[][] ans = new int[n][n];
73+
for (int i = 0; i < n; i++) {
74+
ans[i][i] = 1;
75+
}
76+
for (; p != 0; p >>= 1) {
77+
if ((p & 1) != 0) {
78+
ans = multiply(ans, m);
79+
}
80+
m = multiply(m, m);
81+
}
82+
return ans;
83+
}
84+
85+
// 矩阵相乘
86+
// a的列数一定要等于b的行数
87+
public static int[][] multiply(int[][] a, int[][] b) {
88+
int n = a.length;
89+
int m = b[0].length;
90+
int k = a[0].length;
91+
int[][] ans = new int[n][m];
92+
for (int i = 0; i < n; i++) {
93+
for (int j = 0; j < m; j++) {
94+
for (int c = 0; c < k; c++) {
95+
ans[i][j] += a[i][c] * b[c][j];
96+
}
97+
}
98+
}
99+
return ans;
100+
}
101+
102+
// 用矩阵乘法解决
103+
public static void f1() {
104+
// 0 1 1 2 3 5 8 13 21 34...
105+
// 0 1 2 3 4 5 6 7 8 9
106+
int[][] m = { { 1, 1 }, { 1, 0 } };
107+
int[][] start = { { 1, 0 } };
108+
int[][] a = multiply(start, m);
109+
print(a);
110+
System.out.println("======");
111+
int[][] b = multiply(a, m);
112+
print(b);
113+
System.out.println("======");
114+
int[][] c = multiply(b, m);
115+
print(c);
116+
System.out.println("======");
117+
int[][] d = multiply(c, m);
118+
print(d);
119+
}
120+
121+
// 用矩阵快速幂解决
122+
public static void f2() {
123+
// 0 1 1 2 3 5 8 13 21 34...
124+
// 0 1 2 3 4 5 6 7 8 9
125+
int[][] m = { { 1, 1 }, { 1, 0 } };
126+
int[][] start = { { 1, 0 } };
127+
int[][] ans = multiply(start, power(m, 1));
128+
print(ans);
129+
System.out.println("======");
130+
ans = multiply(start, power(m, 2));
131+
print(ans);
132+
System.out.println("======");
133+
ans = multiply(start, power(m, 3));
134+
print(ans);
135+
System.out.println("======");
136+
ans = multiply(start, power(m, 4));
137+
print(ans);
138+
}
139+
140+
}

0 commit comments

Comments
 (0)