結果

問題 No.2219 Re:010
ユーザー Hydru
提出日時 2023-02-17 23:08:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,273 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 82,416 KB
実行使用メモリ 132,248 KB
最終ジャッジ日時 2024-07-19 14:20:10
合計ジャッジ時間 3,985 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

mod=998244353

class combination():
    def __init__(self,n,mod):
    
        self.mod=mod
        self.fact = [1,1] # x!
        self.factinv = [1,1] # (x!)^(-1)
        inv = [0,1]
        for i in range(2,n+1):
            self.fact.append((self.fact[-1]*i)%mod)
            inv.append((-inv[mod%i]*(mod//i))%mod)
            self.factinv.append((self.factinv[-1]*inv[-1])%mod)

    def cmb(self,n,r):
        r=min(r,n-r)
        return self.fact[n]*self.factinv[r]*self.factinv[n-r]%self.mod

S=input()
cntr=[0,0,0]
for s in S:
    if s=="0":
        cntr[0]+=1
    elif s=="1":
        cntr[1]+=1
    else:
        cntr[2]+=1
cntl=[0,0,0]

C=combination(len(S)+1,mod)
pow_2=[1]
for _ in range(len(S)+1):
    pow_2.append(pow_2[-1]*2%mod)

ans=0
for s in S:
    if s=="0":
        cntr[0]-=1
        cntl[0]+=1
        continue
    if s=="1":
        cntr[1]-=1
        x=cntl[2]
        y=cntr[2]
        ans+=(cntl[0]*pow_2[x]+x*pow_2[max(0,x-1)])*(cntr[0]*pow_2[y]+y*pow_2[max(0,y-1)])
        ans%=mod
        cntl[1]+=1
        continue
    if s=="?":
        cntr[2]-=1
        x=cntl[2]
        y=cntr[2]
        ans+=(cntl[0]*pow_2[x]+x*pow_2[max(0,x-1)])*(cntr[0]*pow_2[y]+y*pow_2[max(0,y-1)])
        ans%=mod
        cntl[2]+=1
        continue

print(ans)
0