結果

問題 No.1059 素敵な集合
ユーザー yuly3yuly3
提出日時 2020-07-15 13:36:10
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,644 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 87,164 KB
実行使用メモリ 82,580 KB
最終ジャッジ日時 2023-09-30 15:39:43
合計ジャッジ時間 3,784 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,184 KB
testcase_01 AC 72 ms
71,260 KB
testcase_02 AC 123 ms
77,872 KB
testcase_03 AC 72 ms
71,448 KB
testcase_04 AC 74 ms
71,176 KB
testcase_05 AC 74 ms
71,224 KB
testcase_06 AC 121 ms
77,728 KB
testcase_07 AC 120 ms
77,844 KB
testcase_08 AC 120 ms
77,768 KB
testcase_09 AC 116 ms
77,484 KB
testcase_10 AC 119 ms
77,568 KB
testcase_11 AC 120 ms
77,936 KB
testcase_12 AC 128 ms
77,832 KB
testcase_13 AC 126 ms
78,064 KB
testcase_14 AC 79 ms
76,080 KB
testcase_15 AC 133 ms
79,036 KB
testcase_16 AC 119 ms
77,556 KB
testcase_17 AC 124 ms
77,728 KB
testcase_18 AC 81 ms
82,580 KB
testcase_19 AC 136 ms
78,360 KB
testcase_20 AC 141 ms
78,376 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


class UnionFind:
    def __init__(self, n: int):
        self.n = n
        self.parents = [-1] * n
    
    def find(self, x: int):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    
    def union(self, x: int, y: int):
        x = self.find(x)
        y = self.find(y)
        
        if x == y:
            return
        if self.parents[y] < self.parents[x]:
            x, y = y, x
        
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    
    def size(self, x: int):
        return -self.parents[self.find(x)]
    
    def same(self, x: int, y: int):
        return self.find(x) == self.find(y)
    
    def members(self, x: int):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
    
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]
    
    def group_count(self):
        return len(self.roots())
    
    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}
    
    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def solve():
    L, R = map(int, rl().split())
    
    if L == 1:
        print(0)
        return
    
    uf = UnionFind(R - L + 1)
    for i in range(L, (R + 1) // 2):
        x = i * 2
        while x <= R:
            uf.union(i - L, x - L)
            x += i
    print(uf.group_count() - 1)


if __name__ == '__main__':
    solve()
0