結果

問題 No.2503 Typical Path Counting Problem on a Grid
ユーザー 👑 emthrmemthrm
提出日時 2023-07-24 20:18:14
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,811 bytes
コンパイル時間 1,369 ms
コンパイル使用メモリ 106,268 KB
実行使用メモリ 316,224 KB
最終ジャッジ日時 2024-04-14 11:40:58
合計ジャッジ時間 9,382 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 634 ms
315,968 KB
testcase_01 WA -
testcase_02 AC 669 ms
316,136 KB
testcase_03 WA -
testcase_04 AC 693 ms
316,148 KB
testcase_05 WA -
testcase_06 AC 699 ms
316,028 KB
testcase_07 AC 706 ms
316,032 KB
testcase_08 AC 682 ms
315,924 KB
testcase_09 AC 701 ms
316,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#include <atcoder/modint>

// <WA> #895007 ベース
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);
    const Matrix dp =
        Mult(Mult(dp2[n],
                  Pow(Matrix{n * 2 + 1, 1, n, 0}, std::max(m - n, INT64_C(0)))),
                  dp1[n]);
    std::cout << dp[0][0].val() << '\n';
  }
  return 0;
}
0