結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー minatominato
提出日時 2020-03-13 11:36:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 4,136 bytes
コンパイル時間 1,940 ms
コンパイル使用メモリ 177,500 KB
実行使用メモリ 8,512 KB
最終ジャッジ日時 2024-05-01 14:21:50
合計ジャッジ時間 3,038 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,512 KB
testcase_01 AC 3 ms
7,772 KB
testcase_02 AC 3 ms
7,804 KB
testcase_03 AC 3 ms
8,036 KB
testcase_04 AC 4 ms
7,636 KB
testcase_05 AC 2 ms
7,584 KB
testcase_06 AC 3 ms
7,872 KB
testcase_07 AC 3 ms
8,392 KB
testcase_08 AC 3 ms
7,444 KB
testcase_09 AC 3 ms
7,968 KB
testcase_10 AC 3 ms
8,032 KB
testcase_11 AC 3 ms
7,496 KB
testcase_12 AC 4 ms
7,320 KB
testcase_13 AC 3 ms
7,428 KB
testcase_14 AC 3 ms
7,304 KB
testcase_15 AC 4 ms
7,512 KB
testcase_16 AC 4 ms
7,464 KB
testcase_17 AC 3 ms
8,132 KB
testcase_18 AC 3 ms
8,108 KB
testcase_19 AC 4 ms
7,796 KB
testcase_20 AC 4 ms
8,512 KB
testcase_21 AC 3 ms
7,484 KB
testcase_22 AC 4 ms
8,504 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; }
///////////////////////////////////////////////////////////////////////////////////////////////////

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr 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<MOD>;

template<typename 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]);
                }
            }
        }
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                swap(A[i][j],C.A[i][j]);
            }
        }
        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;
        }
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                swap(A[i][j],B.A[i][j]);
            }
        }
        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; cin >> N;

    Matrix<mint> mat(2);

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

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