結果
| 問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める | 
| コンテスト | |
| ユーザー |  眼精疲労がやばい | 
| 提出日時 | 2020-07-04 08:19:40 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 1,674 bytes | 
| コンパイル時間 | 810 ms | 
| コンパイル使用メモリ | 102,812 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-09-17 13:46:14 | 
| 合計ジャッジ時間 | 1,430 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 | 
ソースコード
#include <iostream>
#include <iomanip>
#include <bitset>
#include <string>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<tuple>
#include<sstream>
#include<functional>
#include<list>
#include<queue>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
typedef pair<ll, ll> p;
typedef vector<ll> vec;
using Graph = vector<vector<ll>>;
using graph = vector<vector<ll>>;
const int inf = 1e9+7;
const long long MOD = 1000000007;
ll M;
int m;//遷移行列のサイズ
//dpの更新
vec matmul(vec& dp, Graph& G) {
    vec ret(m, 0);
    rep(i, m) {
        rep(j, m) {
            ret[i] += (G[i][j] * dp[j])%M;
        }
        ret[i] %= M;
    }
    return ret;
}
//遷移行列の更新
Graph update(Graph& G) {
    Graph ret(m, vec(m, 0));
    rep(i, m) {
        rep(j, m) {
            rep(k, m)ret[i][j] += (G[i][k] * G[k][j])%M;
            ret[i][j] %= M;
        }
    }
    return ret;
}
void matpow(vec& dp, Graph& G, int k) {
    m = dp.size();
    while (k) {
        if (k & 1)dp = matmul(dp, G);
        G = update(G);
        k /= 2;
    }
}
int main() {
    ll n;
    cin >> n >> M;
    vec dp{ 1,0 };
    Graph G(2, vec(2));
    G[0][0] = 1; G[0][1] = 1; G[1][0] = 1; G[1][1] = 0;
    matpow(dp, G, n-1);
    cout << dp[1] << endl;
    return 0;
}
            
            
            
        