結果

問題 No.2528 pop_(backfront or not)
ユーザー InTheBloomInTheBloom
提出日時 2023-11-03 23:24:57
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 2,597 bytes
コンパイル時間 2,928 ms
コンパイル使用メモリ 192,548 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-11-03 23:25:04
合計ジャッジ時間 7,179 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 8 ms
4,348 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N = readln.chomp.to!int;
    solve(N);
}

void solve (int N) {
    const int MOD = 998244353;
    const int MAX = 2100;
    long[] fact = new long[](MAX);
    long[] factInv = new long[](MAX);
    fact[0] = factInv[0] = 1;
    for (int i = 1; i < MAX; i++) {
        (fact[i] = i*fact[i-1]) %= MOD;
        factInv[i] = modInv(fact[i], MOD);
    }

    long comb (long n_, long k_, const int MOD) {
        int n = cast(int) n_, k = cast(int) k_;
        if (n < k) return 0;
        long res = fact[n];
        res *= factInv[k]; res %= MOD;
        res *= factInv[n-k]; res %= MOD;
        return res;
    }


    /* ナイーブなdpを試してみる */
    long[][][] dp = new long[][][](N+1, 2*N+1, 2*N+1);
    // dp[i][j][k] := 操作i回終了後、左にj個/右にk個残っている場合の数

    long[] ans = new long[](2*N+1);

    for (int x = 1; x <= 2*N+1; x++) {
        foreach (dd; dp) foreach (d; dd) d[] = 0;

        /* 初期状態 */
        dp[0][x-1][2*N+1-x] = 1;

        foreach (i; 0..N) {
            foreach (j; 0..2*N+1) foreach (k; 0..2*N+1) {
                /* 左の二個を抜く */
                if (1 <= j-2) (dp[i+1][j-2][k] += comb(j-1, 2, MOD) * dp[i][j][k]) %= MOD;
                /* 右の二個を抜く */
                if (1 <= k-2) (dp[i+1][j][k-2] += comb(k-1, 2, MOD) * dp[i][j][k]) %= MOD;
                /* 左右を抜く */
                if (1 <= j && 1 <= k && 0 < dp[i][j][k]) (dp[i+1][j-1][k-1] += ((1 + comb(j-1, 1, MOD) * comb(k-1, 1, MOD)) * dp[i][j][k])) %= MOD;
            }
        }
        ans[x-1] = dp[N][0][0];
    }

    /* 出力 */
    foreach (a; ans) writeln(a);
}

void read(T...)(string S, ref T args) {
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}

long modPow (long a, long x, const int MOD) {
    // assertion
    assert(0 <= x);
    assert(1 <= MOD);

    // normalize
    a %= MOD; a += MOD; a %= MOD;

    // simple case
    if (MOD == 1) {
        return 0L;
    }

    if (x == 0) {
        return 1L;
    }

    if (x == 1) {
        return a;
    }

    // calculate
    long res = 1L;
    long base = a % MOD;
    while (x != 0) {
        if ((x&1) != 0) {
            res *= base;
            res %= MOD;
        }
        base = base*base; base %= MOD;
        x >>= 1;
    }

    return res;
}

long modInv (long x, const int MOD) {
    import std.exception;
    enforce(1 <= MOD, format("Line : %s, MOD must be greater than 1. Your input = %s", __LINE__, MOD));
    return modPow(x, MOD-2, MOD);
}
0