MCPcopy Create free account
hub / github.com/algorithmzuo/algorithm-journey / power

Method power

src/class098/Code02_BigShow.java:57–72  ·  view source on GitHub ↗
(int[][] m, int p)

Source from the content-addressed store, hash-verified

55 // 矩阵快速幂
56 // 要求矩阵m是正方形矩阵
57 public static int[][] power(int[][] m, int p) {
58 int n = m.length;
59 // 对角线全是1、剩下数字都是0的正方形矩阵,称为单位矩阵
60 // 相当于正方形矩阵中的1,矩阵a * 单位矩阵 = 矩阵a
61 int[][] ans = new int[n][n];
62 for (int i = 0; i < n; i++) {
63 ans[i][i] = 1;
64 }
65 for (; p != 0; p >>= 1) {
66 if ((p & 1) != 0) {
67 ans = multiply(ans, m);
68 }
69 m = multiply(m, m);
70 }
71 return ans;
72 }
73
74 // 打印二维矩阵
75 public static void print(int[][] m) {

Callers 2

f2Method · 0.95
f4Method · 0.95

Calls 1

multiplyMethod · 0.95

Tested by

no test coverage detected