結果

問題 No.1059 素敵な集合
ユーザー uni_pythonuni_python
提出日時 2020-06-19 00:03:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 2,751 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 82,488 KB
最終ジャッジ日時 2023-09-30 15:39:29
合計ジャッジ時間 3,868 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,416 KB
testcase_01 AC 123 ms
77,712 KB
testcase_02 AC 124 ms
77,964 KB
testcase_03 AC 73 ms
71,368 KB
testcase_04 AC 72 ms
71,392 KB
testcase_05 AC 73 ms
71,308 KB
testcase_06 AC 120 ms
77,812 KB
testcase_07 AC 126 ms
77,732 KB
testcase_08 AC 126 ms
77,904 KB
testcase_09 AC 119 ms
77,176 KB
testcase_10 AC 124 ms
77,512 KB
testcase_11 AC 126 ms
77,996 KB
testcase_12 AC 135 ms
77,892 KB
testcase_13 AC 131 ms
78,172 KB
testcase_14 AC 84 ms
76,028 KB
testcase_15 AC 142 ms
79,156 KB
testcase_16 AC 122 ms
77,648 KB
testcase_17 AC 128 ms
77,724 KB
testcase_18 AC 84 ms
82,488 KB
testcase_19 AC 143 ms
78,396 KB
testcase_20 AC 149 ms
78,464 KB
testcase_21 AC 138 ms
79,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
mod=10**9+7

def main():
    ################
    class UnionFind():
        """
        parents : 親要素(findしない場合は根ではないことの注意),根の場合は"-(要素数)"
        find(x):要素xの属するグループの根を返す
        size(x):要素xの属するグループの要素数を返す
        same(x,y):x,yが同じグループに属しているか返す
        members(x):要素xが属するグループに属する要素をリストで返す
        roots:全ての根の要素を返す
        group_count():グループの数を返す
        all_group_members():{根要素:[そのグループに含まれる要素のリスト]}の辞書を返す
        """
        def __init__(self, n):
            self.n = n
            self.parents = [-1] * n

        def find(self, x):
            #根を探す&つなぎかえる
            if self.parents[x] < 0:
                return x
            else:
                self.parents[x] = self.find(self.parents[x])
                return self.parents[x]

        def union(self, x, y):
            x = self.find(x)
            y = self.find(y)

            if x == y:
                return

            if self.parents[x] > self.parents[y]:
                x, y = y, x

            self.parents[x] += self.parents[y]
            self.parents[y] = x

        def size(self, x):
            return -self.parents[self.find(x)]

        def same(self, x, y):
            return self.find(x) == self.find(y)

        def members(self, x):
            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())
        
    ################


    
    L,R=MI()
    
    #割り切れれば0,そうでなければ1つしたのものを使えば1のコストで達成.上から順番に消せば達成可能.
    #かと思ったけど,倍数があれば約数の方もコスト0.これは順番に注意=>約数倍数を持つものをグルーピング
    uf=UnionFind(R-L+1)
    
    for i in range(L,R+1):
        x=i
        while True:
            x+=i
            if x>R:
                break
            uf.union(i-L,x-L)
            
    print(uf.group_count()-1)


main()
0