結果

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

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long int;
ll MOD;
vector<ll> times(vector<ll> &memo, vector<vector<ll>> &matrix){
    vector<ll> ans(memo.size(), 0);
    for(int i = 0; i < memo.size(); i++){
        for(int j = 0; j < memo.size(); j++){
            ans[i] += memo[j]*matrix[i][j];
            ans[i] %= MOD;
        }
    }
    return ans;
}

vector<vector<ll>> update(vector<vector<ll>> &matrix){
    vector<vector<ll>> ans(matrix.size(), vector<ll>(matrix.size(), 0));
    for(int i = 0; i < matrix.size(); i++){
        for(int j = 0; j < matrix.size(); j++){
            for(int k = 0; k < matrix.size(); k++){
                ans[i][j] += matrix[i][k]*matrix[k][j];
                ans[i][j] %= MOD;
            }
        }
    }
    return ans;
}

void matrixpow(vector<ll> &memo, vector<vector<ll>> &matrix, int n){
    while(n){
        if(n & 1){
            memo = times(memo, matrix);        
        }
        matrix = update(matrix);
        n >>= 1;
    }
}

int main(){
    int n;
    cin >> n >> MOD;
    //MOD = 10000000;
    vector<vector<ll>> matrix(2, vector<ll>(2));
    vector<ll> memo(2);
    matrix[0][0] = matrix[0][1] = matrix[1][0] = 1;
    matrix[1][1] = 0;
    memo[0] = 1;
    memo[1] = 0;
    matrixpow(memo, matrix, n-1);
    cout << memo[1] << endl;
    return 0;
}
0