use std::io::{stdout, BufWriter, Write}; use std::io; #[allow(dead_code)] fn read() -> T { let mut n = String::new(); io::stdin().read_line(&mut n).unwrap(); n.trim().parse().ok().unwrap() } #[allow(dead_code)] fn read_vec() -> Vec { let mut n = String::new(); io::stdin().read_line(&mut n).unwrap(); n.trim() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } fn main() { let out = stdout(); let mut writer = BufWriter::new(out.lock()); solve(&mut writer); writer.flush().unwrap(); } fn solve(mut writer: W) { #[allow(unused_macros)] macro_rules! print { ($fmt:expr) => { write!(writer, $fmt).unwrap() }; ($fmt:expr, $($arg:tt)*) => { write!(writer, $fmt, $($arg)*).unwrap() }; } #[allow(unused_macros)] macro_rules! println { ($fmt:expr) => { writeln!(writer, $fmt).unwrap() }; ($fmt:expr, $($arg:tt)*) => { writeln!(writer, $fmt, $($arg)*).unwrap() }; } // Main Code let s: usize = read(); for _ in 0..s { let data = read_vec::(); let x = data[0]; let y = data[1]; let mut cand_a = std::collections::HashSet::new(); let n1 = x + y; let n2 = if x > y { x - y } else { y - x }; let mut d: usize = 1; while d * d <= n1 { if n1 % d == 0 { let a = d - 1; if a >= 2 && n2 % (a - 1) == 0 { cand_a.insert(a); } let a = n1 / d - 1; if a >= 2 && n2 % (a - 1) == 0 { cand_a.insert(a); } } d += 1; } let mut ans = 0; for a in cand_a { let num = a * a - 1; if a * x > y && a * y > x && (a * x - y) % num == 0 && (a * y - x) % num == 0 { ans += 1; } } if x == y { ans += x - 1; } println!("{}", ans); } }