結果
問題 | No.2529 Treasure Hunter |
ユーザー | ochiaigawa |
提出日時 | 2023-11-03 23:21:52 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 20 ms / 2,000 ms |
コード長 | 5,534 bytes |
コンパイル時間 | 2,740 ms |
コンパイル使用メモリ | 221,592 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-25 21:28:31 |
合計ジャッジ時間 | 3,164 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 20 ms
5,376 KB |
testcase_02 | AC | 3 ms
5,376 KB |
testcase_03 | AC | 3 ms
5,376 KB |
testcase_04 | AC | 3 ms
5,376 KB |
testcase_05 | AC | 3 ms
5,376 KB |
testcase_06 | AC | 3 ms
5,376 KB |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
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 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 1 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 1 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 1 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 1 ms
5,376 KB |
ソースコード
#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) { Matrix<mint> m(3); m[0][0] = 1; m[1][0] = M; m[2][0] = 0; m[0][1] = 1; m[1][1] = M - 1; m[2][1] = 0; m[0][2] = 0; m[1][2] = 0; m[2][2] = 0; Matrix<mint> p = m.pow(N - 1); vector<mint> v = {1, M, 0}; 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 if(M % 2 == 0) { Matrix<mint> m(3); m[0][0] = 1; m[1][0] = M; m[2][0] = mint(M) * (M / 2 - 2) + M / 2; m[0][1] = 1; m[1][1] = M - 1; m[2][1] = mint(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] = mint(M) * (M / 2 - 1); m[0][1] = 1; m[1][1] = M - 1; m[2][1] = mint(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; }