結果

問題 No.826 連絡網
ユーザー vwxyzvwxyz
提出日時 2022-01-05 13:23:49
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 7,745 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 419,328 KB
最終ジャッジ日時 2024-10-15 18:18:39
合計ジャッジ時間 22,004 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
55,168 KB
testcase_01 AC 50 ms
55,168 KB
testcase_02 AC 69 ms
66,432 KB
testcase_03 AC 104 ms
77,568 KB
testcase_04 AC 106 ms
78,080 KB
testcase_05 AC 101 ms
77,824 KB
testcase_06 AC 102 ms
77,824 KB
testcase_07 AC 106 ms
77,568 KB
testcase_08 AC 101 ms
77,440 KB
testcase_09 AC 105 ms
77,696 KB
testcase_10 AC 96 ms
77,696 KB
testcase_11 AC 102 ms
77,184 KB
testcase_12 AC 1,509 ms
328,108 KB
testcase_13 AC 579 ms
200,372 KB
testcase_14 AC 1,079 ms
313,648 KB
testcase_15 AC 192 ms
95,820 KB
testcase_16 AC 710 ms
239,008 KB
testcase_17 AC 570 ms
200,708 KB
testcase_18 AC 462 ms
171,816 KB
testcase_19 AC 1,733 ms
355,364 KB
testcase_20 AC 1,683 ms
350,292 KB
testcase_21 AC 108 ms
78,464 KB
testcase_22 AC 600 ms
203,692 KB
testcase_23 AC 713 ms
239,892 KB
testcase_24 AC 363 ms
138,312 KB
testcase_25 TLE -
testcase_26 AC 423 ms
151,292 KB
testcase_27 AC 1,532 ms
338,328 KB
testcase_28 AC 1,164 ms
307,056 KB
testcase_29 AC 589 ms
199,196 KB
testcase_30 TLE -
testcase_31 AC 711 ms
224,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 n in range(1,N+1):
    for p in Pr.Factorize(n).keys():
        edges.append((n,p))
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)
0