https://www.acmicpc.net/problem/10830
# 2023.01.21
import sys
input = sys.stdin.readline
n, b = map(int, input().split()) # nXn행렬, A^b행렬 구하기
A = [list(map(int, input().split())) for _ in range(n)]
# 행렬의 곱
def multi(A, B):
mulA=[[0]*(n) for _ in range(n)]
for i in range(n):
for j in range(n):
for k in range(n):
mulA[i][j]+=(A[i][k]*B[k][j])
mulA[i][j]%=1000
return mulA
# divide and conquer
def divideConquer(b, A):
if b==1:
for i in range(n):
for j in range(n):
A[i][j]%=1000
return A
elif b%2==0:
B = divideConquer(b//2, A)
return multi(B, B)
elif b%2==1:
B = divideConquer(b//2, A)
return multi(A, multi(B, B))
answer=divideConquer(b, A)
for i in range(n):
print(*answer[i])
'Problem Solving > BOJ' 카테고리의 다른 글
[Greedy] python 13904 과제 (0) | 2023.01.22 |
---|---|
[BFS] python 2206 벽 부수고 이동하기 (1) | 2023.01.22 |
[Meet in the Middle] python 1450 냅색문제 (0) | 2023.01.20 |
[DFS] 1987 알파벳 (0) | 2023.01.18 |
[Combination] 16938 캠프 준비 (0) | 2023.01.18 |