結果
| 問題 |
No.2899 Taffy Permutation
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-20 21:49:15 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 141 ms / 2,000 ms |
| コード長 | 1,928 bytes |
| コンパイル時間 | 278 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 77,312 KB |
| 最終ジャッジ日時 | 2024-09-20 21:49:26 |
| 合計ジャッジ時間 | 2,904 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
ソースコード
import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd,log
input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())
mod = 998244353
def solve(N,S):
dp = [0] * (N+1)
dp[0] = 1
for idx,s in enumerate(S):
ndp = [0] * (N+2)
for i in range(N+1):
if not dp[i]: continue
if s == '0':
#ndp[i+1] += i * dp[i] % mod
#ndp[i+1] %= mod
ndp[i+1] += i * dp[i] % mod
ndp[i+2] -= i * dp[i] % mod
ndp[i+1] %= mod
ndp[i+2] %= mod
ndp[i+1] += dp[i]
ndp[idx+2] -= dp[i]
ndp[i+1] %= mod
ndp[idx+2] %= mod
#for j in range(i+1,idx+2):
#ndp[j] += dp[i]
#ndp[j] %= mod
else:
ndp[i] += dp[i] * (idx-i+1) % mod
ndp[i] %= mod
ndp[i+1] -= dp[i] * (idx-i+1) % mod
ndp[i+1] %= mod
dp = ndp
for i in range(1,N+2):
dp[i] += dp[i-1]
dp[i] %= mod
return sum(dp) % mod
def brute(N,S):
res = 0
for p in permutations([i for i in range(N)]):
flg = 1
for i in range(N):
for j in range(i+1,N):
if S[i] == '0' and S[j] == '1' and p[i] > p[j]:
flg = 0
res += flg
return res
while False:
N = 8
S = "".join([random.choice("01") for i in range(N)])
#S = input()
exp = brute(N,S)
res = solve(N,S)
if exp!=res:
print("WA")
print(N)
print(S)
print(exp,res)
exit()
else:
print("AC",exp)
N = int(input())
S = input()
print(solve(N,S))