結果

問題 No.826 連絡網
ユーザー stngstng
提出日時 2022-07-09 17:44:23
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,851 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 125,580 KB
最終ジャッジ日時 2023-08-30 01:38:53
合計ジャッジ時間 5,528 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,468 KB
testcase_01 AC 72 ms
71,264 KB
testcase_02 AC 76 ms
75,336 KB
testcase_03 AC 114 ms
77,708 KB
testcase_04 AC 116 ms
77,904 KB
testcase_05 AC 110 ms
77,684 KB
testcase_06 AC 109 ms
77,632 KB
testcase_07 AC 112 ms
78,168 KB
testcase_08 AC 105 ms
77,832 KB
testcase_09 AC 115 ms
78,000 KB
testcase_10 AC 96 ms
77,084 KB
testcase_11 AC 110 ms
77,908 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

def sieve_of_eratosthenes(n):
    prime = [True for i in range(n+1)]
    prime[0] = False
    prime[1] = False

    sqrt_n = math.ceil(math.sqrt(n))
    for i in range(2, sqrt_n):
        if prime[i]:
            for j in range(2*i, n+1, i):
                prime[j] = False

    return prime

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1 for _ in range(n+1)]
    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

n,p = map(int,input().split())
ki = UnionFind(n+1)
chk = [0]*(n+1)

prm = sieve_of_eratosthenes(n)
pm = []
for i in range(n+1):
    if prm[i] == True:
        pm.append(i)
pm.sort()

tmp = 2
while tmp*2 <= n:
    #print(tmp*2,tmp,tmp)
    ki.union(2,tmp*2)
    tmp += 1

for i in range(1,len(pm)):
    now = pm[i]
    idx = i
    if now*2 <= n:
        ki.union(now,now*2)
    else:
        break
    tmp = 3
    while now*tmp <= n:
        #print(now,now,prm[idx])
        ki.union(now,now*tmp)
        tmp += 1
        #idx += 1
        #if idx >= len(pm):
        #    break

ans = 0
idx = ki.find(p)

for i in range(n):
    #print(ki.par[i+1])
    if ki.find(i+1) == idx:
        ans += 1
        #print(i+1,end="")

print(ans)
0