結果

問題 No.2503 Typical Path Counting Problem on a Grid
ユーザー 👑 emthrmemthrm
提出日時 2023-07-24 17:18:48
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 669 ms / 2,000 ms
コード長 1,801 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 86,812 KB
実行使用メモリ 316,128 KB
最終ジャッジ日時 2023-10-13 18:07:27
合計ジャッジ時間 8,849 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 669 ms
315,904 KB
testcase_01 AC 640 ms
315,868 KB
testcase_02 AC 589 ms
315,836 KB
testcase_03 AC 627 ms
315,832 KB
testcase_04 AC 636 ms
315,836 KB
testcase_05 AC 592 ms
315,888 KB
testcase_06 AC 622 ms
316,128 KB
testcase_07 AC 638 ms
315,892 KB
testcase_08 AC 627 ms
315,832 KB
testcase_09 AC 627 ms
315,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <array>
#include <cassert>
#include <cstdint>
#include <iostream>

#include <atcoder/modint>

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

  using Matrix = std::array<std::array<mint, 2>, 2>;
  const auto Mult = [](const Matrix& x, const Matrix& y) -> Matrix {
    Matrix res{0, 0, 0, 0};
    for (int i = 0; i < 2; ++i) {
      for (int k = 0; k < 2; ++k) {
        for (int j = 0; j < 2; ++j) {
          res[i][j] += x[i][k] * y[k][j];
        }
      }
    }
    return res;
  };
  const auto Pow = [Mult](const Matrix& x, std::int64_t exponent) -> Matrix {
    Matrix res{1, 0, 0, 1}, tmp = x;
    for (; exponent > 0; exponent /= 2) {
      if (exponent % 2 == 1) res = Mult(res, tmp);
      tmp = Mult(tmp, tmp);
    }
    return res;
  };

  // 前計算
  std::array<Matrix, kMaxN + 1> dp1{};
  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] = Mult(dp1[i + 1], dp1[i]);
  }
  std::array<Matrix, kMaxN + 1> dp2{};
  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] = Mult(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;
    }
    const Matrix dp =
        Mult(Mult(dp2[n], Pow(Matrix{n * 2 + 1, 1, n, 0}, m - n)), dp1[n]);
    std::cout << dp[0][0].val() << '\n';
  }
  return 0;
}
0