結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー sho_00sho_00
提出日時 2020-04-06 18:34:08
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 448 bytes
コンパイル時間 676 ms
コンパイル使用メモリ 10,712 KB
実行使用メモリ 29,600 KB
最終ジャッジ日時 2023-09-20 20:40:58
合計ジャッジ時間 5,516 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
29,424 KB
testcase_01 AC 127 ms
29,520 KB
testcase_02 AC 126 ms
29,480 KB
testcase_03 AC 126 ms
29,488 KB
testcase_04 AC 126 ms
29,600 KB
testcase_05 AC 128 ms
29,588 KB
testcase_06 AC 126 ms
29,496 KB
testcase_07 AC 128 ms
29,572 KB
testcase_08 AC 126 ms
29,464 KB
testcase_09 AC 125 ms
29,500 KB
testcase_10 AC 124 ms
29,524 KB
testcase_11 AC 126 ms
29,592 KB
testcase_12 AC 129 ms
29,504 KB
testcase_13 AC 126 ms
29,348 KB
testcase_14 AC 128 ms
29,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

n,m=map(int,input().split())
mod=m

def seq_mat(A,k,mod):
  if k==1:
    return A
  else:
    if k%2==0:
      tmp=seq_mat(A,k//2,mod)
      ret=np.dot(tmp,tmp)
    else:
      tmp=seq_mat(A,k-1,mod)
      ret=np.dot(A,tmp)
    for i in range(2):
        for j in range(2):
          ret[i][j]%=mod
    return ret
      
A=np.array([[1,1],[1,0]])
if n<=2:
  print((n-1)%mod)
else:
  fib_n=seq_mat(A,n-2,mod)[0][0]
  print(fib_n)
0