結果

問題 No.2441 行列累乗
ユーザー TakaTaka
提出日時 2023-10-04 16:03:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 692 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 10,920 KB
実行使用メモリ 29,756 KB
最終ジャッジ日時 2023-10-04 16:03:12
合計ジャッジ時間 4,514 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
29,496 KB
testcase_01 AC 139 ms
29,588 KB
testcase_02 AC 140 ms
29,652 KB
testcase_03 AC 143 ms
29,572 KB
testcase_04 AC 144 ms
29,676 KB
testcase_05 AC 152 ms
29,756 KB
testcase_06 AC 144 ms
29,588 KB
testcase_07 AC 144 ms
29,660 KB
testcase_08 AC 144 ms
29,676 KB
testcase_09 AC 139 ms
29,740 KB
testcase_10 AC 140 ms
29,648 KB
testcase_11 AC 142 ms
29,596 KB
testcase_12 AC 140 ms
29,756 KB
testcase_13 AC 142 ms
29,592 KB
testcase_14 AC 147 ms
29,532 KB
testcase_15 AC 142 ms
29,520 KB
testcase_16 AC 151 ms
29,644 KB
testcase_17 AC 141 ms
29,528 KB
testcase_18 AC 138 ms
29,644 KB
testcase_19 AC 139 ms
29,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

def dot(A1, A2, N):
    # N: 行列のサイズ
    A = np.zeros((N, N), dtype=int)
    for i in range(N):
        for j in range(N):
            for k in range(N):
                A[i][j] = (A[i][j] + (A1[i][k] * A2[k][j]))
    return A

def pow_mat(A, K, N):
    # A: 累乗する行列, k: 累乗数, n: 行列Aのサイズ
    P = np.eye(N, dtype=int)
    while K:
        if K & 1:
            P = dot(P, A, N)
        A = dot(A, A, N)
        K >>= 1
    return P
    
a,b = map(int,input().split())
c,d = map(int,input().split())

mat = np.array([[a, b], [c, d]], dtype=int)
ans_matrix = pow_mat(mat, 3, 2)

for row in ans_matrix:
    print(" ".join(map(str, row)))
0