結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー Akisawa3Akisawa3
提出日時 2023-04-22 23:03:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 511 ms / 2,000 ms
コード長 378 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 44,452 KB
最終ジャッジ日時 2024-04-25 00:50:29
合計ジャッジ時間 10,504 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 506 ms
44,200 KB
testcase_01 AC 508 ms
44,072 KB
testcase_02 AC 501 ms
44,196 KB
testcase_03 AC 507 ms
44,200 KB
testcase_04 AC 477 ms
43,940 KB
testcase_05 AC 483 ms
44,204 KB
testcase_06 AC 491 ms
44,452 KB
testcase_07 AC 493 ms
43,816 KB
testcase_08 AC 481 ms
43,828 KB
testcase_09 AC 487 ms
44,196 KB
testcase_10 AC 483 ms
43,940 KB
testcase_11 AC 483 ms
44,068 KB
testcase_12 AC 499 ms
43,812 KB
testcase_13 AC 511 ms
44,068 KB
testcase_14 AC 492 ms
43,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np
N, M = map(int, input().split())

def fibmtx2n(k):
  fm = np.array([[1,1],[1,0]], dtype='object')
  for i in range(k): 
    fm = np.dot(fm,fm) % M
  return fm

def fibmtx(n):
  fm1 = np.array([[1,0],[0,1]],dtype='object')
  while n > 0:
    l2n = n.bit_length()-1
    fm1 = np.dot(fm1, fibmtx2n(l2n)) % M
    n -= 2**l2n
  return fm1[0][1]

print(fibmtx(N-1))
0