結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー yamachasoyamachaso
提出日時 2022-08-03 16:19:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,201 bytes
コンパイル時間 4,280 ms
コンパイル使用メモリ 265,272 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-11 10:44:51
合計ジャッジ時間 5,308 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <atcoder/all> // g++ main.cpp -std=c++17 -I .
using namespace std;
using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}
template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}
template<class T> void my_printv(vector<T> v,bool endline = true){
  if(!v.empty()){ for(size_t i{}; i<v.size()-1; i++) cerr<<v[i]<<' '; cerr<<v.back(); }
  if(endline) cerr<<endl;
}
using ll = long long;

const int inf = INT_MAX / 2; const ll INF = 1LL << 60;
const ll MOD = 1000000007;
// const int MOD = 998244353;

// cout << fixed << setprecision(15) << ans << endl;
// using mint = modint1000000007;
using mint = modint998244353;
// using mint = modint;

////////////////////////////////////////////////////////////////////////////////////////////


ll n,m;

template<class T>
struct Matrix {
	int sz;
  vector<vector<T>> x;
  Matrix(int n) : sz(n) {
    x.assign(sz, vector<T> (sz, (T)0));
  }
  vector<T> &operator [] (int i) {
    return x[i];
  }
  Matrix<T> operator*(const Matrix& other) const { 
    Matrix C(sz);
    for (int i = 0; i < sz; i++) {
      for (int j = 0; j < sz; j++) {
        for (int k = 0; k < sz; k++) {
          C.x[i][j] = (C.x[i][j] + x[i][k] * other.x[k][j])%m;
        }
      }
    }
    return C;
  }
  Matrix pow(long long n) {
    Matrix Res(sz);
    Matrix Pow = *this;
    for (int i = 0; i < sz; i++) {
		  for (int j = 0; j < sz; j++) Res.x[i][j] = (i == j);
	  }
    while (n > 0) {
      if (n & 1) Res = Res * Pow;
      Pow = Pow * Pow;
      n >>= 1;
    }
    return Res;
  }
};


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

  Matrix<ll> mat(2);
  mat[0][0] = 1, mat[0][1] = 1, mat[1][0] = 1, mat[1][1] = 0;
  cin>>n>>m;
  auto ans = mat.pow(n-1)[1][0];
  cout << ans << endl;

  // vector<ll> dp(2);
  // dp[0] = 0, dp[1] = 1;
  // rep(i, n-1) {
  //   ll t = dp[1];
  //   dp[1] = (dp[0] + dp[1])%m;
  //   dp[0] = t;
  // }
  // cout << dp[0] << endl;
}
0