結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー xecctxecct
提出日時 2021-08-25 19:36:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,082 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 166,252 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 11:04:19
合計ジャッジ時間 2,321 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

vector<vector<i64>> mat_mul_mod(vector<vector<i64>> a, vector<vector<i64>> b,i64 mod){
    vector<vector<i64>> res(2,vector<i64>(2,0));
    res.at(0).at(0) = ((a.at(0).at(0)*b.at(0).at(0))%mod + (a.at(0).at(1)*b.at(1).at(0))%mod)%mod;
    res.at(0).at(1) = ((a.at(0).at(0)*b.at(0).at(1))%mod + (a.at(0).at(1)*b.at(1).at(1))%mod)%mod;
    res.at(1).at(0) = ((a.at(1).at(0)*b.at(0).at(0))%mod + (a.at(1).at(1)*b.at(1).at(0))%mod)%mod;
    res.at(1).at(1) = ((a.at(1).at(0)*b.at(0).at(1))%mod + (a.at(1).at(1)*b.at(1).at(1))%mod)%mod;
    return res;
}

int main(){
    i64 N,M;
    cin >> N >> M;
    i64 key=N-2;
    vector<vector<i64>> mat_key = {
        {0,1},
        {1,1}
    };
    vector<vector<i64>> mat_res = {
        {1,0},
        {0,1}
    };
    
    while (key>0){
        if (key&1) mat_res = mat_mul_mod(mat_key, mat_res, M);
        key = key >> 1;
        mat_key = mat_mul_mod(mat_key,mat_key, M);
    }

    cout << mat_res.at(1).at(1) << endl;
    

}
0