結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー tookunn_1213tookunn_1213
提出日時 2017-06-09 22:52:15
言語 Python2
(2.7.18)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 928 bytes
コンパイル時間 941 ms
コンパイル使用メモリ 7,112 KB
実行使用メモリ 6,440 KB
最終ジャッジ日時 2023-10-23 22:47:50
合計ジャッジ時間 906 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
6,424 KB
testcase_01 AC 10 ms
6,424 KB
testcase_02 AC 11 ms
6,424 KB
testcase_03 AC 11 ms
6,424 KB
testcase_04 AC 10 ms
6,424 KB
testcase_05 AC 10 ms
6,424 KB
testcase_06 AC 11 ms
6,428 KB
testcase_07 AC 11 ms
6,432 KB
testcase_08 AC 10 ms
6,436 KB
testcase_09 AC 10 ms
6,436 KB
testcase_10 AC 10 ms
6,436 KB
testcase_11 AC 10 ms
6,440 KB
testcase_12 AC 9 ms
6,440 KB
testcase_13 AC 10 ms
6,440 KB
testcase_14 AC 10 ms
6,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

NUM,MOD = map(int,raw_input().split())
M=[]
def _fib_matrix(n,m):
    global M
    if n > 1:
        _fib_matrix(n/2,m)
        M = [
            [M[0][0]*M[0][0] + M[0][1]*M[1][0],
             M[0][0]*M[0][1] + M[0][1]*M[1][1],
            ],
            [M[1][0]*M[0][0] + M[1][1]*M[1][0],
             M[1][0]*M[0][1] + M[1][1]*M[1][1],
            ],
        ]
        if m:
            M = [map(lambda x:x%m, v) for v in M]
    if n % 2 == 1:
        N= [[1,1], [1,0]]
        M = [
            [M[0][0]*N[0][0] + M[0][1]*N[1][0],
             M[0][0]*N[0][1] + M[0][1]*N[1][1],
            ],
            [M[1][0]*N[0][0] + M[1][1]*N[1][0],
             M[1][0]*N[0][1] + M[1][1]*N[1][1],
            ],
        ]
        if m:
            M = [map(lambda x:x%m, v) for v in M]

def fib_matrix(n, modulo=None):
    global M
    M = [[1,0], [0,1]]
    _fib_matrix(n, modulo)
    return M[0][1]
print(fib_matrix(NUM-1,MOD))
0