結果

問題 No.1821 LEQ-GEQ Permutations
ユーザー nok0nok0
提出日時 2022-01-16 22:33:42
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,219 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 280,296 KB
最終ジャッジ日時 2024-05-02 18:44:19
合計ジャッジ時間 16,221 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
66,816 KB
testcase_01 AC 49 ms
66,560 KB
testcase_02 AC 47 ms
66,816 KB
testcase_03 AC 49 ms
66,560 KB
testcase_04 AC 48 ms
66,560 KB
testcase_05 AC 49 ms
66,560 KB
testcase_06 AC 47 ms
66,688 KB
testcase_07 AC 113 ms
85,888 KB
testcase_08 AC 217 ms
92,288 KB
testcase_09 AC 2,139 ms
214,832 KB
testcase_10 AC 96 ms
83,840 KB
testcase_11 AC 257 ms
93,952 KB
testcase_12 AC 229 ms
92,672 KB
testcase_13 AC 437 ms
104,344 KB
testcase_14 AC 947 ms
137,412 KB
testcase_15 AC 1,928 ms
202,176 KB
testcase_16 AC 1,876 ms
197,060 KB
testcase_17 AC 888 ms
133,188 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 AC 792 ms
274,664 KB
testcase_24 AC 790 ms
274,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353

table_len = 10 ** 5 + 10

fac = [1, 1]
for i in range(2, table_len):
    fac.append(fac[-1] * i % mod)

finv = [0] * table_len
finv[-1] = pow(fac[-1], mod - 2, mod)
for i in range(table_len - 1, 0, -1):
    finv[i - 1] = finv[i] * i % mod


def comb(n, k):
    if k < 0 or n < 0 or n - k < 0:
        return 0
    return fac[n] * finv[k] % mod * finv[n - k] % mod


n = int(input())
s = input()
c0 = s.count('0')
c1 = n - c0

dp = [[0] * (n + 1) for i in range(n + 1)]
dp[0][0] = 1
for i in range(n):
    for j in range(n + 1):
        if i + 2 <= n and j + 1 <= n:
            dp[i + 2][j + 1] = (dp[i + 2][j + 1] + dp[i][j] * (i + 1)) % mod
        if j + 1 <= n:
            dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * (i - j)) % mod
        dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * j) % mod

res = 0

for eq0 in range(c0 + 1):
    for eq1 in range(c1 + 1):
        pos_eq = comb(c0, eq0) * comb(c1, eq1) * fac[eq0] * fac[eq1] % mod
        num_eq = comb(c0 + c1, eq0) * comb(c0 + c1 - eq0, eq1) % mod
        pos_neq = dp[c0 - eq0 + c1 - eq1][c0 - eq0]
        pos_neq2 = fac[c0 - eq0] * fac[c1 - eq1] % mod
        res += pos_eq * num_eq * pos_neq * pos_neq2
        res %= mod

print(res)
0