struct UnionFind { n: usize, parents: Vec, size: Vec, vals: Vec, } impl UnionFind { fn new(n: usize) -> Self { UnionFind { n: n, parents: (0..n).collect(), size: vec![1usize; n], vals: vec![0isize; n], } } 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.find(a); let y = self.find(b); if self.size[x] <= self.size[y] { self.size[y] += self.size[x]; self.size[x] = 0; self.vals[x] -= self.vals[y]; self.parents[x] = self.parents[y]; } else { self.size[x] += self.size[y]; self.size[y] = 0; self.vals[y] -= self.vals[x]; 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]); p } fn addval(&mut self, idx: usize, val: isize) { let idx = self.find(idx); self.vals[idx] += val; } fn getval(&self, idx: usize) -> isize { let mut ret = self.vals[idx]; let mut idx = idx; while self.parents[idx] != idx { idx = self.parents[idx]; ret += self.vals[idx]; } ret } } 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+1); for i in l..r { for j in 1.. { let val = i*j; if val > r { break; } uf.unite(i, val); } } let mut result = 0usize; for i in l..r { if !uf.equiv(i, i+1) { result += 1; uf.unite(i, i+1); } } println!("{}", result); }