結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー 59635963
提出日時 2021-03-21 20:13:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,005 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 174,096 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-14 16:19:48
合計ジャッジ時間 2,735 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

vector<vector<ll>> mat_mul(vector<vector<ll>> a, vector<vector<ll>> b, ll m)
{
  vector<vector<ll>> c(a.size(), vector<ll>(b[0].size(), 0));
  for (int i = 0; i < a.size(); i++)
  {
    for (int k = 0; k < b[0].size(); k++)
    {
      for (int j = 0; j < b.size(); j++)
      {
        c[i][k] = (c[i][k] + (a[i][j] * b[j][k]) % m) % m;
      }
    }
  }
  return c;
}

vector<vector<ll>> mat_pow(vector<vector<ll>> x, ll n, ll m)
{
  vector<vector<ll>> y(x.size(), vector<ll>(x.size(), 0));
  for (int i = 0; i < x.size(); i++)
  {
    y[i][i] = 1;
    while (n)
    {
      if (n & 1)
      {
        y = mat_mul(x, y, m);
      }
      x = mat_mul(x, x, m);
      n >>= 1;
    }
  }
  return y;
}

int main()
{
  ll n, m;
  cin >> n >> m;
  vector<vector<ll>> fib = {{0, 1}};
  vector<vector<ll>> t = {{0, 1}, {1, 1}};
  vector<vector<ll>> y = mat_pow(t, n - 1, m);
  y = mat_mul(fib, y, m);
  cout << y[0][0] << endl;
  return 0;
}
0