結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー koutykkkkoutykkk
提出日時 2021-10-11 21:14:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,259 bytes
コンパイル時間 2,415 ms
コンパイル使用メモリ 214,184 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 06:55:59
合計ジャッジ時間 3,372 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using pl = pair<ll,ll>;
using pi = pair<int,int>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const int INF = ((1<<30)-1);
const long long LINF = (1LL<<60);
const double PI = 3.141592653589793238;
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;}return 0;}
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;}return 0;}
//(a+b-1)/b

template<int MOD> struct Fp {  
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator > (const Fp& r) const noexcept {
		return this->val > r.val;
	}
	constexpr bool operator < (const Fp& r) const noexcept {
		return this->val < r.val;
	}
	constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
using mint = Fp<MOD>;


struct ModMatrix{
    using V = vector<vector<long long>>;
    V mat;
    int mod;
    int R,C;
    ModMatrix(int n, int m, int M){
        R = n;
        C = m;
        mod = M;
        mat.resize(n);
        for(int i = 0; i < n; i++)mat[i].resize(m);
    }

    ModMatrix& operator *=(ModMatrix& A){
        V B(R , vector<long long>(C));
        for(int i = 0; i < R; i++){
            for(int j = 0; j < A.C; j++){
                for(int k = 0; k < C; k++){
                    B[i][j] += (mat[i][k] * A.mat[k][j]) % mod;
                    B[i][j] %= mod;
                }
            }
        }
        mat = B;
        C = A.C;
        return *this;
    }
    
    ModMatrix operator * (ModMatrix& A){
        return (ModMatrix(*this) *= A);
    }

    friend ModMatrix pow(ModMatrix &A , long long n){
        if(n == 0){
            ModMatrix I(A.R , A.R , A.mod);
            for(int i = 0; i < A.R; i++)I.mat[i][i] = 1;
            return I;
        }
        auto t = pow(A , n/2);
        t *= t;
        if(n & 1)t *= A;
        return t;
    }
};


long long solve(){
    ll n,m; cin >> n >> m;
    ModMatrix A(2 , 2 , m);
    ModMatrix B(2 , 1 , m);
    A.mat = {{1,1} , {1,0}};
    B.mat = {{1} , {0}};
    auto C = pow(A , n-1);
    C *= B;
    cout << C.mat[1][0] << endl;
    

    return 0;
}

signed main(){
    cout << fixed << setprecision(10);
    ios::sync_with_stdio(0), cin.tie(0);
    int t = 1; 
    //cin >> t;
    for(int i = 0; i < t; i++){
        solve();
    }
    

	return 0;
}
0