結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー ei1333333ei1333333
提出日時 2017-06-09 22:22:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 357 bytes
コンパイル時間 1,394 ms
コンパイル使用メモリ 160,328 KB
実行使用メモリ 42,728 KB
最終ジャッジ日時 2023-10-23 21:04:04
合計ジャッジ時間 3,393 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
42,728 KB
testcase_01 AC 97 ms
42,728 KB
testcase_02 AC 96 ms
42,728 KB
testcase_03 AC 96 ms
42,728 KB
testcase_04 AC 96 ms
42,728 KB
testcase_05 AC 96 ms
42,728 KB
testcase_06 AC 96 ms
42,728 KB
testcase_07 AC 96 ms
42,728 KB
testcase_08 AC 96 ms
42,728 KB
testcase_09 AC 96 ms
42,728 KB
testcase_10 AC 96 ms
42,728 KB
testcase_11 AC 96 ms
42,728 KB
testcase_12 AC 95 ms
42,728 KB
testcase_13 AC 95 ms
42,728 KB
testcase_14 AC 94 ms
42,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

typedef long long int64;

int64 N, M;
int64 dp[5000001];

int64 f(int k)
{
  if(k <= 2) return (k - 1);
  if(~dp[k]) return (dp[k]);
  return (dp[k] = (f(k - 1) + f(k - 2)) % M);
}

int main()
{

  cin >> N >> M;
  memset(dp, -1, sizeof(dp));
  for(int i = 2; i <= 5000000; i++) f(i);
  cout << f(N) << endl;
}
0