結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー momoyuumomoyuu
提出日時 2022-04-08 22:08:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 549 bytes
コンパイル時間 426 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 71,640 KB
最終ジャッジ日時 2023-08-19 00:40:52
合計ジャッジ時間 2,296 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
71,632 KB
testcase_01 AC 59 ms
71,388 KB
testcase_02 AC 59 ms
71,256 KB
testcase_03 AC 61 ms
71,608 KB
testcase_04 AC 57 ms
71,408 KB
testcase_05 AC 56 ms
71,640 KB
testcase_06 AC 58 ms
71,440 KB
testcase_07 AC 56 ms
71,444 KB
testcase_08 AC 57 ms
71,288 KB
testcase_09 AC 59 ms
71,260 KB
testcase_10 AC 59 ms
71,292 KB
testcase_11 AC 58 ms
71,360 KB
testcase_12 AC 58 ms
71,172 KB
testcase_13 AC 57 ms
71,328 KB
testcase_14 AC 58 ms
71,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def dot(A,B,M):
    l = len(A)
    m = len(A[0])
    if m != len(B):
        return False
    n = len(B[0])
    C = [[] for _ in range(l)]
    for i in range(l):
        for j in range(n):
            sum = 0
            for k in range(m):
                sum += A[i][k]*B[k][j]
                sum %= M
            C[i].append(sum)
    return C

A = [[1,1],[1,0]]
N,M = map(int,input().split())
N -= 1
now = [[1,0],[0,1]]

l = len(bin(N)) - 2
for i in range(l):
    if N >> i & 1 == 1:
        now = dot(now,A,M)
    A = dot(A,A,M)
print(now[1][0])
0