結果

問題 No.1059 素敵な集合
ユーザー 👑 rin204rin204
提出日時 2022-07-04 17:00:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,425 ms / 2,000 ms
コード長 1,660 bytes
コンパイル時間 1,411 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 80,024 KB
最終ジャッジ日時 2023-08-21 00:43:23
合計ジャッジ時間 13,017 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,828 KB
testcase_01 AC 1,425 ms
79,260 KB
testcase_02 AC 410 ms
78,788 KB
testcase_03 AC 73 ms
71,400 KB
testcase_04 AC 73 ms
71,760 KB
testcase_05 AC 73 ms
71,624 KB
testcase_06 AC 285 ms
78,952 KB
testcase_07 AC 282 ms
78,800 KB
testcase_08 AC 388 ms
78,880 KB
testcase_09 AC 170 ms
78,016 KB
testcase_10 AC 425 ms
78,972 KB
testcase_11 AC 301 ms
78,460 KB
testcase_12 AC 218 ms
78,396 KB
testcase_13 AC 572 ms
79,120 KB
testcase_14 AC 101 ms
77,236 KB
testcase_15 AC 1,002 ms
80,024 KB
testcase_16 AC 351 ms
79,044 KB
testcase_17 AC 369 ms
78,952 KB
testcase_18 AC 733 ms
78,036 KB
testcase_19 AC 1,163 ms
79,900 KB
testcase_20 AC 1,167 ms
79,860 KB
testcase_21 AC 1,034 ms
79,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def 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.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

l, r = map(int, input().split())
UF = UnionFind(r - l + 1)

for i in range(l, r + 1):
    div = []
    for j in range(1, int(i ** 0.5 + 1)):
        if i % j == 0:
            if l <= j <= r:
                div.append(j)
            j = i // j
            if l <= j <= r:
                div.append(j)
    d0 = div[0]
    for d in div[1:]:
        UF.union(d0 - l, d - l)
print(UF.group_count() - 1)
0