結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー minatominato
提出日時 2020-03-13 10:58:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 3,927 bytes
コンパイル時間 1,734 ms
コンパイル使用メモリ 178,204 KB
実行使用メモリ 9,364 KB
最終ジャッジ日時 2024-05-01 13:15:09
合計ジャッジ時間 2,592 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
9,340 KB
testcase_01 AC 10 ms
9,220 KB
testcase_02 AC 10 ms
9,268 KB
testcase_03 AC 8 ms
9,312 KB
testcase_04 AC 12 ms
9,296 KB
testcase_05 AC 9 ms
9,336 KB
testcase_06 AC 11 ms
9,164 KB
testcase_07 AC 16 ms
9,364 KB
testcase_08 AC 19 ms
9,164 KB
testcase_09 AC 23 ms
9,196 KB
testcase_10 AC 25 ms
9,224 KB
testcase_11 AC 29 ms
9,316 KB
testcase_12 AC 29 ms
9,252 KB
testcase_13 AC 31 ms
9,276 KB
testcase_14 AC 29 ms
9,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define ln '\n'
const long long MOD = 1000000007LL;
//const long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull; 
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
///////////////////////////////////////////////////////////////////////////////////////////////////

//modint::set(M)
struct modint {
    using u64 = std::uint_fast64_t;

    public:
    u64 a;

    modint(u64 x = 0) : a(x >= 0 ? x%Modulus() : (Modulus() - (-x)%Modulus()) % Modulus()) {}

    static u64 &Modulus() {static u64 Modulus = 0; return Modulus;}

    static void set (u64 M) {Modulus() = M;}

    modint operator+(const modint rhs) {
        return modint(*this) += rhs;
    }
    modint operator-(const modint rhs) {
        return modint(*this) -= rhs;
    }
    modint operator*(const modint rhs) {
        return modint(*this) *= rhs;
    }
    modint operator/(const modint rhs) {
        return modint(*this) /= rhs;
    }
    modint &operator+=(const modint rhs) {
        a += rhs.a;
        if (a >= Modulus()) {
            a -= Modulus();
        }
        return *this;
    }
    modint &operator-=(const modint rhs) {
        if (a < rhs.a) {
            a += Modulus();
        }
        a -= rhs.a;
        return *this;
    }
    modint &operator*=(modint rhs) {
        a = a * rhs.a % Modulus();
        return *this;
    }
    modint &operator/=(modint rhs) noexcept {
        u64 exp = Modulus() - 2;
        while (exp) {
        if (exp % 2) {
            *this *= rhs;
        }
        rhs *= rhs;
        exp /= 2;
        }
        return *this;
    }
};

using mint = modint;

template<class T>
struct Matrix {
   	array<array<T, 500>, 500> A;
	int N;

   	Matrix() {}
   	Matrix(int N) : N(N) {}

	inline const array<T, 500> &operator[](int k) const {return A[k];}
  	inline array<T, 500> &operator[](int k) {return A[k];}

  	static Matrix I(int N) {
    	Matrix<T> mat(N);
    	for(int i = 0; i < N; ++i) mat[i][i] = 1;
    	return mat;
  	}

  	Matrix &operator+=(const Matrix &B) {
    	for(int i = 0; i < N; i++) {
      	    for(int j = 0; j < N; j++) {
        	    (*this)[i][j] += B[i][j];
            }
        }
    	return (*this);
  	}

  	Matrix &operator-=(const Matrix &B) {
    	for(int i = 0; i < N; i++) {
      		for(int j = 0; j < N; j++) {
				(*this)[i][j] -= B[i][j];
			}
		}
    	return (*this);
  	}

    Matrix &operator*=(const Matrix &B) {
        Matrix<T> C(N);
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N; j++) {
                for(int k = 0; k < N; k++) {
                    C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);
                }
            }
        }
        A.swap(C.A);
        return *this;
    }

    Matrix &operator^=(long long k) {
        Matrix<T> B = Matrix<T>::I(N);
        while(k > 0) {
            if(k & 1) B *= *this;
            *this *= *this;
            k >>= 1LL;
        }
        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);
    }
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    ll N,M; cin >> N >> M;

    modint::set(M);
    Matrix<mint> mat(2);

    mat[0][0] = mat[0][1] = mat[1][0] = 1;
    mat ^= N-1;

    cout << mat[1][0].a << ln;
}
0