結果

問題 No.826 連絡網
ユーザー TakoKurageTakoKurage
提出日時 2019-05-03 22:24:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 1,139 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 82,720 KB
実行使用メモリ 92,288 KB
最終ジャッジ日時 2024-05-03 05:11:14
合計ジャッジ時間 3,440 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,224 KB
testcase_01 AC 33 ms
52,224 KB
testcase_02 AC 40 ms
59,392 KB
testcase_03 AC 58 ms
70,144 KB
testcase_04 AC 58 ms
70,656 KB
testcase_05 AC 59 ms
68,736 KB
testcase_06 AC 57 ms
69,376 KB
testcase_07 AC 59 ms
70,548 KB
testcase_08 AC 58 ms
68,864 KB
testcase_09 AC 59 ms
70,656 KB
testcase_10 AC 49 ms
64,512 KB
testcase_11 AC 58 ms
70,272 KB
testcase_12 AC 99 ms
86,784 KB
testcase_13 AC 76 ms
76,544 KB
testcase_14 AC 96 ms
82,432 KB
testcase_15 AC 66 ms
71,936 KB
testcase_16 AC 82 ms
78,848 KB
testcase_17 AC 79 ms
76,800 KB
testcase_18 AC 74 ms
75,648 KB
testcase_19 AC 111 ms
88,960 KB
testcase_20 AC 112 ms
88,448 KB
testcase_21 AC 66 ms
70,656 KB
testcase_22 AC 82 ms
77,312 KB
testcase_23 AC 87 ms
78,592 KB
testcase_24 AC 71 ms
73,984 KB
testcase_25 AC 117 ms
92,288 KB
testcase_26 AC 72 ms
74,752 KB
testcase_27 AC 103 ms
86,656 KB
testcase_28 AC 94 ms
83,200 KB
testcase_29 AC 76 ms
76,544 KB
testcase_30 AC 128 ms
91,776 KB
testcase_31 AC 87 ms
78,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, size):
        self.data = [-1 for _ in range(size)]

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

    def union(self, x, y):
        x, y = self.find(x), self.find(y)
        if x != y:
            if self.data[y] < self.data[x]:
                x, y = y, x
            self.data[x] += self.data[y]
            self.data[y] = x
        return (x != y)

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

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


def prime_table(n):
    list = [True for _ in range(n + 1)]
    i = 2
    while i * i <= n:
        if list[i]:
            j = i + i
            while j <= n:
                list[j] = False
                j += i
        i += 1

    table = [i for i in range(n + 1) if list[i] and i >= 2]
    return table


N, P = [int(i) for i in input().split()]
uf = UnionFind(N + 1)
for p in prime_table(N):
    q = p
    while q <= N:
        uf.union(p, q)
        q += p
print(uf.size(P))
0