結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー shiroha_F14shiroha_F14
提出日時 2021-12-29 22:06:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,311 bytes
コンパイル時間 4,163 ms
コンパイル使用メモリ 259,900 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 04:09:21
合計ジャッジ時間 4,657 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

#define fs(n) fixed << setprecision(n)
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define pdesc(t) t, vector<t>, greater<t>
using ll = long long;
using ld = long double;
#define query(t) while(t--)
#define aryin(a, n) for(int i = 0; i < n; i++) cin >> a[i];


#define mint modint

#define mtrx vector<vector<mint>>
mtrx mtrxMul(mtrx &a, mtrx &b) {
  mtrx res(a.size(), vector<mint>(b[0].size()));
  for(int i = 0; i < a.size(); i++){
    for(int j = 0; j < b[0].size(); j++){
      for(int k = 0; k < b.size(); k++){
        res[i][j] += a[i][k] * b[k][j];
      }
    }
  }
  return res;
}
mtrx mtrxPow(mtrx a, ll n){
  mtrx res(a.size(), vector<mint>(a.size()));
  for(int i = 0; i < a.size(); i++){
    res[i][i] = 1;
  }
  while(n > 0){
    if(n & 1) res = mtrxMul(a, res);
    a = mtrxMul(a, a);
    n >>= 1;
  }
  return res;
}
ostream& operator<< (ostream& os, const mint& v){
  os << v.val();
  return os;
}

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int n, m; cin >> n >> m;
  mint::set_mod(m);

  mtrx mx(2, vector<mint>(2, 1));
  mx[1][1] = 0;

  mtrx res = mtrxPow(mx, n - 2);
  cout << res[0][0] << endl;
}
0