結果
| 問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める | 
| コンテスト | |
| ユーザー |  mumin | 
| 提出日時 | 2020-09-16 03:15:35 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 952 bytes | 
| コンパイル時間 | 1,675 ms | 
| コンパイル使用メモリ | 172,576 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-06-22 02:58:50 | 
| 合計ジャッジ時間 | 2,241 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 | 
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
int m, mod;
vector<ll> matmul(vector<ll> &dp, vector<vector<ll>> &mt){
    vector<ll> ret(m,0);
    rep(i,m) rep(j,m){ ret[i] += mt[i][j]*dp[j]; ret[i] %= mod; }
    return ret;
}
vector<vector<ll>> update(vector<vector<ll>> &mt){
    vector<vector<ll>> ret(m,vector<ll>(m,0));
    rep(i,m) rep(j,m) rep(k,m) { ret[i][j] += mt[i][k]*mt[k][j]; ret[i][j] %= mod; }
    return ret;
}
void matpow(vector<ll> &dp, vector<vector<ll>> &mt, int k){
    m = dp.size();
    while(k){
        if(k&1) dp = matmul(dp,mt);
        mt = update(mt);
        k /= 2;
    }
}
int main(){
    int n;
    cin >> n >> mod;
    vector<ll> dp(2);
    vector<vector<ll>> mt(2, vector<ll>(2));
    dp[0] = 1; dp[1] = 0;
    mt[0][0] = mt[0][1] = mt[1][0] = 1; mt[1][1] = 0;
    
    matpow(dp, mt, n-1);
    cout << dp[1] << endl;
}
            
            
            
        