結果
問題 | No.2019 Digits Filling for All Substrings |
ユーザー | Shirotsume |
提出日時 | 2022-01-21 00:31:35 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 5,096 bytes |
コンパイル時間 | 1,909 ms |
コンパイル使用メモリ | 81,536 KB |
実行使用メモリ | 106,752 KB |
最終ジャッジ日時 | 2024-06-12 01:41:59 |
合計ジャッジ時間 | 17,222 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
53,120 KB |
testcase_01 | AC | 40 ms
52,864 KB |
testcase_02 | AC | 41 ms
53,120 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 41 ms
53,248 KB |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 42 ms
53,120 KB |
testcase_23 | WA | - |
testcase_24 | AC | 565 ms
90,240 KB |
testcase_25 | AC | 623 ms
93,000 KB |
testcase_26 | AC | 336 ms
84,112 KB |
testcase_27 | AC | 133 ms
78,336 KB |
testcase_28 | AC | 133 ms
77,824 KB |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
ソースコード
class FFT(): def primitive_root_constexpr(self,m): if m==2:return 1 if m==167772161:return 3 if m==469762049:return 3 if m==754974721:return 11 if m==998244353:return 3 divs=[0]*20 divs[0]=2 cnt=1 x=(m-1)//2 while(x%2==0):x//=2 i=3 while(i*i<=x): if (x%i==0): divs[cnt]=i cnt+=1 while(x%i==0): x//=i i+=2 if x>1: divs[cnt]=x cnt+=1 g=2 while(1): ok=True for i in range(cnt): if pow(g,(m-1)//divs[i],m)==1: ok=False break if ok: return g g+=1 def bsf(self,x): res=0 while(x%2==0): res+=1 x//=2 return res butterfly_first=True butterfly_inv_first=True sum_e=[0]*30 sum_ie=[0]*30 def __init__(self,MOD): self.mod=MOD self.g=self.primitive_root_constexpr(self.mod) def butterfly(self,a): n=len(a) h=(n-1).bit_length() if self.butterfly_first: self.butterfly_first=False es=[0]*30 ies=[0]*30 cnt2=self.bsf(self.mod-1) e=pow(self.g,(self.mod-1)>>cnt2,self.mod) ie=pow(e,self.mod-2,self.mod) for i in range(cnt2,1,-1): es[i-2]=e ies[i-2]=ie e=(e*e)%self.mod ie=(ie*ie)%self.mod now=1 for i in range(cnt2-2): self.sum_e[i]=((es[i]*now)%self.mod) now*=ies[i] now%=self.mod for ph in range(1,h+1): w=1<<(ph-1) p=1<<(h-ph) now=1 for s in range(w): offset=s<<(h-ph+1) for i in range(p): l=a[i+offset] r=a[i+offset+p]*now r%=self.mod a[i+offset]=l+r a[i+offset]%=self.mod a[i+offset+p]=l-r a[i+offset+p]%=self.mod now*=self.sum_e[(~s & -~s).bit_length()-1] now%=self.mod def butterfly_inv(self,a): n=len(a) h=(n-1).bit_length() if self.butterfly_inv_first: self.butterfly_inv_first=False es=[0]*30 ies=[0]*30 cnt2=self.bsf(self.mod-1) e=pow(self.g,(self.mod-1)>>cnt2,self.mod) ie=pow(e,self.mod-2,self.mod) for i in range(cnt2,1,-1): es[i-2]=e ies[i-2]=ie e=(e*e)%self.mod ie=(ie*ie)%self.mod now=1 for i in range(cnt2-2): self.sum_ie[i]=((ies[i]*now)%self.mod) now*=es[i] now%=self.mod for ph in range(h,0,-1): w=1<<(ph-1) p=1<<(h-ph) inow=1 for s in range(w): offset=s<<(h-ph+1) for i in range(p): l=a[i+offset] r=a[i+offset+p] a[i+offset]=l+r a[i+offset]%=self.mod a[i+offset+p]=(l-r)*inow a[i+offset+p]%=self.mod inow*=self.sum_ie[(~s & -~s).bit_length()-1] inow%=self.mod def convolution(self,a,b): n=len(a);m=len(b) if not(a) or not(b): return [] if min(n,m)<=40: if n<m: n,m=m,n a,b=b,a res=[0]*(n+m-1) for i in range(n): for j in range(m): res[i+j]+=a[i]*b[j] res[i+j]%=self.mod return res z=1<<((n+m-2).bit_length()) a=a+[0]*(z-n) b=b+[0]*(z-m) self.butterfly(a) self.butterfly(b) c=[0]*z for i in range(z): c[i]=(a[i]*b[i])%self.mod self.butterfly_inv(c) iz=pow(z,self.mod-2,self.mod) for i in range(n+m-1): c[i]=(c[i]*iz)%self.mod return c[:n+m-1] def solve(n,s): mod = 998244353 a = [0] * (n + 1) for i in range(n): if s[i] == '?': a[i + 1] += 1 a[i + 1] += a[i] b = [0] * (n + 1) for i in range(n + 1): b[a[i]] += 1 c = b[::-1] Comv = FFT(mod) p = Comv.convolution(b, c)[n + 1:] ans = 0 for i in range(1, n + 1): ans += p[i - 1] * (pow(10, i, mod) - 1) * pow(3, mod - 2, mod) ans %= mod a = [0] * (n + 1) b = [0] * 3 b[0] = 1 for i in range(n): if s[i] == '?': b[0] += 1 continue else: a[i + 1] += int(s[i]) a[i + 1] += a[i] b[a[i + 1] % 3] += 1 for i in range(3): ans += b[i] * (b[i] - 1) // 2 ans %= mod return ans n = int(input()) s = list(input()) print(solve(n, s))