結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー 0w10w1
提出日時 2018-09-02 21:24:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,688 bytes
コンパイル時間 2,439 ms
コンパイル使用メモリ 203,684 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 10:00:46
合計ジャッジ時間 3,481 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
vector<vector<T>> mat_mul(vector<vector<T>> a, vector<vector<T>> b) {
  vector<vector<T>> c(a.size(), vector<T>(b[0].size()));
  for (int i = 0; i < a.size(); ++i)
    for (int j = 0; j < b.size(); ++j)
      for (int k = 0; k < b[j].size(); ++k)
        c[i][k] = c[i][k] + a[i][j] * b[j][k];
  return c;
}

template<typename T>
vector<vector<T>> mat_pow(vector<vector<T>> a, long long p) {
  vector<vector<T>> r(a.size(), vector<T>(a.size()));
  for (int i = 0; i < r.size(); ++i) r[i][i] = 1;
  for (; p; p >>= 1) {
    if (p & 1) r = mat_mul(r, a);
    a = mat_mul(a, a);
  }
  return r;
}

template <int mod>
struct ModInt {
  int val;
  ModInt(int v = 0) : val((v % mod + mod) % mod) {}
  ModInt(long long v) : val((v % mod + mod) % mod) {}
  ModInt &operator=(int v) { return val = (v % mod + mod) % mod, *this; }
  ModInt &operator=(const ModInt &oth) { return val = oth.val, *this; }
  ModInt operator+(const ModInt &oth) const {
    int u = val + oth.val;
    return u < 0 ? u + mod : u;
  }
  ModInt operator*(const ModInt &oth) const {
    return 1LL * val * oth.val % mod;
  }
  ModInt operator-() const { return val > 0 ? mod - val : 0; }
};

signed main() {
  const int M = int(1e9 + 7);

  long long N;
  cin >> N;

  vector<vector<ModInt<M>>> T = {{1, 1, 0, 0, 0},
                                 {0, 1, 1, 2, 0},
                                 {0, 1, 0, 0, 0},
                                 {0, 1, 0, 1, 0},
                                 {0, 0, 0, 1, 0}};

  vector<vector<ModInt<M>>> A1 = {{1}, {1}, {1}, {1}, {0}};

  cout << mat_mul(mat_pow(T, N - 1), A1)[0][0].val << endl;

  return 0;
}
0