結果

問題 No.2503 Typical Path Counting Problem on a Grid
ユーザー 👑 emthrmemthrm
提出日時 2023-07-24 14:15:36
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,238 bytes
コンパイル時間 948 ms
コンパイル使用メモリ 109,304 KB
実行使用メモリ 814,224 KB
最終ジャッジ日時 2024-04-19 20:23:35
合計ジャッジ時間 4,811 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#include <atcoder/modint>
using mint = atcoder::modint998244353;

// <TLE>
// O(nm) 時間
mint Solve(const int n, const std::int64_t m) {
  std::vector dp(n + 1, std::vector(m + 1, mint::raw(0)));
  dp[0][0] = 1;
  for (std::int64_t diag = 0; diag < n + m; ++diag) {
    const int min_i = std::max(diag - m, INT64_C(0));
    const int max_i = std::min(diag, std::int64_t{n});
    mint sum = 0;
    for (int i = min_i; i <= max_i; ++i) {
      sum += dp[i][diag - i];
    }
    for (int i = min_i; i <= max_i; ++i) {
      const std::int64_t j = diag - i;
      assert(0 <= j && j <= m);
      if (j + 1 <= m) dp[i][j + 1] += sum;
      if (i + 1 <= n) {
        dp[i + 1][j] += sum;
        if (j + 1 <= m) dp[i + 1][j + 1] += sum;
      }
    }
  }
  return dp[n][m];
}

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

  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);
    std::cout << Solve(n, m).val() << '\n';
  }
  return 0;
}
0