結果

問題 No.1059 素敵な集合
ユーザー stngstng
提出日時 2022-08-27 16:11:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 1,062 bytes
コンパイル時間 625 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 83,712 KB
最終ジャッジ日時 2024-04-22 14:33:32
合計ジャッジ時間 3,614 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
51,968 KB
testcase_01 AC 94 ms
80,512 KB
testcase_02 AC 129 ms
79,852 KB
testcase_03 AC 38 ms
52,224 KB
testcase_04 AC 37 ms
52,352 KB
testcase_05 AC 37 ms
52,096 KB
testcase_06 AC 114 ms
77,952 KB
testcase_07 AC 118 ms
77,568 KB
testcase_08 AC 119 ms
78,848 KB
testcase_09 AC 113 ms
77,568 KB
testcase_10 AC 115 ms
78,352 KB
testcase_11 AC 118 ms
78,428 KB
testcase_12 AC 118 ms
77,856 KB
testcase_13 AC 124 ms
80,228 KB
testcase_14 AC 56 ms
66,688 KB
testcase_15 AC 134 ms
82,272 KB
testcase_16 AC 137 ms
78,484 KB
testcase_17 AC 120 ms
79,360 KB
testcase_18 AC 63 ms
80,000 KB
testcase_19 AC 125 ms
80,828 KB
testcase_20 AC 152 ms
81,656 KB
testcase_21 AC 134 ms
83,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

l,r = map(int,input().split())

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1 for _ in range(n+1)]
    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

n = r+1
ki = UnionFind(n)
for i in range(l,r+1):
    for j in range(i+i,r+1,i):
        if not ki.same_check(i,j):
            ki.union(i,j)

ss = set()

for i in range(l,r+1):
    ss.add(ki.find(i))

print(len(ss)-1)
0