結果
問題 | No.826 連絡網 |
ユーザー | vwxyz |
提出日時 | 2022-01-05 13:25:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,744 ms / 2,000 ms |
コード長 | 7,736 bytes |
コンパイル時間 | 283 ms |
コンパイル使用メモリ | 82,292 KB |
実行使用メモリ | 368,816 KB |
最終ジャッジ日時 | 2024-10-15 18:23:15 |
合計ジャッジ時間 | 16,540 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
55,424 KB |
testcase_01 | AC | 52 ms
55,296 KB |
testcase_02 | AC | 54 ms
61,568 KB |
testcase_03 | AC | 84 ms
78,464 KB |
testcase_04 | AC | 90 ms
79,372 KB |
testcase_05 | AC | 73 ms
72,448 KB |
testcase_06 | AC | 73 ms
72,576 KB |
testcase_07 | AC | 86 ms
78,976 KB |
testcase_08 | AC | 77 ms
73,088 KB |
testcase_09 | AC | 82 ms
78,880 KB |
testcase_10 | AC | 64 ms
67,840 KB |
testcase_11 | AC | 79 ms
75,460 KB |
testcase_12 | AC | 1,199 ms
296,292 KB |
testcase_13 | AC | 386 ms
157,624 KB |
testcase_14 | AC | 799 ms
235,296 KB |
testcase_15 | AC | 128 ms
94,080 KB |
testcase_16 | AC | 511 ms
174,208 KB |
testcase_17 | AC | 413 ms
157,812 KB |
testcase_18 | AC | 305 ms
135,896 KB |
testcase_19 | AC | 1,355 ms
335,636 KB |
testcase_20 | AC | 1,307 ms
330,996 KB |
testcase_21 | AC | 88 ms
79,872 KB |
testcase_22 | AC | 404 ms
158,336 KB |
testcase_23 | AC | 515 ms
174,708 KB |
testcase_24 | AC | 243 ms
125,856 KB |
testcase_25 | AC | 1,710 ms
363,712 KB |
testcase_26 | AC | 283 ms
135,760 KB |
testcase_27 | AC | 1,241 ms
300,340 KB |
testcase_28 | AC | 844 ms
242,100 KB |
testcase_29 | AC | 408 ms
156,004 KB |
testcase_30 | AC | 1,744 ms
368,816 KB |
testcase_31 | AC | 474 ms
170,504 KB |
ソースコード
import sys readline=sys.stdin.readline from collections import defaultdict, deque class Graph: def __init__(self,V,edges=False,graph=False,directed=False,weighted=False,inf=float("inf")): self.V=V self.directed=directed self.weighted=weighted self.inf=inf if not graph: 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) else: 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)) def SIV_BFS(self,s,bfs_tour=False,bipartite_graph=False,linked_components=False,parents=False,unweighted_dist=False,weighted_dist=False): seen=[False]*self.V seen[s]=True if bfs_tour: bt=[s] if linked_components: lc=[s] if parents: ps=[None]*self.V if unweighted_dist or bipartite_graph: uwd=[self.inf]*self.V uwd[s]=0 if weighted_dist: wd=[self.inf]*self.V wd[s]=0 queue=deque([s]) while queue: x=queue.popleft() for y in self.graph[x]: if self.weighted: y,d=y if not seen[y]: seen[y]=True queue.append(y) if bfs_tour: bt.append(y) if linked_components: lc.append(y) if parents: ps[y]=x if unweighted_dist or bipartite_graph: uwd[y]=uwd[x]+1 if weighted_dist: wd[y]=wd[x]+d if bipartite_graph: bg=[[],[]] for tpl in self.edges: i,j=tpl[:2] if self.weighted else tpl if uwd[i]==self.inf or uwd[j]==self.inf: continue if not uwd[i]%2^uwd[j]%2: bg=False break else: for x in range(self.V): if uwd[x]==self.inf: continue bg[uwd[x]%2].append(x) retu=() if bfs_tour: retu+=(bt,) if bipartite_graph: retu+=(bg,) if linked_components: retu+=(lc,) if parents: retu+=(ps,) if unweighted_dist: retu+=(uwd,) if weighted_dist: retu+=(wd,) if len(retu)==1: retu=retu[0] return retu def MIV_BFS(self,initial_vertices=False,bipartite_graph=False,linked_components=False,parents=False): if not initial_vertices: initial_vertices=[i for i in range(self.V)] seen=[False]*self.V if bipartite_graph: bg=[None]*self.V cnt=-1 if linked_components: lc=[] if parents: ps=[None]*self.V for s in initial_vertices: if seen[s]: continue seen[s]=True if bipartite_graph: cnt+=1 bg[s]=(cnt,0) if linked_components: lc.append([s]) queue=deque([s]) while queue: x=queue.popleft() for y in self.graph[x]: if self.weighted: y,d=y if not seen[y]: seen[y]=True queue.append(y) if bipartite_graph: bg[y]=(cnt,bg[x][1]^1) if linked_components: lc[-1].append(y) if parents: ps[y]=x if bipartite_graph: bg_=bg bg=[[[],[]] for i in range(cnt+1)] for tpl in self.edges: i,j=tpl[:2] if self.weighted else tpl if not bg_[i][1]^bg_[j][1]: bg[bg_[i][0]]=False for x in range(self.V): if bg[bg_[x][0]]: bg[bg_[x][0]][bg_[x][1]].append(x) retu=() if bipartite_graph: retu+=(bg,) if linked_components: retu+=(lc,) if parents: retu=(ps,) if len(retu)==1: retu=retu[0] return retu class Prime: def __init__(self,N): assert N<=10**8 self.smallest_prime_factor=[None]*(N+1) for i in range(2,N+1,2): self.smallest_prime_factor[i]=2 n=int(N**.5)+1 for p in range(3,n,2): if self.smallest_prime_factor[p]==None: self.smallest_prime_factor[p]=p for i in range(p**2,N+1,2*p): if self.smallest_prime_factor[i]==None: self.smallest_prime_factor[i]=p for p in range(n,N+1): if self.smallest_prime_factor[p]==None: self.smallest_prime_factor[p]=p self.primes=[p for p in range(N+1) if p==self.smallest_prime_factor[p]] def Factorize(self,N): assert N>=1 factors=defaultdict(int) if N<=len(self.smallest_prime_factor)-1: while N!=1: factors[self.smallest_prime_factor[N]]+=1 N//=self.smallest_prime_factor[N] else: for p in self.primes: while N%p==0: N//=p factors[p]+=1 if N<p*p: if N!=1: factors[N]+=1 break if N<=len(self.smallest_prime_factor)-1: while N!=1: factors[self.smallest_prime_factor[N]]+=1 N//=self.smallest_prime_factor[N] break else: if N!=1: factors[N]+=1 return factors def Divisors(self,N): assert N>0 divisors=[1] for p,e in self.Factorize(N).items(): A=[1] for _ in range(e): A.append(A[-1]*p) divisors=[i*j for i in divisors for j in A] return divisors def Is_Prime(self,N): return N==self.smallest_prime_factor[N] def Totient(self,N): for p in self.Factorize(N).keys(): N*=p-1 N//=p return N def Mebius(self,N): fact=self.Factorize(N) for e in fact.values(): if e>=2: return 0 else: if len(fact)%2==0: return 1 else: return -1 N,P=map(int,readline().split()) edges=[] Pr=Prime(N) for p in Pr.primes: for n in range(2*p,N+1,p): edges.append((p,n)) inf=1<<30 G=Graph(N+1,edges=edges,inf=inf) ans=N+1-G.SIV_BFS(P,unweighted_dist=True).count(inf) print(ans)