結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー muminmumin
提出日時 2020-09-16 03:15:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 952 bytes
コンパイル時間 1,719 ms
コンパイル使用メモリ 169,636 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 03:16:14
合計ジャッジ時間 2,662 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int m, mod;

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

vector<vector<ll>> update(vector<vector<ll>> &mt){
    vector<vector<ll>> ret(m,vector<ll>(m,0));
    rep(i,m) rep(j,m) rep(k,m) { ret[i][j] += mt[i][k]*mt[k][j]; ret[i][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(2);
    vector<vector<ll>> mt(2, vector<ll>(2));
    dp[0] = 1; dp[1] = 0;
    mt[0][0] = mt[0][1] = mt[1][0] = 1; mt[1][1] = 0;
    
    matpow(dp, mt, n-1);
    cout << dp[1] << endl;
}
0