結果

問題 No.1059 素敵な集合
ユーザー shotoyooshotoyoo
提出日時 2021-05-06 19:30:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 660 ms / 2,000 ms
コード長 1,222 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 87,180 KB
実行使用メモリ 80,948 KB
最終ジャッジ日時 2023-10-12 11:53:46
合計ジャッジ時間 6,814 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,124 KB
testcase_01 AC 630 ms
80,568 KB
testcase_02 AC 140 ms
79,004 KB
testcase_03 AC 70 ms
71,076 KB
testcase_04 AC 70 ms
71,460 KB
testcase_05 AC 70 ms
71,412 KB
testcase_06 AC 136 ms
78,820 KB
testcase_07 AC 137 ms
78,620 KB
testcase_08 AC 145 ms
79,104 KB
testcase_09 AC 114 ms
78,228 KB
testcase_10 AC 207 ms
78,892 KB
testcase_11 AC 140 ms
78,796 KB
testcase_12 AC 133 ms
78,812 KB
testcase_13 AC 193 ms
79,560 KB
testcase_14 AC 79 ms
76,576 KB
testcase_15 AC 293 ms
80,948 KB
testcase_16 AC 144 ms
78,828 KB
testcase_17 AC 134 ms
79,104 KB
testcase_18 AC 77 ms
78,376 KB
testcase_19 AC 660 ms
80,580 KB
testcase_20 AC 645 ms
80,868 KB
testcase_21 AC 252 ms
80,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")

class UF:
    # unionfind
    def __init__(self, n):
        self.n = n
        self.parent = list(range(n))
        self.size = [1] * n
    def check(self):
        return [self.root(i) for i in range(self.n)]
    def root(self, i):
        inter = set()
        while self.parent[i]!=i:
            inter.add(i)
            i = self.parent[i]
        r = i
        for i in inter:
            self.parent[i] = r
        return r
    def connect(self, i, j):
        # 繋いだかどうかを返す
        ri = self.root(i)
        rj = self.root(j)
        if ri==rj:
            return False
        if self.size[ri]<self.size[rj]:
            self.parent[ri] = rj
            self.size[rj] += self.size[ri]
        else:
            self.parent[rj] = ri
            self.size[ri] += self.size[rj]
        return True

l,r = list(map(int, input().split()))
uf = UF(r+1)
c = 0
for i in range(l,r):
    if i*2>r:
        break
    for p in range(i*2, r+1, i):
        if uf.connect(i,p):
            c += 1
ans = (r-l+1) - c - 1
print(ans)
0