結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー severrabaenseverrabaen
提出日時 2021-09-30 10:51:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 679 bytes
コンパイル時間 1,478 ms
コンパイル使用メモリ 86,900 KB
実行使用メモリ 71,332 KB
最終ジャッジ日時 2023-09-24 13:25:54
合計ジャッジ時間 3,457 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,152 KB
testcase_01 AC 72 ms
71,296 KB
testcase_02 AC 71 ms
71,232 KB
testcase_03 AC 72 ms
71,328 KB
testcase_04 AC 73 ms
71,216 KB
testcase_05 AC 75 ms
71,292 KB
testcase_06 AC 74 ms
71,328 KB
testcase_07 AC 75 ms
71,128 KB
testcase_08 AC 75 ms
71,332 KB
testcase_09 AC 75 ms
71,332 KB
testcase_10 AC 75 ms
70,972 KB
testcase_11 AC 72 ms
71,268 KB
testcase_12 AC 71 ms
71,028 KB
testcase_13 AC 73 ms
71,264 KB
testcase_14 AC 72 ms
71,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 1e9 + 7

def mul(a,b):
    aa = len(a)
    bb = len(b[0])
    if not aa == len(b):return None
    res = [[0] * bb for _ in range(aa)]
    for i in range(aa):
        for j in range(bb):
            for k in range(len(b)):
                res[i][j] += a[i][k] * b[k][j]
                res[i][j]%=MOD
    return res

def matPow(a,n):
    s = len(a)
    res = [[0] * s for _ in range(s)]
    for i in range(s):
        res[i][i] = 1
    while n > 0:
        if n & 1:res = mul(res,a)
        a = mul(a,a)
        n>>=1
    return res

n,m = map(int,input().split())
MOD = m
a = [[1,1],
     [1,0]]
res = mul(matPow(a,n - 1),[[1],
                       [0]])
print(res[1][0])
0