結果
問題 | No.1193 Penguin Sequence |
ユーザー | vwxyz |
提出日時 | 2023-12-02 02:30:33 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 884 ms / 2,000 ms |
コード長 | 17,974 bytes |
コンパイル時間 | 451 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 209,020 KB |
最終ジャッジ日時 | 2024-09-26 16:17:46 |
合計ジャッジ時間 | 26,100 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 752 ms
207,336 KB |
testcase_01 | AC | 822 ms
208,980 KB |
testcase_02 | AC | 830 ms
208,336 KB |
testcase_03 | AC | 807 ms
208,652 KB |
testcase_04 | AC | 806 ms
208,968 KB |
testcase_05 | AC | 818 ms
208,792 KB |
testcase_06 | AC | 798 ms
208,408 KB |
testcase_07 | AC | 807 ms
208,716 KB |
testcase_08 | AC | 812 ms
209,020 KB |
testcase_09 | AC | 807 ms
208,716 KB |
testcase_10 | AC | 884 ms
208,780 KB |
testcase_11 | AC | 544 ms
143,388 KB |
testcase_12 | AC | 555 ms
143,812 KB |
testcase_13 | AC | 765 ms
187,688 KB |
testcase_14 | AC | 734 ms
174,700 KB |
testcase_15 | AC | 837 ms
204,564 KB |
testcase_16 | AC | 482 ms
144,612 KB |
testcase_17 | AC | 61 ms
57,728 KB |
testcase_18 | AC | 180 ms
82,080 KB |
testcase_19 | AC | 804 ms
207,972 KB |
testcase_20 | AC | 634 ms
173,472 KB |
testcase_21 | AC | 537 ms
150,704 KB |
testcase_22 | AC | 185 ms
85,136 KB |
testcase_23 | AC | 482 ms
130,084 KB |
testcase_24 | AC | 475 ms
120,756 KB |
testcase_25 | AC | 287 ms
94,024 KB |
testcase_26 | AC | 174 ms
80,000 KB |
testcase_27 | AC | 752 ms
184,316 KB |
testcase_28 | AC | 561 ms
149,228 KB |
testcase_29 | AC | 718 ms
187,308 KB |
testcase_30 | AC | 350 ms
103,768 KB |
testcase_31 | AC | 314 ms
98,476 KB |
testcase_32 | AC | 645 ms
159,344 KB |
testcase_33 | AC | 478 ms
125,196 KB |
testcase_34 | AC | 423 ms
111,052 KB |
testcase_35 | AC | 487 ms
125,128 KB |
testcase_36 | AC | 408 ms
110,508 KB |
testcase_37 | AC | 733 ms
175,444 KB |
testcase_38 | AC | 61 ms
58,240 KB |
testcase_39 | AC | 64 ms
57,984 KB |
testcase_40 | AC | 63 ms
57,984 KB |
ソースコード
import sys readline=sys.stdin.readline from collections import defaultdict,Counter class UnionFind: def __init__(self,N,label=None,f=None,weighted=False,rollback=False): self.N=N self.parents=[None]*self.N self.size=[1]*self.N self.roots={i for i in range(self.N)} self.label=label if self.label!=None: self.label=[x for x in label] self.f=f self.weighted=weighted if self.weighted: self.weight=[0]*self.N self.rollback=rollback if self.rollback: self.operate_list=[] self.operate_set=[] def Find(self,x): stack=[] while self.parents[x]!=None: stack.append(x) x=self.parents[x] if not self.rollback: if self.weighted: w=0 for y in stack[::-1]: self.parents[y]=x w+=self.weight[y] self.weight[y]=w else: for y in stack[::-1]: self.parents[y]=x return x def Union(self,x,y,w=None): root_x=self.Find(x) root_y=self.Find(y) if self.rollback: self.operate_list.append([]) self.operate_set.append([]) if root_x==root_y: if self.weighted: if self.weight[y]-self.weight[x]==w: return True else: return False else: if self.size[root_x]<self.size[root_y]: x,y=y,x root_x,root_y=root_y,root_x if self.weighted: w=-w if self.rollback: self.operate_list[-1].append((self.parents,root_y,self.parents[root_y])) self.operate_list[-1].append((self.size,root_x,self.size[root_x])) self.operate_set[-1].append(root_y) if self.label!=None: self.operate_list[-1]((self.label,root_x,self.label[root_x])) if self.weighted: self.operate_list[-1].append((self.weight,root_y,self.weight[root_y])) self.parents[root_y]=root_x self.size[root_x]+=self.size[root_y] self.roots.remove(root_y) if self.label!=None: self.label[root_x]=self.f(self.label[root_x],self.label[root_y]) if self.weighted: self.weight[root_y]=w+self.weight[x]-self.weight[y] def Size(self,x): return self.size[self.Find(x)] def Same(self,x,y): return self.Find(x)==self.Find(y) def Label(self,x): return self.label[self.Find(x)] def Weight(self,x,y): root_x=self.Find(x) root_y=self.Find(y) if root_x!=root_y: return None return self.weight[y]-self.weight[x] def Roots(self): return list(self.roots) def Linked_Components_Count(self): return len(self.roots) def Linked_Components(self): linked_components=defaultdict(list) for x in range(self.N): linked_components[self.Find(x)].append(x) return linked_components def Rollback(self): assert self.rollback if self.operate_list: for lst,x,v in self.operate_list.pop(): lst[x]=v for x in self.operate_set.pop(): self.roots.add(x) return True else: return False def __str__(self): linked_components=defaultdict(list) for x in range(self.N): linked_components[self.Find(x)].append(x) return "\n".join(f"{r}: {linked_components[r]}" for r in sorted(list(linked_components.keys()))) class Graph: def __init__(self,V,edges=None,graph=None,directed=False,weighted=False,inf=float("inf")): self.V=V self.directed=directed self.weighted=weighted self.inf=inf if graph!=None: self.graph=graph """ self.edges=[] for i in range(self.V): if self.weighted: for j,d in self.graph[i]: if self.directed or not self.directed and i<=j: self.edges.append((i,j,d)) else: for j in self.graph[i]: if self.directed or not self.directed and i<=j: self.edges.append((i,j)) """ else: self.edges=edges self.graph=[[] for i in range(self.V)] if weighted: for i,j,d in self.edges: self.graph[i].append((j,d)) if not self.directed: self.graph[j].append((i,d)) else: for i,j in self.edges: self.graph[i].append(j) if not self.directed: self.graph[j].append(i) def Warshall_Floyd(self,route_restoration=False): dist=[[self.inf]*self.V for i in range(self.V)] for i in range(self.V): dist[i][i]=0 if route_restoration: parents=[[j for j in range(self.V)] for i in range(self.V)] for i,j,d in self.edges: if i==j: continue if dist[i][j]>d: dist[i][j]=d if route_restoration: parents[i][j]=i if not self.directed and dist[j][i]>d: dist[j][i]=d if route_restoration: parents[j][i]=j for k in range(self.V): for i in range(self.V): for j in range(self.V): if dist[i][j]>dist[i][k]+dist[k][j]: dist[i][j]=dist[i][k]+dist[k][j] if route_restoration: parents[i][j]=parents[k][j] for i in range(self.V): if dist[i][i]<0: for j in range(self.V): if dist[i][j]!=self.inf: dist[i][j]=-self.inf if route_restoration: for i in range(self.V): if dist[i][i]==0: parents[i][i]=None return dist,parents else: return dist def Kruskal(self,maximize=False,spanning_tree=False): UF=UnionFind(self.V) sorted_edges=sorted(self.edges if self.weighted else [(x,y,1) for x,y in self.edges],key=lambda tpl:tpl[2],reverse=maximize) if spanning_tree: st=[] else: cost=0 for x,y,d in sorted_edges: if not UF.Same(x,y): UF.Union(x,y) if spanning_tree: st.append((x,y,d)) else: cost+=d return st if spanning_tree else cost def Inversion_Number(lst,weight=False,weakly=False): compress,decompress=Compress(lst) compressed_lst=[compress[x] for x in lst] N=len(compress) if not weight: weight=[1]*len(lst) ST=Segment_Tree(N,lambda x,y:x+y,0) inversion_number=0 for c,x in zip(weight,compressed_lst): inversion_number+=ST.Fold(x if weakly else x+1,N)*c ST[x]+=c return inversion_number def Compress(lst): decomp=sorted(list(set(lst))) comp={x:i for i,x in enumerate(decomp)} return comp,decomp class Segment_Tree: def __init__(self,N,f,e,lst=None,dynamic=False): self.f=f self.e=e self.N=N if dynamic: self.segment_tree=defaultdict(lambda:self.e) else: if lst==None: self.segment_tree=[self.e]*2*self.N else: assert len(lst)<=self.N self.segment_tree=[self.e]*self.N+[x for x in lst]+[self.e]*(N-len(lst)) for i in range(self.N-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def __getitem__(self,i): if type(i)==int: if -self.N<=i<0: return self.segment_tree[i+self.N*2] elif 0<=i<self.N: return self.segment_tree[i+self.N] else: raise IndexError("list index out of range") else: a,b,c=i.start,i.stop,i.step if a==None: a=self.N else: a+=self.N if b==None: b=self.N*2 else: b+=self.N return self.segment_tree[slice(a,b,c)] def __setitem__(self,i,x): if -self.N<=i<0: i+=self.N*2 elif 0<=i<self.N: i+=self.N else: raise IndexError("list index out of range") self.segment_tree[i]=x while i>1: i>>= 1 self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Build(self,lst): for i,x in enumerate(lst,self.N): self.segment_tree[i]=x for i in range(self.N-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Fold(self,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=self.N*2 else: R+=self.N vL=self.e vR=self.e while L<R: if L&1: vL=self.f(vL,self.segment_tree[L]) L+=1 if R&1: R-=1 vR=self.f(self.segment_tree[R],vR) L>>=1 R>>=1 return self.f(vL,vR) def Fold_Index(self,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=self.N*2 else: R+=self.N if L==R: return None x=self.Fold(L-self.N,R-self.N) while L<R: if L&1: if self.segment_tree[L]==x: i=L break L+=1 if R&1: R-=1 if self.segment_tree[R]==x: i=R break L>>=1 R>>=1 while i<self.N: if self.segment_tree[i]==self.segment_tree[i<<1]: i<<=1 else: i<<=1 i|=1 i-=self.N return i def Bisect_Right(self,L=None,f=None): if L==self.N: return self.N if L==None: L=0 L+=self.N vl=self.e vr=self.e l,r=L,self.N*2 while l<r: if l&1: vl=self.f(vl,self.segment_tree[l]) l+=1 if r&1: r-=1 vr=self.f(self.segment_tree[r],vr) l>>=1 r>>=1 if f(self.f(vl,vr)): return self.N v=self.e while True: while L%2==0: L>>=1 vv=self.f(v,self.segment_tree[L]) if f(vv): v=vv L+=1 else: while L<self.N: L<<=1 vv=self.f(v,self.segment_tree[L]) if f(vv): v=vv L+=1 return L-self.N def Bisect_Left(self,R=None,f=None): if R==0: return 0 if R==None: R=self.N R+=self.N vl=self.e vr=self.e l,r=self.N,R while l<r: if l&1: vl=self.f(vl,self.segment_tree[l]) l+=1 if r&1: r-=1 vr=self.f(self.segment_tree[r],vr) l>>=1 r>>=1 if f(self.f(vl,vr)): return 0 v=self.e while True: R-=1 while R>1 and R%2: R>>=1 vv=self.f(self.segment_tree[R],v) if f(vv): v=vv else: while R<self.N: R=2*R+1 vv=self.f(self.segment_tree[R],v) if f(vv): v=vv R-=1 return R+1-self.N def __str__(self): return "["+", ".join(map(str,self.segment_tree[self.N:]))+"]" class Cumsum: def __init__(self,lst,mod=0): self.N=len(lst) self.mod=mod self.cumsum=[0]*(self.N+1) self.cumsum[0]=0 for i in range(1,self.N+1): self.cumsum[i]=self.cumsum[i-1]+lst[i-1] if self.mod: self.cumsum[i]%=self.mod def __getitem__(self,i): if type(i)==int: if 0<=i<self.N: a,b=i,i+1 elif -self.N<=i<0: a,b=i+self.N,i+self.N+1 else: raise IndexError('list index out of range') else: a,b=i.start,i.stop if a==None or a<-self.N: a=0 elif self.N<=a: a=self.N elif a<0: a+=self.N if b==None or self.N<=b: b=self.N elif b<-self.N: b=0 elif b<0: b+=self.N s=self.cumsum[b]-self.cumsum[a] if self.mod: s%=self.mod return s def __setitem__(self,i,x): if -self.N<=i<0: i+=self.N elif not 0<=i<self.N: raise IndexError('list index out of range') self.cumsum[i+1]=self.cumsum[i]+x if self.mod: self.cumsum[i+1]%=self.mod def __len__(self): return self.N def __str__(self): lst=[self.cumsum[i+1]-self.cumsum[i] for i in range(self.N)] if self.mod: for i in range(self.N): lst[i]%=self.mod return "["+", ".join(map(str,lst))+"]" def Swap_Count(N,A,B): if sorted(A)==sorted(B): idxA={tpl:i for i,tpl in enumerate(sorted([(A[i],i) for i in range(N)]))} idxB={tpl:i for i,tpl in enumerate(sorted([(B[i],i) for i in range(N)]))} for i in range(N): A[i]=idxA[(A[i],i)] B[i]=idxB[(B[i],i)] idx={A[i]:i for i in range(N)} for i in range(N): B[i]=idx[B[i]] retu=Inversion_Number(B) else: retu=-1 return retu 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 Build_Inverse(self,N): self.inverse=[None]*(N+1) assert self.p>N self.inverse[1]=1 for n in range(2,N+1): if n%self.p==0: continue a,b=divmod(self.mod,n) self.inverse[n]=(-a*self.inverse[b])%self.mod def Inverse(self,n): return self.inverse[n] 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 N=int(input()) A=list(map(int,readline().split())) mod=998244353 MD=MOD(mod) MD.Build_Fact(N) MD.Build_Inverse(N) ans=0 inv=Inversion_Number(A) L=[1]+[MD.Comb(N,i) for i in range(1,N+1)] R=[MD.Comb(N,i) for i in range(1,N+1)]+[1] for i in range(1,N+1): L[i]*=L[i-1] L[i]%=mod for i in range(N-1,-1,-1): R[i]*=R[i+1] R[i]%=mod for i in range(2,N+1): ans+=L[i-1]*R[i]*inv*MD.Comb(N-2,i-2)%mod ans%=mod cnt=N*N for c in Counter(A).values(): cnt-=c*c cnt//=2 s1,s2=0,0 for i in range(1,N+1): s1+=i*MD.Inverse(N) s2+=(i*MD.Inverse(N))**2%mod s1%=mod s2%=mod s=(s1*s1-s2)*(1+mod)//2%mod for i in range(1,N+1): s*=MD.Comb(N,i) s%=mod ans+=cnt*s%mod ans%=mod print(ans)