結果

問題 No.1059 素敵な集合
ユーザー H3PO4H3PO4
提出日時 2021-10-05 09:14:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,273 ms / 2,000 ms
コード長 1,352 bytes
コンパイル時間 515 ms
コンパイル使用メモリ 11,100 KB
実行使用メモリ 27,016 KB
最終ジャッジ日時 2023-09-30 08:27:55
合計ジャッジ時間 7,758 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,392 KB
testcase_01 AC 1,273 ms
19,112 KB
testcase_02 AC 129 ms
12,884 KB
testcase_03 AC 16 ms
8,188 KB
testcase_04 AC 16 ms
8,264 KB
testcase_05 AC 16 ms
8,272 KB
testcase_06 AC 101 ms
11,280 KB
testcase_07 AC 100 ms
11,232 KB
testcase_08 AC 146 ms
12,696 KB
testcase_09 AC 38 ms
9,532 KB
testcase_10 AC 246 ms
12,764 KB
testcase_11 AC 115 ms
11,368 KB
testcase_12 AC 59 ms
10,340 KB
testcase_13 AC 246 ms
14,480 KB
testcase_14 AC 21 ms
8,744 KB
testcase_15 AC 486 ms
18,124 KB
testcase_16 AC 127 ms
11,980 KB
testcase_17 AC 115 ms
12,572 KB
testcase_18 AC 85 ms
27,016 KB
testcase_19 AC 1,192 ms
19,036 KB
testcase_20 AC 1,186 ms
19,228 KB
testcase_21 AC 447 ms
18,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.height = [1] * n
        self.size = [1] * n

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.height[x] < self.height[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]
                if self.height[x] == self.height[y]:
                    self.height[x] += 1

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

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

    def group_members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parent) if i == x]

    def group_count(self):
        return len(self.roots())


L, R = map(int, input().split())
uf = UnionFind(R + 1)
for i in range(L, R + 1):
    for j in range(i * 2, R + 1, i):
        uf.unite(i, j)
print(uf.group_count() - L - 1)
0