結果

問題 No.1112 冥界の音楽
ユーザー H3PO4
提出日時 2023-06-17 20:19:18
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 910 ms / 2,000 ms
コード長 612 bytes
コンパイル時間 124 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 45,376 KB
最終ジャッジ日時 2024-06-25 10:13:27
合計ジャッジ時間 25,342 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

MOD = 10 ** 9 + 7
int1 = lambda x: int(x) - 1


def pow_matrix_mod(x, n, mod=MOD):
    x = x.astype(object)
    if not n:
        return np.eye(len(x), dtype=object)
    if n % 2 == 0:
        return pow_matrix_mod(x @ x % mod, n // 2) % mod
    else:
        return x @ pow_matrix_mod(x @ x % mod, (n - 1) // 2) % mod


K, M, N = map(int, input().split())
matr = np.zeros((36, 36), dtype=int)
for _ in range(M):
    P, Q, R = map(int1, input().split())
    matr[6 * P + Q, 6 * Q + R] = True

A = np.zeros(36, dtype=object)
A[:6] = 1
print((A @ pow_matrix_mod(matr, N - 2))[::6].sum() % MOD)
0