結果

問題 No.1059 素敵な集合
ユーザー MisterMister
提出日時 2020-05-20 00:33:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,480 ms / 2,000 ms
コード長 823 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 20,224 KB
最終ジャッジ日時 2024-04-09 22:46:08
合計ジャッジ時間 8,776 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,752 KB
testcase_01 AC 1,480 ms
20,224 KB
testcase_02 AC 189 ms
14,464 KB
testcase_03 AC 29 ms
10,624 KB
testcase_04 AC 29 ms
10,752 KB
testcase_05 AC 29 ms
10,752 KB
testcase_06 AC 149 ms
13,312 KB
testcase_07 AC 149 ms
13,312 KB
testcase_08 AC 207 ms
14,464 KB
testcase_09 AC 63 ms
11,904 KB
testcase_10 AC 321 ms
14,720 KB
testcase_11 AC 162 ms
13,312 KB
testcase_12 AC 94 ms
12,416 KB
testcase_13 AC 326 ms
16,000 KB
testcase_14 AC 36 ms
11,008 KB
testcase_15 AC 617 ms
18,944 KB
testcase_16 AC 180 ms
13,952 KB
testcase_17 AC 172 ms
14,208 KB
testcase_18 AC 187 ms
20,224 KB
testcase_19 AC 1,412 ms
20,224 KB
testcase_20 AC 1,382 ms
20,224 KB
testcase_21 AC 596 ms
19,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

        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