結果

問題 No.1059 素敵な集合
ユーザー convexineqconvexineq
提出日時 2020-05-22 21:34:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 508 ms / 2,000 ms
コード長 1,260 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 80,656 KB
最終ジャッジ日時 2023-09-30 15:25:23
合計ジャッジ時間 5,075 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,192 KB
testcase_01 AC 508 ms
80,276 KB
testcase_02 AC 130 ms
78,508 KB
testcase_03 AC 77 ms
71,372 KB
testcase_04 AC 77 ms
71,088 KB
testcase_05 AC 72 ms
71,476 KB
testcase_06 AC 119 ms
78,352 KB
testcase_07 AC 123 ms
78,164 KB
testcase_08 AC 130 ms
78,600 KB
testcase_09 AC 112 ms
78,020 KB
testcase_10 AC 165 ms
78,428 KB
testcase_11 AC 126 ms
78,264 KB
testcase_12 AC 119 ms
78,040 KB
testcase_13 AC 158 ms
79,696 KB
testcase_14 AC 83 ms
76,672 KB
testcase_15 AC 216 ms
79,936 KB
testcase_16 AC 125 ms
78,276 KB
testcase_17 AC 121 ms
78,156 KB
testcase_18 AC 81 ms
79,340 KB
testcase_19 AC 485 ms
80,308 KB
testcase_20 AC 490 ms
80,656 KB
testcase_21 AC 195 ms
80,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]

# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

l,r = [int(i) for i in readline().split()]


UF = UnionFind(r+1)
res = r-l+1
for i in range(l,r+1):
    for j in range(i+i,r+1,i):
        if UF.merge(i,j):
            res -= 1


print(res-1)



0