結果
問題 | No.1195 数え上げを愛したい(文字列編) |
ユーザー | vwxyz |
提出日時 | 2023-04-22 18:53:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,133 ms / 3,000 ms |
コード長 | 4,915 bytes |
コンパイル時間 | 637 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 208,972 KB |
最終ジャッジ日時 | 2024-04-24 21:45:11 |
合計ジャッジ時間 | 19,034 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,109 ms
208,972 KB |
testcase_01 | AC | 1,082 ms
207,936 KB |
testcase_02 | AC | 1,089 ms
208,612 KB |
testcase_03 | AC | 343 ms
107,104 KB |
testcase_04 | AC | 364 ms
110,940 KB |
testcase_05 | AC | 124 ms
102,912 KB |
testcase_06 | AC | 45 ms
55,680 KB |
testcase_07 | AC | 44 ms
55,552 KB |
testcase_08 | AC | 295 ms
97,056 KB |
testcase_09 | AC | 1,095 ms
206,224 KB |
testcase_10 | AC | 579 ms
139,716 KB |
testcase_11 | AC | 1,054 ms
207,704 KB |
testcase_12 | AC | 1,133 ms
205,200 KB |
testcase_13 | AC | 900 ms
170,148 KB |
testcase_14 | AC | 574 ms
135,928 KB |
testcase_15 | AC | 598 ms
139,680 KB |
testcase_16 | AC | 579 ms
137,208 KB |
testcase_17 | AC | 300 ms
97,052 KB |
testcase_18 | AC | 1,069 ms
202,852 KB |
testcase_19 | AC | 1,072 ms
201,304 KB |
testcase_20 | AC | 891 ms
174,320 KB |
testcase_21 | AC | 1,053 ms
202,900 KB |
testcase_22 | AC | 617 ms
144,736 KB |
testcase_23 | AC | 47 ms
55,296 KB |
testcase_24 | AC | 45 ms
55,680 KB |
testcase_25 | AC | 46 ms
55,808 KB |
ソースコード
import math import sys readline=sys.stdin.readline from collections import Counter from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max mod=998244353 def NTT(polynomial0,polynomial1): """ if len(polynomial0)*len(polynomial1)<=50: poly=[0]*(len(polynomial0)+len(polynomial1)-1) for i in range(len(polynomial0)): for j in range(len(polynomial1)): poly[i+j]+=polynomial0[i]*polynomial1[j]%mod poly[i+j]%=mod return poly """ if mod==998244353: prim_root=3 prim_root_inve=332748118 else: prim_root=Primitive_Root(mod) prim_root_inve=MOD(mod).Pow(prim_root,-1) def DFT(polynomial,n,inverse=False): if inverse: for bit in range(1,n+1): a=1<<bit-1 x=pow(prim_root,mod-1>>bit,mod) U=[1] for _ in range(a): U.append(U[-1]*x%mod) for i in range(1<<n-bit): for j in range(a): s=i*2*a+j t=s+a polynomial[s],polynomial[t]=(polynomial[s]+polynomial[t]*U[j])%mod,(polynomial[s]-polynomial[t]*U[j])%mod x=pow((mod+1)//2,n,mod) for i in range(1<<n): polynomial[i]*=x polynomial[i]%=mod else: for bit in range(n,0,-1): a=1<<bit-1 x=pow(prim_root_inve,mod-1>>bit,mod) U=[1] for _ in range(a): U.append(U[-1]*x%mod) for i in range(1<<n-bit): for j in range(a): s=i*2*a+j t=s+a polynomial[s],polynomial[t]=(polynomial[s]+polynomial[t])%mod,U[j]*(polynomial[s]-polynomial[t])%mod l=len(polynomial0)+len(polynomial1)-1 n=(len(polynomial0)+len(polynomial1)-2).bit_length() polynomial0=polynomial0+[0]*((1<<n)-len(polynomial0)) polynomial1=polynomial1+[0]*((1<<n)-len(polynomial1)) DFT(polynomial0,n) DFT(polynomial1,n) ntt=[x*y%mod for x,y in zip(polynomial0,polynomial1)] DFT(ntt,n,inverse=True) ntt=ntt[:l] return ntt def Extended_Euclid(n,m): stack=[] while m: stack.append((n,m)) n,m=m,n%m if n>=0: x,y=1,0 else: x,y=-1,0 for i in range(len(stack)-1,-1,-1): n,m=stack[i] x,y=y,x-(n//m)*y return x,y class MOD: def __init__(self,p,e=None): self.p=p self.e=e if self.e==None: self.mod=self.p else: self.mod=self.p**self.e def Pow(self,a,n): a%=self.mod if n>=0: return pow(a,n,self.mod) else: assert math.gcd(a,self.mod)==1 x=Extended_Euclid(a,self.mod)[0] return pow(x,-n,self.mod) def Build_Fact(self,N): assert N>=0 self.factorial=[1] if self.e==None: for i in range(1,N+1): self.factorial.append(self.factorial[-1]*i%self.mod) else: self.cnt=[0]*(N+1) for i in range(1,N+1): self.cnt[i]=self.cnt[i-1] ii=i while ii%self.p==0: ii//=self.p self.cnt[i]+=1 self.factorial.append(self.factorial[-1]*ii%self.mod) self.factorial_inve=[None]*(N+1) self.factorial_inve[-1]=self.Pow(self.factorial[-1],-1) for i in range(N-1,-1,-1): ii=i+1 while ii%self.p==0: ii//=self.p self.factorial_inve[i]=(self.factorial_inve[i+1]*ii)%self.mod def Fact(self,N): if N<0: return 0 retu=self.factorial[N] if self.e!=None and self.cnt[N]: retu*=pow(self.p,self.cnt[N],self.mod)%self.mod retu%=self.mod return retu def Fact_Inve(self,N): if self.e!=None and self.cnt[N]: return None return self.factorial_inve[N] def Comb(self,N,K,divisible_count=False): if K<0 or K>N: return 0 retu=self.factorial[N]*self.factorial_inve[K]%self.mod*self.factorial_inve[N-K]%self.mod if self.e!=None: cnt=self.cnt[N]-self.cnt[N-K]-self.cnt[K] if divisible_count: return retu,cnt else: retu*=pow(self.p,cnt,self.mod) retu%=self.mod return retu S=readline().rstrip() N=len(S) mod=998244353 MD=MOD(mod) MD.Build_Fact(N) queue=[(c,[MD.Fact_Inve(i) for i in range(c+1)]) for c in Counter(S).values()] while len(queue)>=2: poly=NTT(heappop(queue)[1],heappop(queue)[1]) heappush(queue,(len(poly)-1,poly)) ans=sum(queue[0][1][i]*MD.Fact(i)%mod for i in range(1,N+1))%mod print(ans)