struct UnionFind { n: usize, parents: Vec, } impl UnionFind { fn new(n: usize) -> Self { UnionFind { n: n, parents: (0..n).collect(), } } fn equiv(&mut self, a: usize, b: usize) -> bool { self.find(a) == self.find(b) } fn unite(&mut self, a: usize, b: usize) { if self.equiv(a, b) { return; } let (a, b) = (a.min(b), a.max(b)); let x = self.parents[a]; let y = self.parents[b]; self.parents[y] = self.parents[x]; } fn find(&mut self, a: usize) -> usize { if self.parents[a] == a { return a; } let p = self.find(self.parents[a]); self.parents[a] = p; p } } fn main() { let mut lr = String::new(); std::io::stdin().read_line(&mut lr).ok(); let lr: Vec = lr.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let l = lr[0]; let r = lr[1]; let mut uf = UnionFind::new(r + 10); for i in l..r { for j in 2..1000000 { let val = i * j; if val > r { break; } uf.unite(i, val); } } let mut result = 0usize; for i in l..r { if uf.find(i) != uf.find(i+1) { result += 1; uf.unite(i, i+1); } } println!("{}", result); }