結果
問題 | No.718 行列のできるフィボナッチ数列道場 (1) |
ユーザー | TAISA_ |
提出日時 | 2020-04-05 00:40:02 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 4,786 bytes |
コンパイル時間 | 2,269 ms |
コンパイル使用メモリ | 210,552 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-03 07:51:35 |
合計ジャッジ時間 | 3,268 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,944 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,944 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,944 KB |
testcase_14 | AC | 3 ms
6,944 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 2 ms
6,940 KB |
testcase_17 | AC | 2 ms
6,940 KB |
testcase_18 | AC | 3 ms
6,940 KB |
testcase_19 | AC | 2 ms
6,944 KB |
testcase_20 | AC | 2 ms
6,940 KB |
testcase_21 | AC | 2 ms
6,940 KB |
testcase_22 | AC | 2 ms
6,940 KB |
ソースコード
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() #define eb emplace_back using namespace std; using ll = long long; using P = pair<ll, ll>; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll MOD = 1e9 + 7; template <class T> void chmin(T &a, T b) { a = min(a, b); } template <class T> void chmax(T &a, T b) { a = max(a, b); } void ok() { cerr << "ok" << endl; } //from http://noshi91.hatenablog.com/entry/2019/03/31/174006 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 u64 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; } constexpr modint &operator^=(u64 exp) { modint rhs = modint(*this); a = 1; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; using mint = modint<MOD>; template <class T> struct Matrix { vector<vector<T>> a; Matrix() {} Matrix(int n, int m) : a(n, vector<T>(m)) {} inline const vector<T> &operator[](int k) const { return a.at(k); } inline vector<T> &operator[](int k) { return a.at(k); } Matrix I(int n) { Matrix mat(n, n); for (int i = 0; i < n; i++) { mat[i][i] = 1; } return mat; } Matrix &operator+=(const Matrix &rhs) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < a[i].size(); j++) { (*this)[i][j] += rhs[i][j]; } } return (*this); } Matrix &operator-=(const Matrix &rhs) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < a[i].size(); j++) { (*this)[i][j] -= rhs[i][j]; } } return (*this); } Matrix &operator*=(const Matrix &rhs) { int n = a.size(), m = a[0].size(), p = rhs[0].size(); assert(m == rhs.a.size()); vector<vector<T>> b(n, vector<T>(p)); for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { for (int k = 0; k < m; k++) { b[i][j] += (*this)[i][k] * rhs[k][j]; } } } swap(a, b); return (*this); } Matrix &operator^=(const long long &rhs) { long long n = rhs; Matrix res = Matrix::I(a.size()); while (n > 0) { if (n & 1) { res *= (*this); } (*this) *= (*this); n >>= 1; } swap(a, res.a); return (*this); } Matrix operator+(const Matrix &rhs) const { return Matrix(*this) += rhs; } Matrix operator-(const Matrix &rhs) const { return Matrix(*this) -= rhs; } Matrix operator*(const Matrix &rhs) const { return Matrix(*this) *= rhs; } Matrix operator^(const long long &rhs) const { return Matrix(*this) ^= rhs; } }; int main() { cin.tie(0); ios::sync_with_stdio(0); ll n; cin >> n; Matrix<mint> a(6, 6), b(6, 1), res(6, 1); b[1][0] = 1, b[3][0] = 1, b[5][0] = 1; a[0][1] = 1, a[1][0] = 1, a[1][1] = 1, a[2][3] = 1, a[3][2] = 1; a[3][3] = 1, a[3][4] = 2, a[4][3] = 1, a[4][4] = 1, a[5][2] = 1, a[5][3] = 1, a[5][4] = 2, a[5][5] = 1; res = (a ^ (n - 1LL)) * b; cout << res[5][0].a << endl; }