結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー le_panda_noirle_panda_noir
提出日時 2020-06-13 20:33:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,866 bytes
コンパイル時間 818 ms
コンパイル使用メモリ 83,920 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-08 03:13:21
合計ジャッジ時間 1,812 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

#define rep(i,n) for(int i=0,_i=(n);i<_i;++i)
template<int MOD> struct MInt {
  long long val;
  constexpr MInt(long long val = 0) : val(val % MOD) { if (val < 0) val += MOD; }
  MInt operator+(const MInt& n) const { return MInt(val) += n; }
  MInt operator*(const MInt& n) const { return MInt(val) *= n; }
  MInt& operator+=(const MInt& n) { val = (val + n.val) % MOD; return *this; }
  MInt& operator*=(const MInt& n) { val = (val * n.val) % MOD; return *this; }
  MInt pow(long long n) const {
    MInt pow = val, ans = 1;
    for (long long p = n; p > 0; p >>= 1, pow *= pow)
      if (p & 1) ans *= pow;
    return ans;
  }
  friend ostream& operator<<(ostream& os, const MInt& n) { os<<n.val; return os; }
};
constexpr int MOD = 1e9+7;
using mint = MInt<MOD>;

template<class T> struct Matrix {
  int width, height;
  vector<vector<T>> v;
  Matrix(int w) : width(w), height(w), v(w, vector<T>(w, 0)) {};
  Matrix(int w, int h) : width(w), height(h), v(w, vector<T>(h, 0)) {};
  Matrix operator*(const Matrix& n) const { Matrix ans = v; return ans *= n; }
  Matrix& operator*=(const Matrix& rhs) {
    Matrix<T> ans(height, rhs.width);
    for (int i = 0; i < height; ++i)
      for (int j = 0; j < rhs.width; ++j)
        for (int k = 0; k < width; ++k)
          ans[i][j] += v[i][k] * rhs.v[k][j];
    width = rhs.width;
    v = ans.v;
    return *this;
  }
  Matrix pow(ll N) {
    Matrix pow = *this, ans(width);
    for (int i = 0; i < width; ++i) ans[i][i] = 1;
    for (ll p = N; p > 0; p >>= 1, pow *= pow)
      if (p & 1) ans *= pow;
    return ans;
  }
  vector<T>& operator[](int i) { return v[i]; }
};

int main() {
  ll N; cin >> N;

  Matrix<mint> f(2);
  f[0][0] = 1, f[0][1] = 1, f[1][0] = 1;
  f = f.pow(N);
  cout << f[0][0] * f[1][0] << endl;

  return 0;
}
0