結果

問題 No.2105 Avoid MeX
ユーザー 👑 chro_96chro_96
提出日時 2022-10-21 23:35:57
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,629 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 30,672 KB
実行使用メモリ 33,224 KB
最終ジャッジ日時 2023-09-13 23:44:25
合計ジャッジ時間 2,001 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
33,200 KB
testcase_01 AC 9 ms
33,140 KB
testcase_02 AC 9 ms
33,224 KB
testcase_03 AC 10 ms
33,104 KB
testcase_04 AC 17 ms
33,084 KB
testcase_05 AC 10 ms
33,012 KB
testcase_06 AC 12 ms
33,204 KB
testcase_07 AC 12 ms
33,140 KB
testcase_08 AC 9 ms
33,172 KB
testcase_09 AC 16 ms
33,100 KB
testcase_10 AC 9 ms
33,008 KB
testcase_11 AC 10 ms
33,100 KB
testcase_12 AC 11 ms
33,136 KB
testcase_13 AC 10 ms
33,112 KB
testcase_14 AC 10 ms
33,012 KB
testcase_15 AC 13 ms
33,100 KB
testcase_16 AC 10 ms
33,068 KB
testcase_17 AC 9 ms
33,168 KB
testcase_18 AC 10 ms
33,148 KB
testcase_19 AC 26 ms
33,104 KB
testcase_20 AC 26 ms
33,152 KB
testcase_21 AC 26 ms
33,168 KB
testcase_22 AC 26 ms
33,168 KB
testcase_23 AC 9 ms
33,168 KB
testcase_24 AC 10 ms
33,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

long long power_mod (long long a, long long b, long long mod_num) {
  long long ans = 1LL;
  
  if (b > 0LL) {
    ans = power_mod(a, b/2LL, mod_num);
    ans = (ans * ans) % mod_num;
    if (b%2LL == 1LL) {
      ans = (ans * a) % mod_num;
    }
  }
  
  return ans;
}

int main () {
  int c = 0;
  int x = 0;
  
  int res = 0;
  
  long long ans = 0LL;
  long long mod_num = 998244353LL;
  
  long long dp[2001][2001] = {};
  
  long long fact[2002] = {};
  long long invf[2002] = {};
  
  res = scanf("%d", &c);
  res = scanf("%d", &x);
  
  fact[0] = 1LL;
  for (int i = 0; i <= x; i++) {
    fact[i+1] = fact[i];
    fact[i+1] *= (long long) (i+1);
    fact[i+1] %= mod_num;
  }
  
  invf[x+1] = power_mod(fact[x+1], mod_num-2LL, mod_num);
  for (int i = x+1; i > 0; i--) {
    invf[i-1] = invf[i];
    invf[i-1] *= (long long)i;
    invf[i-1] %= mod_num;
  }
  
  if (x <= 0) {
    printf("%lld\n", power_mod((long long)(c+1), mod_num-2LL, mod_num));
    return 0;
  }
  
  dp[0][0] = 1LL;
  for (int i = 0; i+c < x; i++) {
    for (int j = 0; j <= i; j++) {
      long long tmp = dp[i][j];
      tmp *= (invf[c+i+1]*fact[c+i])%mod_num;
      tmp %= mod_num;
      dp[i+1][j] += (tmp*((long long)j))%mod_num;
      dp[i+1][j] %= mod_num;
      dp[i+1][j+1] += (tmp*((long long)(c+i+1-j)))%mod_num;
      dp[i+1][j+1] %= mod_num;
    }
  }
  
  if (c > x) {
    c = x;
  }
  
  for (int i = 0; i < x; i++) {
    long long tmp = (fact[x+1-i]+mod_num-fact[x-i])%mod_num;
    tmp *= invf[x+1-i];
    tmp %= mod_num;
    ans += (tmp*dp[x-c][i])%mod_num;
  }
  
  printf("%lld\n", ans%mod_num);
  return 0;
}
0