結果

問題 No.572 妖精の演奏
ユーザー convexineq
提出日時 2021-02-25 00:58:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 650 bytes
コンパイル時間 404 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 66,896 KB
最終ジャッジ日時 2024-09-25 01:44:22
合計ジャッジ時間 2,084 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

def matmul(A,B): # A,B: 行列
    res = [[0]*len(B[0]) for _ in range(len(A))]
    for i, resi in enumerate(res):
        for k, aik in enumerate(A[i]):
            for j,bkj in enumerate(B[k]):
                resi[j] = max(resi[j],aik+bkj)
    return res

def matpow(A,p): #A^p mod M
    if p%2:
        return matmul(A, matpow(A,p-1))
    elif p > 0:
        b = matpow(A,p//2)
        return matmul(b,b)
    else:
        return [[0 if i == j else -(1<<31) for j in range(len(A))] for i in range(len(A))]

n = int(input())
m = int(input())
a = [list(map(int,input().split())) for _ in range(m)]
r = matmul([[0]*m],matpow(a,n-1))
print(max(r[0]))
0