結果

問題 No.826 連絡網
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-24 16:12:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 341 ms / 2,000 ms
コード長 1,362 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 81,744 KB
実行使用メモリ 237,332 KB
最終ジャッジ日時 2023-10-21 15:43:01
合計ジャッジ時間 6,121 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,648 KB
testcase_01 AC 39 ms
53,648 KB
testcase_02 AC 46 ms
59,408 KB
testcase_03 AC 65 ms
70,672 KB
testcase_04 AC 67 ms
70,684 KB
testcase_05 AC 63 ms
68,616 KB
testcase_06 AC 63 ms
68,616 KB
testcase_07 AC 66 ms
70,672 KB
testcase_08 AC 64 ms
68,624 KB
testcase_09 AC 66 ms
70,684 KB
testcase_10 AC 54 ms
64,188 KB
testcase_11 AC 65 ms
70,672 KB
testcase_12 AC 286 ms
195,412 KB
testcase_13 AC 143 ms
114,352 KB
testcase_14 AC 218 ms
158,168 KB
testcase_15 AC 84 ms
81,052 KB
testcase_16 AC 160 ms
126,872 KB
testcase_17 AC 140 ms
114,368 KB
testcase_18 AC 121 ms
104,928 KB
testcase_19 AC 291 ms
214,664 KB
testcase_20 AC 286 ms
214,192 KB
testcase_21 AC 67 ms
70,684 KB
testcase_22 AC 140 ms
114,688 KB
testcase_23 AC 162 ms
127,608 KB
testcase_24 AC 105 ms
95,224 KB
testcase_25 AC 341 ms
237,276 KB
testcase_26 AC 115 ms
99,992 KB
testcase_27 AC 266 ms
196,656 KB
testcase_28 AC 212 ms
167,756 KB
testcase_29 AC 135 ms
114,320 KB
testcase_30 AC 335 ms
237,332 KB
testcase_31 AC 152 ms
121,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import chain


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def size(self, x):
        return -self.root[self.find(x)]


def prime_set(N):
    """
    Nまでの素数のsetを返す
    """
    if N < 4:
        return ({}, {}, {2}, {2, 3})[N]
    Nsq = int(N ** 0.5 + 0.5) + 1
    primes = {2, 3} | set(chain(range(5, N + 1, 6), range(7, N + 1, 6)))
    for i in range(5, Nsq, 2):
        if i in primes:
            primes -= set(range(i * i, N + 1, i * 2))
    return primes


N, P = map(int, input().split())
uf = UF_tree(N)
for p in prime_set(N):
    q = p * 2
    while q <= N:
        uf.unite(p, q)
        q += p

print(uf.size(P))
0