結果

問題 No.1059 素敵な集合
ユーザー MisterMister
提出日時 2020-05-20 00:28:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 908 bytes
コンパイル時間 260 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 81,012 KB
最終ジャッジ日時 2023-09-30 15:24:39
合計ジャッジ時間 3,986 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,448 KB
testcase_01 AC 116 ms
79,248 KB
testcase_02 AC 141 ms
78,868 KB
testcase_03 AC 74 ms
71,268 KB
testcase_04 AC 73 ms
71,416 KB
testcase_05 AC 73 ms
71,440 KB
testcase_06 AC 134 ms
78,156 KB
testcase_07 AC 137 ms
78,216 KB
testcase_08 AC 136 ms
78,364 KB
testcase_09 AC 127 ms
77,956 KB
testcase_10 AC 138 ms
78,384 KB
testcase_11 AC 140 ms
78,772 KB
testcase_12 AC 140 ms
78,552 KB
testcase_13 AC 150 ms
79,836 KB
testcase_14 AC 94 ms
76,504 KB
testcase_15 AC 159 ms
79,904 KB
testcase_16 AC 137 ms
78,632 KB
testcase_17 AC 145 ms
79,096 KB
testcase_18 AC 89 ms
79,512 KB
testcase_19 AC 144 ms
80,300 KB
testcase_20 AC 157 ms
80,116 KB
testcase_21 AC 162 ms
81,012 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