-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Gold IV] Title: 행렬 제곱, Time: 44 ms, Memory: 31120 KB -BaekjoonHub
- Loading branch information
1 parent
9ad6167
commit 66bead0
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# [Gold IV] 행렬 제곱 - 10830 | ||
|
||
[문제 링크](https://www.acmicpc.net/problem/10830) | ||
|
||
### 성능 요약 | ||
|
||
메모리: 31120 KB, 시간: 44 ms | ||
|
||
### 분류 | ||
|
||
분할 정복, 분할 정복을 이용한 거듭제곱, 선형대수학, 수학 | ||
|
||
### 제출 일자 | ||
|
||
2024년 8월 11일 16:35:13 | ||
|
||
### 문제 설명 | ||
|
||
<p>크기가 N*N인 행렬 A가 주어진다. 이때, A의 B제곱을 구하는 프로그램을 작성하시오. 수가 매우 커질 수 있으니, A^B의 각 원소를 1,000으로 나눈 나머지를 출력한다.</p> | ||
|
||
### 입력 | ||
|
||
<p>첫째 줄에 행렬의 크기 N과 B가 주어진다. (2 ≤ N ≤ 5, 1 ≤ B ≤ 100,000,000,000)</p> | ||
|
||
<p>둘째 줄부터 N개의 줄에 행렬의 각 원소가 주어진다. 행렬의 각 원소는 1,000보다 작거나 같은 자연수 또는 0이다.</p> | ||
|
||
### 출력 | ||
|
||
<p>첫째 줄부터 N개의 줄에 걸쳐 행렬 A를 B제곱한 결과를 출력한다.</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import sys | ||
def input(): | ||
return sys.stdin.readline().rstrip() | ||
|
||
|
||
def print_dev_only(args): | ||
printable = False | ||
if printable: | ||
print(args) | ||
|
||
|
||
division_by = 1_000 | ||
|
||
|
||
def multiply_matrix(A, B): | ||
result_M = [] | ||
size = len(A) | ||
print_dev_only(f"A={A}\nB={B}") | ||
for row_1 in range(size): | ||
sub_list = [] | ||
|
||
for col_2 in range(size): | ||
# 자릿수마다 곱해서 더해주기 | ||
A_collected_w_row = A[row_1] | ||
B_collected_w_col = [] | ||
|
||
for row_2 in range(size): | ||
B_collected_w_col.append(B[row_2][col_2]) | ||
|
||
acc = 0 | ||
for i in range(size): | ||
acc += A_collected_w_row[i] * B_collected_w_col[i] | ||
sub_list.append(acc % division_by) | ||
|
||
result_M.append(sub_list) | ||
# sub_list = multiply_matrix(A_collected_w_row, B_collected_w_col) | ||
# print_dev_only(sub_list) | ||
|
||
# result_M.append(sub_list) | ||
|
||
# print_dev_only(f"result={result_M}") | ||
return result_M | ||
|
||
|
||
computed_storage = {} | ||
|
||
|
||
def get_square_matrix(M, square): | ||
result = None | ||
|
||
if square == 1: # 정복 | ||
result = M | ||
elif computed_storage.get(square) != None: | ||
result = computed_storage.get(square) | ||
elif square % 2 == 0: # 분할 | ||
half_M = get_square_matrix(M, square // 2) | ||
result = multiply_matrix(half_M, half_M) | ||
else: | ||
reduced_M = get_square_matrix(M, (square - 1)) | ||
result = multiply_matrix(reduced_M, M) | ||
|
||
computed_storage[square] = result | ||
|
||
return result | ||
|
||
|
||
N, B = map(int, input().split()) | ||
matrix = [] | ||
|
||
for i in range(N): | ||
matrix.append(list(map(int, input().split()))) | ||
|
||
|
||
result = get_square_matrix(matrix, B) | ||
|
||
for i in range(N): | ||
for j in range(N): | ||
print(result[i][j] % division_by, end=" ") | ||
print("") |