64 lines
1.5 KiB
Java
64 lines
1.5 KiB
Java
import java.util.*;
|
|
|
|
public class _10830 {
|
|
static int MOD = 1000;
|
|
|
|
static void print_mat(int[][] M, int N) {
|
|
for (int[] row : M) {
|
|
for(int i=0; i<N; i++) System.out.printf("%d ",row[i]);
|
|
System.out.println();
|
|
}
|
|
}
|
|
|
|
static int[][] mul_mat(int[][] m1, int[][] m2, int N) {
|
|
int[][] result = new int[N][N];
|
|
|
|
for(int i=0; i<N; i++) {
|
|
for(int j=0; j<N; j++) {
|
|
for(int k=0; k<N; k++) {
|
|
result[i][j] += m1[i][k]*m2[k][j];
|
|
}
|
|
result[i][j] %= MOD;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
static int[][] power_mat(int[][] A, long B, int N) {
|
|
int[][] result = new int[N][N];
|
|
|
|
if(B==1) {
|
|
for(int i=0; i<N; i++) {
|
|
for(int j=0; j<N; j++) {
|
|
result[i][j] = A[i][j]%MOD;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int[][] temp = power_mat(A, B/2, N);
|
|
|
|
if(B%2 == 0) return mul_mat(temp, temp, N);
|
|
|
|
return mul_mat(mul_mat(temp, temp, N), A, N);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
Scanner sc = new Scanner(System.in);
|
|
int N = sc.nextInt();
|
|
long B = sc.nextLong();
|
|
|
|
int[][] A = new int[N][N];
|
|
for(int i=0; i<N; i++) {
|
|
for(int j=0; j<N; j++) {
|
|
A[i][j] = sc.nextInt();
|
|
}
|
|
}
|
|
sc.close();
|
|
|
|
int[][] result = power_mat(A, B, N);
|
|
print_mat(result, N);
|
|
}
|
|
}
|