結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー kumakumakumakuma
提出日時 2020-05-09 11:49:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,198 bytes
コンパイル時間 1,879 ms
コンパイル使用メモリ 173,532 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-19 03:01:54
合計ジャッジ時間 2,852 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

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()
{
  ll N, mod;
  cin >> N >> mod;
  vector<vector<ll>> matrix = {{0, 1}, {1, 1}};
  vector<vector<ll>> F = {{0}, {1}};
  matrix = modmatpow(matrix, N - 2, mod);
  vector<vector<ll>> ans = modmatmul(matrix, F, mod);
  cout << ans.at(1).at(0) << "\n";
}
0