Skip to content

Commit bf8661b

Browse files
committed
modify code
1 parent 0551e7a commit bf8661b

7 files changed

+121
-104
lines changed

src/class098/Code02_BigShow.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,22 @@ public static void main(String[] args) {
1212
System.out.println("矩阵乘法展示结束");
1313
System.out.println();
1414
System.out.println("f2() : ");
15+
System.out.println("矩阵快速幂展示开始");
16+
f2();
17+
System.out.println("矩阵快速幂展示结束");
18+
System.out.println();
19+
System.out.println("f3() : ");
1520
System.out.println("求斐波那契数列第n项");
1621
System.out.println("用矩阵乘法解决");
1722
System.out.println("展示开始");
18-
f2();
23+
f3();
1924
System.out.println("展示结束");
2025
System.out.println();
21-
System.out.println("f3() : ");
26+
System.out.println("f4() : ");
2227
System.out.println("求斐波那契数列第n项");
2328
System.out.println("用矩阵快速幂解决");
2429
System.out.println("展示开始");
25-
f2();
30+
f4();
2631
System.out.println("展示结束");
2732
System.out.println();
2833
}
@@ -45,6 +50,7 @@ public static int[][] multiply(int[][] a, int[][] b) {
4550
}
4651

4752
// 矩阵快速幂
53+
// 要求矩阵m是正方形矩阵
4854
public static int[][] power(int[][] m, int p) {
4955
int n = m.length;
5056
int[][] ans = new int[n][n];
@@ -119,9 +125,20 @@ public static void f1() {
119125
int[][] ans4 = multiply(g, h);
120126
print(ans4);
121127
}
128+
129+
public static void f2() {
130+
// 只有正方形矩阵可以求幂
131+
int[][] a = { { 1, 2 }, { 3, 4 } };
132+
// 连乘得到矩阵a的5次方
133+
int[][] b = multiply(a, multiply(a, multiply(a, multiply(a, a))));
134+
print(b);
135+
System.out.println("======");
136+
// 矩阵快速幂得到a的5次方
137+
print(power(a, 5));
138+
}
122139

123140
// 用矩阵乘法解决斐波那契第n项的问题
124-
public static void f2() {
141+
public static void f3() {
125142
// 0 1 1 2 3 5 8 13 21 34...
126143
// 0 1 2 3 4 5 6 7 8 9
127144
int[][] start = { { 1, 0 } };
@@ -159,7 +176,7 @@ public static void f2() {
159176
}
160177

161178
// 用矩阵快速幂解决斐波那契第n项的问题
162-
public static void f3() {
179+
public static void f4() {
163180
// 0 1 1 2 3 5 8 13 21 34...
164181
// 0 1 2 3 4 5 6 7 8 9
165182
int[][] start = { { 1, 0 } };

src/class098/Code02_FibonacciNumber.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ public static int fib2(int n) {
4040
return ans[0][0];
4141
}
4242

43-
// 矩阵快速幂
44-
public static int[][] power(int[][] m, int p) {
45-
int n = m.length;
46-
int[][] ans = new int[n][n];
47-
for (int i = 0; i < n; i++) {
48-
ans[i][i] = 1;
49-
}
50-
for (; p != 0; p >>= 1) {
51-
if ((p & 1) != 0) {
52-
ans = multiply(ans, m);
53-
}
54-
m = multiply(m, m);
55-
}
56-
return ans;
57-
}
58-
5943
// 矩阵相乘
6044
// a的列数一定要等于b的行数
6145
public static int[][] multiply(int[][] a, int[][] b) {
@@ -73,4 +57,20 @@ public static int[][] multiply(int[][] a, int[][] b) {
7357
return ans;
7458
}
7559

60+
// 矩阵快速幂
61+
public static int[][] power(int[][] m, int p) {
62+
int n = m.length;
63+
int[][] ans = new int[n][n];
64+
for (int i = 0; i < n; i++) {
65+
ans[i][i] = 1;
66+
}
67+
for (; p != 0; p >>= 1) {
68+
if ((p & 1) != 0) {
69+
ans = multiply(ans, m);
70+
}
71+
m = multiply(m, m);
72+
}
73+
return ans;
74+
}
75+
7676
}

src/class098/Code03_ClimbingStairs.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,6 @@ public static int climbStairs(int n) {
2626
return ans[0][0];
2727
}
2828

29-
// 矩阵快速幂
30-
public static int[][] power(int[][] m, int p) {
31-
int n = m.length;
32-
int[][] ans = new int[n][n];
33-
for (int i = 0; i < n; i++) {
34-
ans[i][i] = 1;
35-
}
36-
for (; p != 0; p >>= 1) {
37-
if ((p & 1) != 0) {
38-
ans = multiply(ans, m);
39-
}
40-
m = multiply(m, m);
41-
}
42-
return ans;
43-
}
44-
4529
// 矩阵相乘
4630
// a的列数一定要等于b的行数
4731
public static int[][] multiply(int[][] a, int[][] b) {
@@ -59,4 +43,20 @@ public static int[][] multiply(int[][] a, int[][] b) {
5943
return ans;
6044
}
6145

46+
// 矩阵快速幂
47+
public static int[][] power(int[][] m, int p) {
48+
int n = m.length;
49+
int[][] ans = new int[n][n];
50+
for (int i = 0; i < n; i++) {
51+
ans[i][i] = 1;
52+
}
53+
for (; p != 0; p >>= 1) {
54+
if ((p & 1) != 0) {
55+
ans = multiply(ans, m);
56+
}
57+
m = multiply(m, m);
58+
}
59+
return ans;
60+
}
61+
6262
}

src/class098/Code04_TribonacciNumber.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,6 @@ public static int tribonacci(int n) {
2929
return ans[0][0];
3030
}
3131

32-
// 矩阵快速幂
33-
public static int[][] power(int[][] m, int p) {
34-
int n = m.length;
35-
int[][] ans = new int[n][n];
36-
for (int i = 0; i < n; i++) {
37-
ans[i][i] = 1;
38-
}
39-
for (; p != 0; p >>= 1) {
40-
if ((p & 1) != 0) {
41-
ans = multiply(ans, m);
42-
}
43-
m = multiply(m, m);
44-
}
45-
return ans;
46-
}
47-
4832
// 矩阵相乘
4933
// a的列数一定要等于b的行数
5034
public static int[][] multiply(int[][] a, int[][] b) {
@@ -62,4 +46,20 @@ public static int[][] multiply(int[][] a, int[][] b) {
6246
return ans;
6347
}
6448

49+
// 矩阵快速幂
50+
public static int[][] power(int[][] m, int p) {
51+
int n = m.length;
52+
int[][] ans = new int[n][n];
53+
for (int i = 0; i < n; i++) {
54+
ans[i][i] = 1;
55+
}
56+
for (; p != 0; p >>= 1) {
57+
if ((p & 1) != 0) {
58+
ans = multiply(ans, m);
59+
}
60+
m = multiply(m, m);
61+
}
62+
return ans;
63+
}
64+
6565
}

src/class098/Code05_DominoTromino.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,7 @@ public static int f2(int n) {
6464
return ans[0][0];
6565
}
6666

67-
// 矩阵快速幂
68-
public static int[][] power(int[][] m, int p) {
69-
int n = m.length;
70-
int[][] ans = new int[n][n];
71-
for (int i = 0; i < n; i++) {
72-
ans[i][i] = 1;
73-
}
74-
for (; p != 0; p >>= 1) {
75-
if ((p & 1) != 0) {
76-
ans = multiply(ans, m);
77-
}
78-
m = multiply(m, m);
79-
}
80-
return ans;
81-
}
82-
83-
// 矩阵相乘
67+
// 矩阵相乘 + 乘法取模
8468
// a的列数一定要等于b的行数
8569
public static int[][] multiply(int[][] a, int[][] b) {
8670
int n = a.length;
@@ -97,4 +81,20 @@ public static int[][] multiply(int[][] a, int[][] b) {
9781
return ans;
9882
}
9983

84+
// 矩阵快速幂
85+
public static int[][] power(int[][] m, int p) {
86+
int n = m.length;
87+
int[][] ans = new int[n][n];
88+
for (int i = 0; i < n; i++) {
89+
ans[i][i] = 1;
90+
}
91+
for (; p != 0; p >>= 1) {
92+
if ((p & 1) != 0) {
93+
ans = multiply(ans, m);
94+
}
95+
m = multiply(m, m);
96+
}
97+
return ans;
98+
}
99+
100100
}

src/class098/Code06_CountVowelsPermutation.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,7 @@ public static int countVowelPermutation(int n) {
3535
return ret;
3636
}
3737

38-
// 矩阵快速幂
39-
public static int[][] power(int[][] m, int p) {
40-
int n = m.length;
41-
int[][] ans = new int[n][n];
42-
for (int i = 0; i < n; i++) {
43-
ans[i][i] = 1;
44-
}
45-
for (; p != 0; p >>= 1) {
46-
if ((p & 1) != 0) {
47-
ans = multiply(ans, m);
48-
}
49-
m = multiply(m, m);
50-
}
51-
return ans;
52-
}
53-
54-
// 矩阵相乘
38+
// 矩阵相乘 + 乘法取模
5539
// a的列数一定要等于b的行数
5640
public static int[][] multiply(int[][] a, int[][] b) {
5741
int n = a.length;
@@ -68,4 +52,20 @@ public static int[][] multiply(int[][] a, int[][] b) {
6852
return ans;
6953
}
7054

55+
// 矩阵快速幂
56+
public static int[][] power(int[][] m, int p) {
57+
int n = m.length;
58+
int[][] ans = new int[n][n];
59+
for (int i = 0; i < n; i++) {
60+
ans[i][i] = 1;
61+
}
62+
for (; p != 0; p >>= 1) {
63+
if ((p & 1) != 0) {
64+
ans = multiply(ans, m);
65+
}
66+
m = multiply(m, m);
67+
}
68+
return ans;
69+
}
70+
7171
}

src/class098/Code07_StudentAttendanceRecordII.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,7 @@ public static int checkRecord(int n) {
3939
return ret;
4040
}
4141

42-
// 矩阵快速幂
43-
public static int[][] power(int[][] m, int p) {
44-
int n = m.length;
45-
int[][] ans = new int[n][n];
46-
for (int i = 0; i < n; i++) {
47-
ans[i][i] = 1;
48-
}
49-
for (; p != 0; p >>= 1) {
50-
if ((p & 1) != 0) {
51-
ans = multiply(ans, m);
52-
}
53-
m = multiply(m, m);
54-
}
55-
return ans;
56-
}
57-
58-
// 矩阵相乘
42+
// 矩阵相乘 + 乘法取模
5943
// a的列数一定要等于b的行数
6044
public static int[][] multiply(int[][] a, int[][] b) {
6145
int n = a.length;
@@ -72,4 +56,20 @@ public static int[][] multiply(int[][] a, int[][] b) {
7256
return ans;
7357
}
7458

59+
// 矩阵快速幂
60+
public static int[][] power(int[][] m, int p) {
61+
int n = m.length;
62+
int[][] ans = new int[n][n];
63+
for (int i = 0; i < n; i++) {
64+
ans[i][i] = 1;
65+
}
66+
for (; p != 0; p >>= 1) {
67+
if ((p & 1) != 0) {
68+
ans = multiply(ans, m);
69+
}
70+
m = multiply(m, m);
71+
}
72+
return ans;
73+
}
74+
7575
}

0 commit comments

Comments
 (0)