結果

問題 No.344 ある無理数の累乗
ユーザー convexineq
提出日時 2021-01-14 07:32:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 637 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 52,480 KB
最終ジャッジ日時 2024-11-23 19:31:00
合計ジャッジ時間 2,592 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

def matmul(A,B): # A,B: 行列
    res = [[0]*len(B[0]) for _ in [None]*len(A)]
    for i, resi in enumerate(res):
        for k, aik in enumerate(A[i]):
            for j,bkj in enumerate(B[k]):
                resi[j] += aik*bkj
                resi[j] %= MOD
    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 [[1 if i == j else 0 for j in range(len(A))] for i in range(len(A))]

MOD=1000
n = int(input())
A = matpow([[0,1],[2,2]],n)
c = A[0][0]*2 + A[0][1]*2
print((c+ (0 if n%2 else -1))%MOD)
0