-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution2.cs
60 lines (54 loc) · 1.74 KB
/
Solution2.cs
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
public class Solution {
private const int mod = 1000000007;
private readonly int[][] baseMatrix = {
new int[] {0, 0, 0, 0, 1, 0, 1, 0, 0, 0},
new int[] {0, 0, 0, 0, 0, 0, 1, 0, 1, 0},
new int[] {0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
new int[] {0, 0, 0, 0, 1, 0, 0, 0, 1, 0},
new int[] {1, 0, 0, 1, 0, 0, 0, 0, 0, 1},
new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
new int[] {1, 1, 0, 0, 0, 0, 0, 1, 0, 0},
new int[] {0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
new int[] {0, 1, 0, 1, 0, 0, 0, 0, 0, 0},
new int[] {0, 0, 1, 0, 1, 0, 0, 0, 0, 0}
};
public int KnightDialer(int n) {
int[][] res = Pow(baseMatrix, n - 1);
int ans = 0;
foreach (var x in res[0]) {
ans = (ans + x) % mod;
}
return ans;
}
private int[][] Mul(int[][] a, int[][] b) {
int m = a.Length, n = b[0].Length;
int[][] c = new int[m][];
for (int i = 0; i < m; i++) {
c[i] = new int[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] = (int)((c[i][j] + (long)a[i][k] * b[k][j]) % mod);
}
}
}
return c;
}
private int[][] Pow(int[][] a, int n) {
int size = a.Length;
int[][] res = new int[1][];
res[0] = new int[size];
for (int i = 0; i < size; i++) {
res[0][i] = 1;
}
while (n > 0) {
if (n % 2 == 1) {
res = Mul(res, a);
}
a = Mul(a, a);
n /= 2;
}
return res;
}
}