結果
問題 | No.2165 Let's Play Nim! |
ユーザー | vwxyz |
提出日時 | 2023-12-02 03:07:23 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 18,342 bytes |
コンパイル時間 | 480 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 93,896 KB |
平均クエリ数 | 70.87 |
最終ジャッジ日時 | 2024-09-26 16:18:31 |
合計ジャッジ時間 | 7,425 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 73 ms
74,848 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
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 | RE | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | AC | 67 ms
74,848 KB |
testcase_31 | AC | 69 ms
74,720 KB |
testcase_32 | WA | - |
evil_2_big_1.txt | WA | - |
evil_2_big_2.txt | WA | - |
evil_2_big_3.txt | WA | - |
evil_big_1.txt | WA | - |
evil_big_2.txt | WA | - |
evil_big_3.txt | WA | - |
ソースコード
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())) xor=0 for a in A: xor^=a if xor: t=1 else: t=0 print(t) if t: for i in range(N): if A[i]>A[i]^xor: print(i+1,A[i]-(A[i]^xor)) A[i]^=xor idx=[set() for b in range(20)] for i in range(N): for b in range(20): if A[i]&1<<b: idx[b].add(i) sys.stdout.flush() if t: ret=int(readline()) if ret==-1: exit() while True: i,k=map(int,readline().split()) ret=int(readline()) if ret==-1: exit() i-=1 xor=A[i]^(A[i]-k) for b in range(20): if A[i]&1<<b: idx[b].remove(i) A[i]-=k for b in range(20): if A[i]&1<<b: idx[b].add(i) b=xor.bit_length()-1 for i in idx[b]: for b in range(20): if A[i]&1<<b: idx[b].remove(i) k=A[i]-(A[i]^xor) for b in range(20): if A[i]&1<<b: idx[b].add(i) break print(i+1,k) sys.stdout.flush() ret=int(readline()) if ret==-1: exit()