28 lines
612 B
Python
28 lines
612 B
Python
def matrix_multiply(A, B) :
|
|
B_T = list(zip(*B))
|
|
|
|
return [[sum(a*b for a,b in zip(row_A, col_B))%1000 for col_B in B_T] for row_A in A]
|
|
|
|
def power_matrix(A, B) :
|
|
if B == 1 :
|
|
return [[n % 1000 for n in row] for row in A]
|
|
|
|
half = power_matrix(A, B//2)
|
|
result = matrix_multiply(half, half)
|
|
|
|
if B%2==1 :
|
|
result = matrix_multiply(result, A)
|
|
|
|
return result
|
|
|
|
def print_matrix(A) :
|
|
for row in A :
|
|
print(*row)
|
|
|
|
N, B = map(int, input().split())
|
|
A = []
|
|
for _ in range(N) :
|
|
A.append(list(map(int,input().split())))
|
|
|
|
print_matrix(power_matrix(A, B))
|