๋ฌธ์
https://www.acmicpc.net/problem/10830
ํ์ด
# ๋ฐฑ์ค 10830๋ฒ ๋ฌธ์ - ํ๋ ฌ ์ ๊ณฑ
import sys
input = sys.stdin.readline
# ์
๋ ฅ
N, B = map(int, input().split())
matrix = []
for _ in range(N):
matrix.append(list(map(int, input().split())))
# ํ๋ ฌ ๊ณฑ์
def mul_matrix(mat1, mat2):
res = [[0]*N for _ in range(N)]
for i in range(N):
for j in range(N):
for z in range(N):
# c11 = a11*b11 + a12*b21
res[i][j] += mat1[i][z] * mat2[z][j] % 1000
return res
# ๋ถํ ์ ๋ณต
def power(a, b):
if b == 1: # b์ ๊ฐ์ด 1์ด ๋ ๋๊น์ง ์ฌ๊ท
return a
else:
tmp = power(a, b // 2) # a^(b // 2)
if b % 2 == 0:
return mul_matrix(tmp, tmp) # b๊ฐ ์ง์์ธ ๊ฒฝ์ฐ
else:
return mul_matrix(mul_matrix(tmp, tmp), a) # b๊ฐ ํ์์ธ ๊ฒฝ์ฐ
result = power(matrix, B)
# ์ถ๋ ฅ
for row in result:
for col in row:
print(col % 1000, end=" ")
print()