結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー kk
提出日時 2020-11-29 16:41:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,104 bytes
コンパイル時間 1,585 ms
コンパイル使用メモリ 172,004 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-11 02:28:31
合計ジャッジ時間 2,737 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORR(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()

using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VD = vector<double>;
using VII = vector<VI>;
using VLL = vector<VL>;
using VDD = vector<VD>;
using P = pair<int, int>;
using PL = pair<ll, ll>;

VLL matmul(VLL A, VLL B, int m) {
  VLL C(A.size(), VL(B[0].size()));
  REP(i, A.size()) {
    REP(j, B[0].size()) {
      REP(k, B.size()) {
        C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % m;
      }
    }
  }
  return C;
}

VLL pow(VLL A, int n, int m) {
  VLL E(2, VL(2, 0));
  E[0][0] = 1;
  E[1][1] = 1;
  VLL ans = E;
  while(n > 0) {
    if(n % 2 == 1) ans = matmul(A, ans, m);
    A = matmul(A, A, m);
    n /= 2;
  }
  return ans;
}

int main() {
  int n, m;
  cin >> n >> m;
  VLL A(2, VL(2));
  A[0][0] = 1;
  A[0][1] = 1;
  A[1][0] = 1;
  A[1][1] = 0;
  A = pow(A, n - 1, m);
  cout << A[1][0] << endl;
}
0