結果

問題 No.1967 Sugoroku Optimization
ユーザー siman
提出日時 2022-06-10 08:16:36
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 301 ms / 2,000 ms
コード長 1,059 bytes
コンパイル時間 1,127 ms
コンパイル使用メモリ 141,460 KB
実行使用メモリ 35,064 KB
最終ジャッジ日時 2024-09-21 05:42:52
合計ジャッジ時間 4,903 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

ll mod_pow(ll x, ll n, ll mod = MOD) {
  ll res = 1;

  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }

    x = x * x % mod;
    n >>= 1;
  }

  return res;
}

ll mod_inverse(ll x, ll mod = MOD) {
  return mod_pow(x, mod - 2, mod);
}

ll dp[2010][2010];

int main() {
  int N, K;
  cin >> N >> K;

  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;

  for (int i = 1; i <= K; ++i) {
    int remain = K - i + 1;
    ll sum = 0;

    for (int j = i; j <= N; ++j) {
      ll r = N - j + 1;

      if (j == N) {
        sum += dp[i - 1][j];
      }
      if (j - remain >= N) {
        sum += dp[i - 1][j - 1];
      } else {
        sum += dp[i - 1][j - 1] * mod_inverse(r);
      }

      sum %= MOD;
      dp[i][j] = sum;
    }
  }

  cout << dp[K][N] << endl;

  return 0;
}
0