結果

問題 No.1059 素敵な集合
ユーザー MisterMister
提出日時 2020-05-20 00:37:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 816 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 86,816 KB
実行使用メモリ 82,424 KB
最終ジャッジ日時 2023-09-30 15:24:50
合計ジャッジ時間 4,529 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,520 KB
testcase_01 AC 108 ms
79,520 KB
testcase_02 AC 180 ms
79,644 KB
testcase_03 AC 72 ms
71,316 KB
testcase_04 AC 71 ms
71,140 KB
testcase_05 AC 71 ms
71,232 KB
testcase_06 AC 178 ms
79,460 KB
testcase_07 AC 179 ms
79,572 KB
testcase_08 AC 204 ms
80,168 KB
testcase_09 AC 150 ms
78,792 KB
testcase_10 AC 163 ms
79,064 KB
testcase_11 AC 179 ms
79,596 KB
testcase_12 AC 179 ms
79,296 KB
testcase_13 AC 181 ms
80,240 KB
testcase_14 AC 103 ms
77,716 KB
testcase_15 AC 204 ms
81,992 KB
testcase_16 AC 183 ms
79,680 KB
testcase_17 AC 181 ms
79,816 KB
testcase_18 AC 85 ms
79,488 KB
testcase_19 AC 141 ms
80,348 KB
testcase_20 AC 167 ms
81,044 KB
testcase_21 AC 203 ms
82,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 経路圧縮さぼったver.
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.size = [1 for i in range(n)]

    def find(self, v):
        if v == self.par[v]:
            return v
        else:
            return self.find(self.par[v])

    def unite(self, u, v):
        u = self.find(u)
        v = self.find(v)
        if u == v:
            return

        if self.size[u] < self.size[v]:
            u, v = v, u

        self.size[u] += self.size[v]
        self.par[v] = u


l, r = list(map(int, input().split()))

uf = UnionFind(r + 1)

for x in range(l, r + 1):
    y = x * 2
    while y <= r:
        uf.unite(x, y)
        y += x

ans = 0
for x in range(l, r):
    if uf.find(x) != uf.find(x + 1):
        ans += 1
        uf.unite(x, x + 1)

print(ans)
0