結果
問題 | No.2529 Treasure Hunter |
ユーザー | ochiaigawa |
提出日時 | 2023-11-03 23:08:27 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,128 bytes |
コンパイル時間 | 2,553 ms |
コンパイル使用メモリ | 219,320 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-25 21:23:07 |
合計ジャッジ時間 | 3,142 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | WA | - |
testcase_02 | AC | 3 ms
5,376 KB |
testcase_03 | AC | 4 ms
5,376 KB |
testcase_04 | AC | 3 ms
5,376 KB |
testcase_05 | AC | 3 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 3 ms
5,376 KB |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
ソースコード
#include <bits/stdc++.h> #include <atcoder/modint> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; using mint = atcoder::modint998244353; template <typename T> class Matrix { public: Matrix() {} explicit Matrix(int N) : Matrix(N, N) {} explicit Matrix(int H, int W) : mat(H, vector<T>(W)) {} int height() const { return (int) mat.size(); } int width() const { return (int) mat[0].size(); } const std::vector<T> &operator[](int k) const { return mat[k]; } std::vector<T> &operator[](int k) { return mat[k]; } static inline Matrix I(int N) { Matrix ret(N); for(int i = 0; i < N; i++) ret[i][i] = T(1); return ret; } Matrix &operator+=(const Matrix &other) { int H = height(); int W = width(); assert(H == other.height() && W == other.width()); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { (*this)[i][j] += other[i][j]; } } return (*this); } Matrix &operator+=(T X) { int H = height(); int W = width(); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { mat[i][j] += X; } } return (*this); } Matrix &operator-=(const Matrix &other) { int H = height(); int W = width(); assert(H == other.height() && W == other.width()); for(size_t i = 0; i < H; i++) { for(size_t j = 0; j < W; j++) { (*this)[i][j] -= other[i][j]; } } return (*this); } Matrix &operator-=(T X) { int H = height(); int W = width(); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { mat[i][j] -= X; } } return (*this); } Matrix &operator*=(T X) { int H = height(); int W = width(); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { mat[i][j] *= X; } } return (*this); } Matrix &operator/=(T X) { int H = height(); int W = width(); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { mat[i][j] /= X; } } return (*this); } Matrix operator+(const Matrix &other) const { return (Matrix(*this) += other); } Matrix operator+(T X) const { return (Matrix(*this) += X); } Matrix operator-(const Matrix &other) const { return (Matrix(*this) -= other); } Matrix operator-(T X) const { return (Matrix(*this) -= X); } Matrix operator*(T X) const { return (Matrix(*this) *= X); } Matrix operator/(T X) const { return (Matrix(*this) /= X); } Matrix mat_mul(Matrix &other) { int h0 = height(); int w0 = width(); int h1 = other.height(); int w1 = other.width(); assert(w0 == h1); vector<vector<T>> ret(h0, vector<T>(w1, T(0))); for(int i = 0; i < h0; i++) { for(int j = 0; j < w1; j++) { for(int k = 0; k < w0; k++) { ret[i][j] += (*this)[i][k] * other[k][j]; } } } this->mat.swap(ret); return (*this); } Matrix pow(long long k) const { Matrix A = (*this); assert(height() == width()); Matrix ret = Matrix::I(height()); while(k) { if(k & 1) { ret.mat_mul(A); } A.mat_mul(A); k >>= 1LL; } return ret; } Matrix sum() { Matrix A = (*this); T ret = 0; int h = height(); int w = width(); for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { ret += A[i][j]; } } return T(ret); } private: std::vector<std::vector<T>> mat; }; void solve() { int N, M; cin >> M >> N; if(M <= 3) { cout << 0 << '\n'; } else if(M % 2 == 0) { Matrix<mint> m(3); m[0][0] = 1; m[1][0] = M; m[2][0] = M * (M / 2 - 2) + M / 2; m[0][1] = 1; m[1][1] = M - 1; m[2][1] = (M - 2) * (M / 2 - 2) + (M - 1) / 2; m[0][2] = 1; m[1][2] = M - 2; m[2][2] = mint(M - 3) * (M - 2) / 2 - (M - 4); Matrix<mint> p = m.pow(N - 1); vector<mint> v = {1, M, mint(M - 3) * (M - 2) / 2 + (M - 3)}; mint ans = 0; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { ans += p[i][j] * v[j]; } } cout << ans.val() << '\n'; } else { Matrix<mint> m(3); m[0][0] = 1; m[1][0] = M; m[2][0] = M * (M / 2 - 1); m[0][1] = 1; m[1][1] = M - 1; m[2][1] = (M - 2) * (M / 2 - 1); m[0][2] = 1; m[1][2] = M - 2; m[2][2] = mint(M - 3) * (M - 2) / 2 - (M - 4); Matrix<mint> p = m.pow(N - 1); vector<mint> v = {1, M, mint(M - 3) * (M - 2) / 2 + (M - 3)}; mint ans = 0; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { ans += p[i][j] * v[j]; } } cout << ans.val() << '\n'; } } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int T; cin >> T; while(T--) { solve(); } return 0; }