結果

問題 No.2503 Typical Path Counting Problem on a Grid
ユーザー 👑 emthrmemthrm
提出日時 2023-07-24 17:01:53
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 3,095 bytes
コンパイル時間 1,438 ms
コンパイル使用メモリ 105,796 KB
実行使用メモリ 814,624 KB
最終ジャッジ日時 2024-04-14 11:40:37
合計ジャッジ時間 5,673 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>

#include <atcoder/modint>

namespace emthrm {

template <typename T>
struct Matrix {
  explicit Matrix(const int m, const int n, const T def = 0)
      : data(m, std::vector<T>(n, def)) {}

  int nrow() const { return data.size(); }
  int ncol() const { return data.front().size(); }

  Matrix pow(long long exponent) const {
    const int n = nrow();
    Matrix<T> res(n, n, 0), tmp = *this;
    for (int i = 0; i < n; ++i) {
      res[i][i] = 1;
    }
    for (; exponent > 0; exponent >>= 1) {
      if (exponent & 1) res *= tmp;
      tmp *= tmp;
    }
    return res;
  }

  inline const std::vector<T>& operator[](const int i) const { return data[i]; }
  inline std::vector<T>& operator[](const int i) { return data[i]; }

  Matrix& operator=(const Matrix& x) = default;

  Matrix& operator+=(const Matrix& x) {
    const int m = nrow(), n = ncol();
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        data[i][j] += x[i][j];
      }
    }
    return *this;
  }

  Matrix& operator-=(const Matrix& x) {
    const int m = nrow(), n = ncol();
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        data[i][j] -= x[i][j];
      }
    }
    return *this;
  }

  Matrix& operator*=(const Matrix& x) {
    const int m = nrow(), l = ncol(), n = x.ncol();
    std::vector<std::vector<T>> res(m, std::vector<T>(n, 0));
    for (int i = 0; i < m; ++i) {
      for (int k = 0; k < l; ++k) {
        for (int j = 0; j < n; ++j) {
          res[i][j] += data[i][k] * x[k][j];
        }
      }
    }
    data.swap(res);
    return *this;
  }

  Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
  Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
  Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }

 private:
  std::vector<std::vector<T>> data;
};

}  // namespace emthrm

int main() {
  using mint = atcoder::modint998244353;
  constexpr int kMaxT = 20000, kMaxN = 10000000;
  constexpr std::int64_t kMaxM = 1000000000000000000;

  std::vector dp1(kMaxN + 1, emthrm::Matrix<mint>(2, 2));
  dp1[0][0][0] = dp1[0][1][1] = 1;
  for (int i = 0; i < kMaxN; ++i) {
    dp1[i + 1][0][0] = (i + 1) * 2;
    dp1[i + 1][0][1] = 1;
    dp1[i + 1][1][0] = i + 1;
    dp1[i + 1] *= dp1[i];
  }
  std::vector dp2(kMaxN + 1, emthrm::Matrix<mint>(2, 2));
  dp2[0][0][0] = dp2[0][1][1] = 1;
  for (int i = 0; i < kMaxN; ++i) {
    dp2[i + 1][0][0] = (i + 1) * 2;
    dp2[i + 1][0][1] = 1;
    dp2[i + 1][1][0] = i;
    dp2[i + 1] = dp2[i] * dp2[i + 1];
  }

  int t;
  std::cin >> t;
  assert(1 <= t && t <= kMaxT);

  while (t--) {
    int n;
    std::int64_t m;
    std::cin >> n >> m;
    assert(0 <= n && n <= kMaxN && 0 <= m && m <= kMaxM);
    if (n > m) {
      const int tmp = m;
      m = n;
      n = tmp;
    }
    emthrm::Matrix<mint> matrix(2, 2);
    matrix[0][0] = n * 2 + 1;
    matrix[0][1] = 1;
    matrix[1][0] = n;
    std::cout << (dp2[n] * matrix.pow(m - n) * dp1[n])[0][0].val() << '\n';
  }
  return 0;
}
0