結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー NULL502
提出日時 2025-09-23 00:07:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 620 bytes
コンパイル時間 1,892 ms
コンパイル使用メモリ 204,676 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-09-23 00:07:58
合計ジャッジ時間 2,763 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ll N,M; cin>>N>>M; N-=2;
    vector<vector<ll>> e={{1,1},{1,0}},ans={{1,0},{0,1}};
    while(N>0){
        if(N&1!=0) ans={{(ans[0][0]*e[0][0]+ans[0][1]*e[1][0])%M,(ans[0][0]*e[0][1]+ans[0][1]*e[1][1])%M},
                        {(ans[1][0]*e[0][0]+ans[1][1]*e[1][0])%M,(ans[1][0]*e[0][1]+ans[1][1]*e[1][1])%M}};
        e={{(e[0][0]*e[0][0]+e[0][1]*e[1][0])%M,(e[0][0]*e[0][1]+e[0][1]*e[1][1])%M},
           {(e[1][0]*e[0][0]+e[1][1]*e[1][0])%M,(e[1][0]*e[0][1]+e[1][1]*e[1][1])%M}};
        N>>=1;
    }
    cout<<ans[0][0]<<endl;
}
0