結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー tanon710tanon710
提出日時 2020-08-01 13:52:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 438 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 71,436 KB
最終ジャッジ日時 2023-09-22 08:37:38
合計ジャッジ時間 2,139 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,164 KB
testcase_01 AC 68 ms
71,376 KB
testcase_02 AC 69 ms
71,080 KB
testcase_03 AC 68 ms
71,304 KB
testcase_04 AC 72 ms
71,316 KB
testcase_05 AC 68 ms
71,436 KB
testcase_06 AC 68 ms
71,324 KB
testcase_07 AC 69 ms
71,404 KB
testcase_08 AC 69 ms
71,280 KB
testcase_09 AC 69 ms
71,204 KB
testcase_10 AC 68 ms
71,364 KB
testcase_11 AC 69 ms
71,276 KB
testcase_12 AC 69 ms
71,312 KB
testcase_13 AC 72 ms
71,304 KB
testcase_14 AC 71 ms
71,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def prod(mat1,mat2,mod):
    n=len(mat1)
    ret=[[0]*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            for k in range(n):
                ret[i][j]+=mat1[i][k]*mat2[k][j]
            ret[i][j]%=mod
    return ret

n,m=map(int,input().split())
n-=2
matrix=[[1,1],[1,0]]
tmp=[[1,0],[0,1]]
for i in range(33):
    if n&(1<<i):
        tmp=prod(tmp,matrix,m)
    matrix=prod(matrix,matrix,m)
print(tmp[0][0])
0