結果

問題 No.658 テトラナッチ数列 Hard
ユーザー le_panda_noirle_panda_noir
提出日時 2020-07-10 23:02:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 411 ms / 2,000 ms
コード長 1,954 bytes
コンパイル時間 904 ms
コンパイル使用メモリ 87,840 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 22:49:48
合計ジャッジ時間 3,559 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 160 ms
6,944 KB
testcase_05 AC 182 ms
6,940 KB
testcase_06 AC 221 ms
6,944 KB
testcase_07 AC 233 ms
6,944 KB
testcase_08 AC 271 ms
6,940 KB
testcase_09 AC 411 ms
6,940 KB
testcase_10 AC 406 ms
6,944 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; }
  friend ostream& operator<<(ostream& os, const MInt& n) { os<<n.val; return os; }
};
using mint = MInt<17>;

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(vector<vector<T>> _v) : width(_v.size()), height(_v[0].size()), v(_v) {};
  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() {
  int N; cin >> N;

  Matrix<mint> m(4);
  m.v[0][0] = m.v[0][1] = m.v[0][2] = m.v[0][3] = 1;
  m.v[1][0] = m.v[2][1] = m.v[3][2] = 1;

  rep(i, N) {
    ll a; cin >> a;
    if (a <= 4) {
      cout << a / 4 << endl;
      continue;
    }
    Matrix<mint> p(4, 1); p[0][0] = 1;
    cout << (m.pow(a-4) * p)[0][0] << endl;
  }

  return 0;
}
0