結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー kumatan400kumatan400
提出日時 2021-09-22 18:53:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 745 bytes
コンパイル時間 76 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 8,356 KB
最終ジャッジ日時 2023-09-18 19:41:28
合計ジャッジ時間 1,176 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,188 KB
testcase_01 AC 16 ms
8,292 KB
testcase_02 AC 15 ms
8,204 KB
testcase_03 AC 16 ms
8,180 KB
testcase_04 AC 16 ms
8,168 KB
testcase_05 AC 16 ms
8,340 KB
testcase_06 AC 16 ms
8,356 KB
testcase_07 AC 16 ms
8,200 KB
testcase_08 AC 16 ms
7,880 KB
testcase_09 AC 16 ms
8,280 KB
testcase_10 AC 16 ms
8,120 KB
testcase_11 AC 16 ms
8,196 KB
testcase_12 AC 16 ms
8,284 KB
testcase_13 AC 17 ms
8,160 KB
testcase_14 AC 16 ms
8,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def mul(A, B):
    if len(A[0]) != len(B):
        return -1

    R = len(A)
    C = len(B[0])
    K = len(B)

    res = [[0]*C for _ in range(R)]
    for i in range(R):
        for j in range(C):
            res[i][j] = sum(A[i][k]*B[k][j] for k in range(K))
            res[i][j] %= M
    return res


def power(A, x):
    if x == 0:
        N = len(A)
        res = [[0]*N for _ in range(N)]
        for i in range(N):
            res[i][i] = 1
        return res

    B = power(A, x//2)
    res = mul(B, B)
    if x % 2 == 1:
        res = mul(res, A)
    return res


N, M = map(int, input().split())
if N == 1:
    ans = 0
elif N == 2:
    ans = 1
else:
    A = [[1, 1],
         [1, 0]]
    A = power(A, N-2)
    ans = A[0][0]

print(ans)
0