結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー kumakumaaaaakumakumaaaaa
提出日時 2020-08-14 07:29:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,302 bytes
コンパイル時間 1,867 ms
コンパイル使用メモリ 171,984 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 18:09:34
合計ジャッジ時間 2,878 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;

//=============modmatpow============================
vector<vector<ll>> modmatmul(vector<vector<ll>>& a, vector<vector<ll>>& b, ll mod)
{
  vector<vector<ll>> res(a.size(), vector<ll>(b.at(0).size()));
  for (ll row = 0; row < a.size(); ++row) {
    for (ll colom = 0; colom < b.at(0).size(); ++colom) {
      ll sum = 0;
      for (ll i = 0; i < b.size(); ++i) {
        sum = (sum  + a.at(row).at(i) * b.at(i).at(colom) % mod) % mod;
      }
      res.at(row).at(colom) = sum;
    }
  }
  return res;
}

vector<vector<ll>> modmatpow(vector<vector<ll>> a, ll power, ll mod)
{
  vector<vector<ll>> res(a.size(), vector<ll>(a.size(), 0));
  for (ll i = 0; i < a.size(); ++i) {
    res.at(i).at(i) = 1;
  }
  while (power) {
    if (power & 1) {
      res = modmatmul(a, res, mod);
    }
    a = modmatmul(a, a, mod);
    power = (power >> 1);
  }
  return res;
}
//=================================================

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, M;
  cin >> N >> M;

  vector<vector<ll>> a = {{0, 1}, {1, 1}};
  a = modmatpow(a, N - 1, M);
  vector<vector<ll>> b = {{0}, {1}};
  vector<vector<ll>> ans = modmatmul(a, b, M);
  cout << ans.at(0).at(0) << "\n";
}
0