#include #include #include #include #include #include #include #include #include #include #include #include #include #include std::vector primes(const int upper) { std::vector is_prime(upper + 1, true); for (auto i = 2; i * i <= upper; ++i) { if (is_prime[i]) { for (auto j = i * i; j <= upper; j += i) { is_prime[j] = false; } } } std::vector result; for (auto i = 2; i <= upper; ++i) { if (is_prime[i]) result.push_back(i); } return result; } std::vector cal_factors(int x, const std::vector& prime) { std::map prime_factors; int size{ 1 }; for (const auto p : prime) { if (p * p > x) break; if (x % p == 0) { int count{ 0 }; while (x % p == 0) { x /= p; ++count; } prime_factors[p] = count;; size *= count + 1; } } if (x > 1) { prime_factors[x] = 1; size <<= 1; } std::vector factors{ 1 }; factors.reserve(size); for (const auto &pair: prime_factors) { for (int i = factors.size() - 1; i >= 0; --i) { factors.push_back(factors[i] * pair.first); for (auto j = 1; j < pair.second; ++j) { factors.push_back(factors.back() * pair.first); } } } return factors; } int main() { std::cin.tie(nullptr); std::cin.sync_with_stdio(false); const auto prime = primes(std::sqrt(500000000) + 1); int s; std::cin >> s; for (; s > 0; --s) { int x, y; std::cin >> x >> y; if (x < y) std::swap(x, y); const auto sum = x + y; const auto diff = x - y; const auto add = cal_factors(sum, prime); const auto sub = cal_factors(diff, prime); std::set sub_set; std::copy(sub.begin(), sub.end(), std::inserter(sub_set, sub_set.begin())); std::cout << std::count_if(add.begin(), add.end(), [&sub_set, sum, diff](const int f) {return sub_set.find(f - 2) != sub_set.end() && sum / f > diff / (f - 2) && (sum / f & 1) == (diff / (f - 2) & 1); }) << '\n'; } }