結果
問題 | No.826 連絡網 |
ユーザー | vwxyz |
提出日時 | 2022-01-05 13:25:49 |
言語 | PyPy2 (7.3.15) |
結果 |
AC
|
実行時間 | 1,842 ms / 2,000 ms |
コード長 | 7,736 bytes |
コンパイル時間 | 3,032 ms |
コンパイル使用メモリ | 76,864 KB |
実行使用メモリ | 371,384 KB |
最終ジャッジ日時 | 2024-10-15 18:23:42 |
合計ジャッジ時間 | 19,569 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 99 ms
77,440 KB |
testcase_01 | AC | 86 ms
78,008 KB |
testcase_02 | AC | 98 ms
79,104 KB |
testcase_03 | AC | 118 ms
80,000 KB |
testcase_04 | AC | 116 ms
80,256 KB |
testcase_05 | AC | 111 ms
80,256 KB |
testcase_06 | AC | 112 ms
80,000 KB |
testcase_07 | AC | 112 ms
80,128 KB |
testcase_08 | AC | 110 ms
80,056 KB |
testcase_09 | AC | 113 ms
80,128 KB |
testcase_10 | AC | 101 ms
79,616 KB |
testcase_11 | AC | 114 ms
80,000 KB |
testcase_12 | AC | 1,233 ms
299,652 KB |
testcase_13 | AC | 443 ms
160,380 KB |
testcase_14 | AC | 829 ms
229,168 KB |
testcase_15 | AC | 161 ms
101,632 KB |
testcase_16 | AC | 535 ms
180,120 KB |
testcase_17 | AC | 443 ms
160,860 KB |
testcase_18 | AC | 350 ms
135,808 KB |
testcase_19 | AC | 1,383 ms
317,224 KB |
testcase_20 | AC | 1,318 ms
313,052 KB |
testcase_21 | AC | 117 ms
80,684 KB |
testcase_22 | AC | 440 ms
160,684 KB |
testcase_23 | AC | 567 ms
183,480 KB |
testcase_24 | AC | 275 ms
125,184 KB |
testcase_25 | AC | 1,842 ms
371,384 KB |
testcase_26 | AC | 319 ms
133,260 KB |
testcase_27 | AC | 1,274 ms
294,520 KB |
testcase_28 | AC | 871 ms
252,000 KB |
testcase_29 | AC | 434 ms
158,604 KB |
testcase_30 | AC | 1,724 ms
367,652 KB |
testcase_31 | AC | 536 ms
175,412 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)