結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
コンテスト
ユーザー e2412_久米 琉惺
提出日時 2026-03-31 01:23:22
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 977 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,215 ms
コンパイル使用メモリ 342,184 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2026-03-31 01:23:26
合計ジャッジ時間 3,160 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(T) T.begin(), T.end()
using ll = long long;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

#define mat vector<vector<ll>>
ll MOD;

mat mat_mul(mat &a,mat &b){
    mat res(a.size(),vector<ll>(b[0].size()));
    for(int i=0;i<a.size();i++){
        for(int j=0;j<b[0].size();j++){
            for(int k=0;k<b.size();k++){
                (res[i][j]+=a[i][k]*b[k][j])%=MOD;
            }
        }
    }
    return res;
}

mat mat_pow(mat a,ll n){
    mat res(a.size(),vector<ll>(a.size()));
    for(ll i=0;i<a.size();i++)res[i][i]=1;
    while(n>0){
        if(n&1)res=mat_mul(a,res);
        a=mat_mul(a,a);
        n>>=1;
    }
    return res;
}

int main() {
    ll n; cin>>n>>MOD;
    mat m(2,vector<ll>(2));
    m[0][0]=m[0][1]=m[1][0]=1;
    m[1][1]=0;
    m=mat_pow(m,n-1);
    ll ans=m[1][0];
    cout<<ans<<endl;
}
0