結果
問題 | No.1754 T-block Tiling |
ユーザー | Ricky_pon |
提出日時 | 2022-03-19 11:42:59 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 5,813 bytes |
コンパイル時間 | 2,305 ms |
コンパイル使用メモリ | 211,136 KB |
実行使用メモリ | 6,816 KB |
最終ジャッジ日時 | 2024-10-04 07:27:29 |
合計ジャッジ時間 | 2,464 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
ソースコード
#include <bits/stdc++.h> #include <atcoder/modint> #include <assert.h> #include <vector> template <class T> struct Matrix { std::vector<std::vector<T>> a; Matrix(int n, int m) : a(n, std::vector<T>(m, 0)) {} Matrix(int n) : a(n, std::vector<T>(n, 0)) {} int height() const { return a.size(); } int width() const { return a[0].size(); } inline const std::vector<T> &operator[](int i) const { return a[i]; } inline std::vector<T> &operator[](int i) { return a[i]; } static Matrix id(int n) { Matrix res(n); for (int i = 0; i < n; i++) res[i][i] = 1; return res; } Matrix &operator*=(const Matrix &b) { assert(width() == b.height()); std::vector<std::vector<T>> c(height(), std::vector<T>(b.width())); for (int i = 0; i < height(); ++i) { for (int k = 0; k < b.height(); ++k) { for (int j = 0; j < b.width(); ++j) { c[i][j] += a[i][k] * b[k][j]; } } } a.swap(c); return *this; } Matrix pow(long long n) const { auto x = (*this), res = id(height()); while (n) { if (n & 1) { res *= x; --n; } else { x *= x; n >>= 1; } } return res; } Matrix operator*(const Matrix &b) const { return Matrix(*this) *= b; } std::vector<T> operator*(const std::vector<T> &v) { assert(width() == (int)v.size()); std::vector<T> res(height(), 0); for (int i = 0; i < height(); ++i) { for (int j = 0; j < width(); ++j) { res[i] += a[i][j] * v[j]; } } return res; } }; template <class T> std::pair<int, bool> gauss_jordan(Matrix<T> &a) { int rnk = 0; bool swp = false; for (int j = 0; j < a.width(); ++j) { int pivot = -1; for (int i = rnk; i < a.height(); ++i) { if (a[i][j] != 0) { pivot = i; break; } } if (pivot < 0) continue; swap(a[pivot], a[rnk]); if (pivot != rnk) swp ^= true; for (int i = 0; i < a.height(); ++i) { if (i != rnk && a[i][j] != 0) { auto coef = a[i][j] / a[rnk][j]; for (int k = j; k < a.width(); ++k) { a[i][k] -= a[rnk][k] * coef; } } } ++rnk; } return {rnk, swp}; } template <class T> T determinant(Matrix<T> a) { auto [rnk, swp] = gauss_jordan(a); if (rnk < a.height()) return 0; T res = 1; for (int i = 0; i < a.height(); ++i) res *= a[i][i]; if (swp) res = -res; return res; } template <class T> std::pair<std::vector<T>, std::vector<std::vector<T>>> system_of_linear_equations(Matrix<T> a, const std::vector<T> &b) { assert(a.height() == (int)b.size()); Matrix<T> aug(a.height(), a.width() + 1); for (int i = 0; i < a.height(); ++i) { for (int j = 0; j < a.width(); ++j) { aug[i][j] = a[i][j]; } aug[i][a.width()] = b[i]; } auto rnk = gauss_jordan(a).first, rnk_aug = gauss_jordan(aug).first; std::vector<T> solution; std::vector<std::vector<T>> kernel; if (rnk < rnk_aug) return {solution, kernel}; solution.resize(a.width(), 0); std::vector<bool> used(a.width(), false); std::vector<int> pos(rnk); for (int i = 0; i < rnk; ++i) { for (int j = 0; j < a.width(); ++j) { if (aug[i][j] != 0) { solution[j] = aug[i][a.width()] / aug[i][j]; used[j] = true; pos[i] = j; break; } } } for (int j = 0; j < a.width(); ++j) { if (!used[j]) { std::vector<T> v(a.width(), 0); v[j] = 1; for (int i = 0; i < rnk; ++i) { v[pos[i]] = -aug[i][j] / aug[i][pos[i]]; } kernel.push_back(v); } } return {solution, kernel}; } #include <algorithm> #include <cassert> #include <vector> template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template <class T> T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } template <typename T> struct CoordComp { std::vector<T> v; bool sorted; CoordComp() : sorted(false) {} int size() { return v.size(); } void add(T x) { v.push_back(x); } void build() { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); sorted = true; } int get_idx(T x) { assert(sorted); return lower_bound(v.begin(), v.end(), x) - v.begin(); } T &operator[](int i) { return v[i]; } }; #define For(i, a, b) for (int i = (int)(a); (i) < (int)(b); ++(i)) #define rFor(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); --(i)) #define rep(i, n) For(i, 0, n) #define rrep(i, n) rFor(i, n, 0) #define fi first #define se second using namespace std; using lint = long long; using pii = pair<int, int>; using pll = pair<lint, lint>; using mint = atcoder::modint998244353; void solve() { int n; scanf("%d", &n); Matrix<mint> a(2); a[0][0] = a[1][0] = 2; a[0][1] = a[1][1] = 1; a = a.pow(n); printf("%u\n", a[0][0].val()); } int main() { int t; scanf("%d", &t); rep(tt, t) solve(); }