結果
| 問題 | 
                            No.1059 素敵な集合
                             | 
                    
| コンテスト | |
| ユーザー | 
                             maspy
                         | 
                    
| 提出日時 | 2020-05-22 21:30:32 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 527 ms / 2,000 ms | 
| コード長 | 1,260 bytes | 
| コンパイル時間 | 103 ms | 
| コンパイル使用メモリ | 12,800 KB | 
| 実行使用メモリ | 22,400 KB | 
| 最終ジャッジ日時 | 2024-07-23 09:24:49 | 
| 合計ジャッジ時間 | 4,733 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 19 | 
ソースコード
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
class UnionFind:
    def __init__(self, N):
        self.root = list(range(N))
        self.size = [1] * (N)
        self.n_comp = N
    def find_root(self, x):
        root = self.root
        while root[x] != x:
            root[x] = root[root[x]]
            x = root[x]
        return x
    def merge(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return False
        sx, sy = self.size[x], self.size[y]
        if sx < sy:
            self.root[x] = y
            self.size[y] += sx
        else:
            self.root[y] = x
            self.size[x] += sy
        self.n_comp -= 1
        return True
L, R = map(int, read().split())
U = R + 10
uf = UnionFind(R + 1)
is_prime = [0] * U
is_prime[2] = 1
for p in range(3, R + 1, 2):
    is_prime[p] = 1
for p in range(3, R + 1, 2):
    if p * p > R:
        break
    for i in range(p * p, R + 1, p + p):
        is_prime[i] = 0
primes = [p for p, x in enumerate(is_prime) if x]
for p in primes:
    for i in range(L, R + 1):
        if p * i > R:
            break
        uf.merge(i, p * i)
x = uf.n_comp - L - 1
print(x)
            
            
            
        
            
maspy