結果

問題 No.2503 Typical Path Counting Problem on a Grid
ユーザー SSRSSSRS
提出日時 2023-10-13 22:09:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 404 ms / 2,000 ms
コード長 1,198 bytes
コンパイル時間 1,985 ms
コンパイル使用メモリ 202,352 KB
実行使用メモリ 315,612 KB
最終ジャッジ日時 2023-10-13 22:09:50
合計ジャッジ時間 6,701 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 290 ms
315,452 KB
testcase_01 AC 351 ms
315,468 KB
testcase_02 AC 358 ms
315,536 KB
testcase_03 AC 344 ms
315,528 KB
testcase_04 AC 359 ms
315,420 KB
testcase_05 AC 340 ms
315,596 KB
testcase_06 AC 404 ms
315,596 KB
testcase_07 AC 350 ms
315,588 KB
testcase_08 AC 347 ms
315,468 KB
testcase_09 AC 368 ms
315,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int MAX = 10000000;
const long long MOD = 998244353;
struct mat{
  array<array<int, 2>, 2> A;
  mat(){
    A = {{{1, 0}, {0, 1}}};
  }
  mat(int a, int b, int c, int d){
    A = {{{a, b}, {c, d}}};
  }
  array<int, 2> &operator [](int k){
    return A[k];
  }
};
mat operator *(mat A, mat B){
  mat ans;
  for (int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      ans[i][j] = ((long long) A[i][0] * B[0][j] + (long long) A[i][1] * B[1][j]) % MOD;
    }
  }
  return ans;
}
mat matpow(mat A, long long N){
  mat ans;
  while (N > 0){
    if (N % 2 == 1){
      ans = ans * A;
    }
    A = A * A;
    N /= 2;
  }
  return ans;
}
int main(){
  int T;
  cin >> T;
  vector<mat> left(MAX + 1);
  for (int i = 0; i < MAX; i++){
    left[i + 1] = mat(i * 2 + 2, i, 1, 0) * left[i];
  }
  vector<mat> right(MAX + 1);
  for (int i = 0; i < MAX; i++){
    right[i + 1] = right[i] * mat(i * 2 + 2, i + 1, 1, 0);
  }
  for (int i = 0; i < T; i++){
    long long n, m;
    cin >> n >> m;
    if (n > m){
      swap(n, m);
    }
    mat A(n * 2 + 1, n, 1, 0);
    mat ans = right[n] * matpow(A, m - n) * left[n];
    cout << ans[0][0] << endl;
  }
}
0