結果
| 問題 |
No.1821 LEQ-GEQ Permutations
|
| コンテスト | |
| ユーザー |
nok0
|
| 提出日時 | 2022-01-16 22:33:42 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,219 bytes |
| コンパイル時間 | 305 ms |
| コンパイル使用メモリ | 82,432 KB |
| 実行使用メモリ | 549,424 KB |
| 最終ジャッジ日時 | 2024-11-23 12:29:56 |
| 合計ジャッジ時間 | 30,918 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 TLE * 5 MLE * 1 |
ソースコード
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)
nok0