結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー Joe75792433Joe75792433
提出日時 2020-12-21 22:40:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,040 bytes
コンパイル時間 2,370 ms
コンパイル使用メモリ 208,208 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 11:54:50
合計ジャッジ時間 3,170 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;
#define rep(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define in(x, l, r) (ll)(l) <= (x) && (x) < (ll)(r)

int m, MOD;

vector<ll> matmul(vector<ll> &dp, vector<vector<ll>> &mt) {
    vector<ll> ret(m,0);
    rep(i,0,m) rep(j,0,m) ret[i] = (ret[i] + mt[i][j] * dp[j]) % MOD;
    return ret;
}

vector<vector<ll>> update(vector<vector<ll>> &mt) {
    vector<vector<ll>> ret(m, vector<ll>(m,0));
    rep(i,0,m) rep(j,0,m) rep(k,0,m) ret[i][j] = (ret[i][j] + mt[i][k] * mt[k][j]) % MOD;
    return ret;
}

void matpow(vector<ll> &dp, vector<vector<ll>> &mt, int k) {
    m = dp.size();
    while (k) {
        if (k&1) dp = matmul(dp,mt);
        mt = update(mt);
        k /= 2;
    }
}

int main() {
    int N;
    cin >> N >> MOD;

    vector<ll> dp = {1, 0};
    vector<vector<ll>> mt = {{1,1}, {1,0}};
    matpow(dp,mt,N-2);
    cout << dp[0] << endl;
}
0