結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー tkht 7tkht 7
提出日時 2020-11-10 17:10:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,214 bytes
コンパイル時間 837 ms
コンパイル使用メモリ 85,532 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 23:40:39
合計ジャッジ時間 1,828 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;
using vec = vector<ll>;
using mat = vector<vector<ll>>;

class matpow {
  ll m, MOD; // 遷移行列のサイズ, mod
  // DPの更新
  vec matmul(vec &dp, mat &mt){
    vec ret(m, 0);
    rep(i, m)rep(j, m){
      ret[i] += mt[i][j] * dp[j];
      ret[i] %= MOD;
    }
    return ret;
  }
  // 遷移行列の更新
  mat update(mat &mt){
    mat ret(m, vec(m,0));
    rep(i, m)rep(j, m)rep(k, m){
      ret[i][j] += mt[i][k] * mt[k][j];
      ret[i][j] %= MOD;
    }
    return ret;
  }
  
public:
  // 行列の大きさはm×m
  matpow(ll m, ll MOD) : m(m), MOD(MOD) {};
  void calc(vec &dp, mat &mt, ll k){
    while(k){
      if (k&1) dp = matmul(dp,mt);
      mt = update(mt);
      k /= 2;
    }
  }
};

int main(){
  ll n, m;
  cin >> n >> m;
  vec dp(2);
  mat mt(2, vec(2));
  
  dp[0] = dp[1] = 1;
  mt[0][0] = mt[0][1] = mt[1][0] = 1;
  mt[1][1] = 0;
  matpow mp(2, m);
  mp.calc(dp,mt,n-2);
  cout << dp[1] << endl;
  
  return 0;
}
0