結果

問題 No.2441 行列累乗
ユーザー Nikkuniku029Nikkuniku029
提出日時 2023-08-25 21:21:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 771 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 10,820 KB
実行使用メモリ 7,940 KB
最終ジャッジ日時 2023-08-25 21:21:58
合計ジャッジ時間 1,351 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,840 KB
testcase_01 AC 15 ms
7,904 KB
testcase_02 AC 15 ms
7,904 KB
testcase_03 AC 15 ms
7,908 KB
testcase_04 AC 16 ms
7,900 KB
testcase_05 AC 15 ms
7,748 KB
testcase_06 AC 15 ms
7,860 KB
testcase_07 AC 16 ms
7,940 KB
testcase_08 AC 15 ms
7,752 KB
testcase_09 AC 16 ms
7,748 KB
testcase_10 AC 15 ms
7,840 KB
testcase_11 AC 16 ms
7,900 KB
testcase_12 AC 15 ms
7,720 KB
testcase_13 AC 15 ms
7,756 KB
testcase_14 AC 16 ms
7,840 KB
testcase_15 AC 15 ms
7,876 KB
testcase_16 AC 16 ms
7,840 KB
testcase_17 AC 16 ms
7,748 KB
testcase_18 AC 15 ms
7,824 KB
testcase_19 AC 15 ms
7,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def mat_mul(a, b):
    """
    a: 行列(2次元配列)I*J
    b: 行列(2次元配列)J*K
    """
    MOD = 1000000007
    I, J, K = len(a), len(b[0]), len(b)
    c = [[0] * J for _ in range(I)]
    for i in range(I):
        for j in range(J):
            for k in range(K):
                c[i][j] += a[i][k] * b[k][j]
    return c


def pow_mat(A, n):
    """
    A: 正方行列(2次元配列)
    n: 累乗指数
    """
    p = [[0]*len(A) for _ in range(len(A))]

    # 基本行列にする
    for i in range(len(A)):
        p[i][i] = 1

    while n > 0:
        if n & 1:
            p = mat_mul(A, p)
        A = mat_mul(A, A)
        n >>= 1
    return p


M = [list(map(int, input().split())) for _ in range(2)]
M = pow_mat(M, 3)
for c in M:
    print(*c)
0