結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー hirayuu_ychirayuu_yc
提出日時 2023-06-01 06:00:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 870 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 71,940 KB
最終ジャッジ日時 2023-08-28 01:49:45
合計ジャッジ時間 2,651 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
71,664 KB
testcase_01 AC 88 ms
71,576 KB
testcase_02 AC 86 ms
71,940 KB
testcase_03 AC 86 ms
71,428 KB
testcase_04 AC 87 ms
71,572 KB
testcase_05 AC 85 ms
71,636 KB
testcase_06 AC 85 ms
71,620 KB
testcase_07 AC 85 ms
71,736 KB
testcase_08 AC 88 ms
71,696 KB
testcase_09 AC 84 ms
71,616 KB
testcase_10 AC 87 ms
71,428 KB
testcase_11 AC 89 ms
71,620 KB
testcase_12 AC 85 ms
71,576 KB
testcase_13 AC 86 ms
71,696 KB
testcase_14 AC 86 ms
71,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import copy

def matrixpow(A,n):
  pow2=[A.copy()]
  bit=2
  while bit<=n:
    pow2.append([])
    for i in range(len(A)):
      pow2[-1].append([])
      for j in range(len(A)):
        cnt=0
        for k in range(len(A)):
          cnt+=pow2[-2][i][k]*pow2[-2][k][j]
          cnt=cnt%M
        pow2[-1][-1].append(cnt)
    bit*=2
  ret=0
  bit=str(format(n,"b"))
  for i in range(len(bit)):
    if bit[-i-1]=="1":
      if ret==0:
        ret=pow2[i].copy()
      else:
        befret=copy.deepcopy(ret)
        for j in range(len(A)):
          for k in range(len(A)):
            cnt=0
            for l in range(len(A)):
              cnt+=befret[j][l]*pow2[i][l][k]
              cnt=cnt%M
            ret[j][k]=cnt
  return ret
      
N,M=map(int,input().split())
if N>=3:
  print(matrixpow([[1,1],[1,0]],N-2)[0][0])
else:
	if N==2:
		print(2)
	else:
		print(1)
0