結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー tsutajtsutaj
提出日時 2018-04-29 15:17:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,911 bytes
コンパイル時間 1,052 ms
コンパイル使用メモリ 85,332 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 08:03:23
合計ジャッジ時間 2,567 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <cassert>
#include <sstream>
#include <string>
#include <climits>
using namespace std;
using ll = long long int;
long long int N, MOD;

// ModInt begin

ll mod_pow(ll x, ll n) {return (!n)?1:(mod_pow((x*x)%MOD,n/2)*((n&1)?x:1))%MOD;}
struct ModInt {
    ll v;
    ModInt(ll a = 0) : v(((a%MOD) + MOD) % MOD) {}
    ModInt  operator+ ( const ModInt& b ) const {return (v + b.v) % MOD;}
    ModInt  operator- ( const ModInt& b ) const {return (v - b.v + MOD) % MOD;}
    ModInt  operator* ( const ModInt& b ) const {return (v * b.v) % MOD;}
    ModInt  operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, MOD-2)) % MOD;}
};

bool    operator==(ModInt  a, ModInt b) {return a.v == b.v;}
ModInt& operator+=(ModInt& a, ModInt b) {return a = a + b;}
ModInt& operator-=(ModInt& a, ModInt b) {return a = a - b;}
ModInt& operator*=(ModInt& a, ModInt b) {return a = a * b;}
ModInt& operator/=(ModInt& a, ModInt b) {return a = a / b;}
ostream& operator<<(ostream& out, ModInt  a) {return out << a.v;}
istream& operator>>(istream& in,  ModInt& a) {
    ll v; in >> v;
    a = ModInt(v);
    return in;
}

// ModInt end

template<typename Type>
struct Matrix {
private:
    int R, C;
public:
    std::vector< std::vector<Type> > mat;
    Matrix (int R_, int C_, bool is_identity=false) {
        R = R_, C = C_;
        mat = std::vector< std::vector<Type> >(R, std::vector<Type>(C));
        if(is_identity) {
            assert(R == C);
            for(int i=0; i<R; i++) {
                mat[i][i] = 1;
            }
        }
    }
    int get_row() const {
        return mat.size();
    }
    int get_col() const {
        assert(mat.size() > 0);
        return mat[0].size();
    }
    std::vector<Type>& operator[](int x) {
        return mat[x];
    }
    Matrix& operator=(const Matrix &a) {
        mat = a.mat;
        return *this;
    }
    Matrix operator*(Matrix a) {
        assert(this->get_col() == a.get_row());
        int R = this->get_row(), C = a.get_col(), X = this->get_col();
        Matrix<Type> ret(R, C);

        for(int i=0; i<R; i++) {
            for(int j=0; j<C; j++) {
                for(int k=0; k<X; k++) {
                    ret[i][j] += ((*this)[i][k] * a[k][j]);
                }
            }
        }
        return ret;
    }
};

template<typename Type>
Matrix<Type> pow(Matrix<Type> a, long long int k) {
    assert(a.get_row() == a.get_col());
    Matrix<Type> ret(a.get_row(), a.get_col(), true);
    for(; k>0; k>>=1) {
        if(k & 1) ret = ret * a;
        a = a * a;
    }
    return ret;
}

int main() {
    std::cin >> N >> MOD;
    Matrix<ModInt> P(2, 2);
    P[0][0] = 1;
    P[0][1] = 1;
    P[1][0] = 1;
    P[1][1] = 0;
    P = pow(P, N-2);

    Matrix<ModInt> ans(2, 1);
    ans[0][0] = 1;
    ans[1][0] = 0;
    ans = P * ans;

    std::cout << ans[0][0] << std::endl;
    return 0;
}
0