結果

問題 No.1059 素敵な集合
ユーザー MisterMister
提出日時 2020-05-20 00:36:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 337 ms / 2,000 ms
コード長 792 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 87,260 KB
実行使用メモリ 83,764 KB
最終ジャッジ日時 2023-09-30 15:24:45
合計ジャッジ時間 5,109 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,468 KB
testcase_01 AC 111 ms
79,500 KB
testcase_02 AC 201 ms
80,548 KB
testcase_03 AC 70 ms
71,328 KB
testcase_04 AC 70 ms
71,344 KB
testcase_05 AC 70 ms
71,072 KB
testcase_06 AC 198 ms
79,656 KB
testcase_07 AC 184 ms
79,788 KB
testcase_08 AC 207 ms
80,552 KB
testcase_09 AC 157 ms
78,744 KB
testcase_10 AC 231 ms
81,292 KB
testcase_11 AC 193 ms
79,712 KB
testcase_12 AC 186 ms
79,052 KB
testcase_13 AC 223 ms
81,772 KB
testcase_14 AC 89 ms
76,512 KB
testcase_15 AC 237 ms
81,672 KB
testcase_16 AC 199 ms
79,960 KB
testcase_17 AC 196 ms
79,488 KB
testcase_18 AC 86 ms
79,188 KB
testcase_19 AC 337 ms
82,912 KB
testcase_20 AC 322 ms
83,764 KB
testcase_21 AC 258 ms
82,452 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 self.par[v] == v:
            return v
        else:
            self.par[v] = self.find(self.par[v])
            return self.par[v]

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

        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