結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
コンテスト
ユーザー tomoru04
提出日時 2025-12-11 20:52:27
言語 C++17
(gcc 13.3.0 + boost 1.89.0)
結果
RE  
実行時間 -
コード長 3,443 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,937 ms
コンパイル使用メモリ 204,800 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2025-12-11 20:52:32
合計ジャッジ時間 3,911 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 RE * 2
other AC * 3 WA * 5 RE * 4
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;

template <typename T>
class Matrix {
private: 
    vector<vector<T>> A;
    int mod;

public:
    Matrix(){}
    Matrix(size_t n): A(n, vector<T>(n, T{})){ assert(n > 0); }
    Matrix(size_t h, size_t w): A(h, vector<T>(w, T{})){ assert(h > 0 && w > 0); }
    Matrix(size_t h, size_t w, int m): A(h, vector<T>(w, T{})), mod(m) { assert(h > 0 && w > 0); }
    
    size_t getHeight() const {
        return A.size();
    }

    size_t getWidth() const {
        assert(!A.empty());
        return A[0].size();
    }

    // when you read the item
    inline const vector<T> &operator[](size_t k) const {
        return A.at(k);
    }

    // when you assign the item
    inline vector<T> &operator[](size_t k){
        return A.at(k);
    }

    // Matrix I = Matrix<int>::I(n);
    static Matrix I(size_t n){
        Matrix mat_I(n);
        for(size_t i=0; i<n; ++i){
            mat_I[i][i] = T{1};
        }
        return mat_I;
    }

    Matrix &operator+=(const Matrix &B){
        size_t h = getHeight(), w = getWidth();
        assert(h == B.getHeight() && w == B.getWidth());
        for(size_t i=0; i<h; ++i) for(size_t j=0; j<w; ++j) (*this)[i][j] += B[i][j];
        return (*this);
    }

    Matrix &operator-=(const Matrix &B){
        size_t h = getHeight(), w = getWidth();
        assert(h == B.getHeight() && w == B.getWidth());
        for(size_t i=0; i<h; ++i) for(size_t j=0; j<w; ++j) (*this)[i][j] -= B[i][j];
        return (*this);
    }

    Matrix &operator*=(const Matrix &B){
        size_t h = getHeight(), w = B.getWidth(), p = getWidth();
        assert(p == B.getHeight());
        vector<vector<T>> C(h, vector<T>(w, 0));
        for(size_t i=0; i<h; ++i)
            for(size_t j=0; j<w; ++j){
                for(size_t k=0; k<p; ++k)
                    C[i][j] += ((*this)[i][k] * B[k][j]) % mod;
                C[i][j] %= mod;
            }
        A.swap(C);
        return (*this);
    }

    Matrix &operator^=(long long k){
        size_t h = getHeight();
        size_t w = getWidth();
        assert(h == w && k >= 0);

        Matrix B = Matrix::I(h);
        while(k > 0){
            if(k & 1) B *= (*this);
            (*this) *= (*this);
            k >>= 1;
        }
        A.swap(B.A);
        return (*this);
    }

    Matrix operator+(const Matrix &B) const {
        return (Matrix(*this) += B);
    }

    Matrix operator-(const Matrix &B) const {
        return (Matrix(*this) -= B);
    }

    Matrix operator*(const Matrix &B) const {
        return (Matrix(*this) *= B);
    }

    Matrix operator^(const long long k) const {
        return (Matrix(*this) ^= k);
    }

    friend ostream &operator<<(ostream &os, const Matrix &p){
        size_t h = p.getHeight();
        size_t w = p.getWidth();
        for(size_t i=0; i<h; ++i){
            for(size_t j=0; j<w; ++j){
                os << p[i][j] << (j == w-1 ? "\n" : ", ");
            }
        }
        return(os);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, m; cin >> n >> m;

    Matrix<ll> F(2, 1, m);
    Matrix<ll> M(2, 2, m);

    F[0][0] = 0;
    F[1][0] = 1;

    M[0][0] = 1;
    M[0][1] = 1;
    M[1][0] = 1;
    M[1][1] = 0;

    M ^= (n-1);

    Matrix<ll> ans = M * F;

    cout << ans[0][0] << endl;
}
0