結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー kiyukiyu
提出日時 2020-04-20 10:00:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,178 bytes
コンパイル時間 1,120 ms
コンパイル使用メモリ 109,652 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 00:32:36
合計ジャッジ時間 1,766 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 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 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

/* Macros reg. ends here */

const ll INF = 1LL << 50;

// static const long long mod = 1000000007;

ll mod;

vvll matmul(vvll& a, vvll& b) {
  ll xx = a.size();
  ll zz = a[0].size();
  assert(zz == (ll)b.size());
  ll yy = b[0].size();
  vvll ret(xx, vll(yy));

  rep(i, xx) rep(j, yy) {
    ll val = 0;
    rep(k, zz) {
      val += a[i][k] * b[k][j];
      ret[i][j] = val % mod;
    }
  }

  return ret;
}

vvll matpow(vvll& m, ll n) {  // m^n
  ll xx = m.size();
  assert(xx == (ll)m[0].size());
  vvll ret(xx, vll(xx, 0));
  vvll cum = m;
  rep(i, xx) ret[i][i] = 1;
  while (n > 0) {
    if ((n & 1) == 1) ret = matmul(cum, ret);
    if (n == 1) break;
    cum = matmul(cum, cum);
    n >>= 1;
  }
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  ll n;
  cin >> n >> mod;

  vvll fmat = {{1, 1}, {1, 0}};
  vvll nmat = matpow(fmat, n-1);
  ll ans = nmat[1][0] % mod;

  cout << ans << endl;

  return 0;
}
0