結果

問題 No.1856 Mex Sum 2
ユーザー ぷらぷら
提出日時 2022-02-25 22:39:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 295 ms / 3,000 ms
コード長 1,750 bytes
コンパイル時間 2,763 ms
コンパイル使用メモリ 200,680 KB
実行使用メモリ 71,928 KB
最終ジャッジ日時 2023-09-16 17:50:03
合計ジャッジ時間 14,604 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,440 KB
testcase_01 AC 5 ms
10,544 KB
testcase_02 AC 6 ms
11,288 KB
testcase_03 AC 6 ms
10,488 KB
testcase_04 AC 6 ms
11,016 KB
testcase_05 AC 5 ms
10,648 KB
testcase_06 AC 6 ms
10,724 KB
testcase_07 AC 6 ms
10,708 KB
testcase_08 AC 5 ms
10,556 KB
testcase_09 AC 6 ms
10,488 KB
testcase_10 AC 5 ms
10,716 KB
testcase_11 AC 6 ms
10,600 KB
testcase_12 AC 6 ms
10,736 KB
testcase_13 AC 5 ms
10,640 KB
testcase_14 AC 6 ms
10,532 KB
testcase_15 AC 5 ms
10,684 KB
testcase_16 AC 6 ms
10,580 KB
testcase_17 AC 6 ms
10,944 KB
testcase_18 AC 5 ms
10,908 KB
testcase_19 AC 6 ms
10,588 KB
testcase_20 AC 5 ms
10,600 KB
testcase_21 AC 6 ms
10,576 KB
testcase_22 AC 6 ms
10,992 KB
testcase_23 AC 6 ms
10,516 KB
testcase_24 AC 6 ms
10,716 KB
testcase_25 AC 6 ms
10,896 KB
testcase_26 AC 5 ms
10,484 KB
testcase_27 AC 5 ms
10,636 KB
testcase_28 AC 5 ms
10,576 KB
testcase_29 AC 206 ms
66,228 KB
testcase_30 AC 223 ms
69,496 KB
testcase_31 AC 219 ms
69,236 KB
testcase_32 AC 181 ms
62,172 KB
testcase_33 AC 240 ms
70,536 KB
testcase_34 AC 207 ms
66,276 KB
testcase_35 AC 160 ms
60,900 KB
testcase_36 AC 179 ms
62,404 KB
testcase_37 AC 219 ms
69,224 KB
testcase_38 AC 230 ms
70,212 KB
testcase_39 AC 260 ms
71,520 KB
testcase_40 AC 260 ms
71,520 KB
testcase_41 AC 250 ms
71,464 KB
testcase_42 AC 264 ms
70,984 KB
testcase_43 AC 215 ms
65,592 KB
testcase_44 AC 235 ms
66,916 KB
testcase_45 AC 266 ms
70,564 KB
testcase_46 AC 216 ms
65,672 KB
testcase_47 AC 242 ms
69,224 KB
testcase_48 AC 270 ms
71,192 KB
testcase_49 AC 269 ms
71,088 KB
testcase_50 AC 178 ms
60,908 KB
testcase_51 AC 284 ms
71,480 KB
testcase_52 AC 282 ms
71,928 KB
testcase_53 AC 285 ms
71,516 KB
testcase_54 AC 285 ms
71,568 KB
testcase_55 AC 285 ms
71,516 KB
testcase_56 AC 285 ms
71,588 KB
testcase_57 AC 287 ms
71,528 KB
testcase_58 AC 286 ms
71,520 KB
testcase_59 AC 285 ms
71,568 KB
testcase_60 AC 287 ms
71,592 KB
testcase_61 AC 285 ms
71,588 KB
testcase_62 AC 285 ms
71,520 KB
testcase_63 AC 295 ms
71,616 KB
testcase_64 AC 288 ms
71,516 KB
testcase_65 AC 286 ms
71,516 KB
testcase_66 AC 285 ms
71,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

constexpr int mod = 998244353;

long long fac[200005], finv[200005], inv[200005];

void COMinit() {
    fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1;
    for (int i = 2; i < 200005; i++) {
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod % i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

long long modpow(long long a,long long b) {
    long long ans = 1;
    while(b) {
        if(b & 1) {
            (ans *= a) %= mod;
        }
        (a *= a) %= mod;
        b /= 2;
    }
    return ans;
}

long long dp[2022][2022],fac1[2022][2022];

int main() {
    int N,M;
    cin >> N >> M;
    dp[0][0] = 1;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            dp[i+1][j] += dp[i][j]*j%mod;
            dp[i+1][j] %= mod;
            dp[i+1][j+1] += dp[i][j];
            dp[i+1][j+1] %= mod;
        }
    }
    COMinit();
    for(int i = 1; i < N; i++) {
        for(int j = 0; j < N; j++) {
            dp[N][j] += dp[i][j]*modpow(M+1,N-i)%mod*COM(N,i)%mod;
            dp[N][j] %= mod;
        }
    }
    long long sum = 0;
    for(int i = 1; i <= N; i++) {
        fac1[i][i] = M+2-i;
        for(int j = i+1; j <= N; j++) {
            fac1[i][j] = fac1[i][j-1]*(M+2-j)%mod;
        }
    }
    for(int i = 1; i <= N; i++) {
        for(int j = 1; j <= min(M+1,N); j++) {
            if(i <= j) {
                sum += dp[N][j]*COM(j,i)%mod*fac[i]%mod*((i == j)?1:fac1[i+1][j])%mod;
                sum %= mod;
            }
        }
    }
    cout << sum << endl;
}
0