結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,752 KB
testcase_01 AC 1,550 ms
20,224 KB
testcase_02 AC 190 ms
14,592 KB
testcase_03 AC 29 ms
10,752 KB
testcase_04 AC 28 ms
10,752 KB
testcase_05 AC 29 ms
10,624 KB
testcase_06 AC 148 ms
13,568 KB
testcase_07 AC 148 ms
13,184 KB
testcase_08 AC 200 ms
14,336 KB
testcase_09 AC 63 ms
11,648 KB
testcase_10 AC 321 ms
14,720 KB
testcase_11 AC 166 ms
13,568 KB
testcase_12 AC 94 ms
12,544 KB
testcase_13 AC 329 ms
16,000 KB
testcase_14 AC 37 ms
11,136 KB
testcase_15 AC 625 ms
19,072 KB
testcase_16 AC 182 ms
13,952 KB
testcase_17 AC 168 ms
14,208 KB
testcase_18 AC 192 ms
20,224 KB
testcase_19 AC 1,437 ms
20,224 KB
testcase_20 AC 1,381 ms
20,352 KB
testcase_21 AC 589 ms
19,456 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


lr = list(map(int, input().split()))

if len(lr) != 2:
    exit(1)

l, r = lr
if l < 1 or l >= r or 200000 < r:
    exit(1)

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