結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー harogen_cubeharogen_cube
提出日時 2022-10-08 01:53:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,659 bytes
コンパイル時間 2,468 ms
コンパイル使用メモリ 208,904 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 19:30:36
合計ジャッジ時間 3,426 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll M;

vector<vector<ll>> mul(vector<vector<ll>> vec, vector<vector<ll>> vec2){
        if(vec.size()!=vec2[0].size())return {};
        vector<vector<ll>> vec3;
        vec3.resize(vec2.size());
        for(int i = 0 ; i < vec[0].size() ; i++){
                for(int j = 0 ; j < vec2.size() ; j++){
                        vec3[j].resize(vec[0].size());
                        ll ans = 0;
                        for(int k = 0 ; k < vec.size() ; k++){
                                ans += vec[k][i]*vec2[j][k];
                                ans %= M;
                        }
                        vec3[j][i] = ans;
                }
        }

        return vec3;
}
vector<vector<ll>> add(vector<vector<ll>> vec, vector<vector<ll>> vec2){
        if(vec.size()!=vec2.size())return {};
        if(vec[0].size()!=vec2[0].size())return {};
        vector<vector<ll>> vec3;
        vec3.resize(vec.size());
        for(int i = 0 ; i < vec.size() ; i++){
                vec3[i].resize(vec[0].size());
                for(int j = 0 ; j < vec[0].size() ; j++){
                        vec3[i][j] = vec[i][j]+vec2[i][j];
                        vec3[i][j] %= M;
                }
        }
        return vec3;
}  

int main(){
        ll N;
        cin >> N >> M;
        vector<vector<ll>> pow = {{0,1},{1,1}};
        vector<vector<ll>> ans = {{0},{1}};
        N--;
        for(int i = 0 ; i <= 60 ; i++){
                if(N>>i & 1){
                        ans = mul(ans,pow);
                }
                pow = mul(pow,pow);
        }
        cout << ans[0][0] << endl;
}
0