Chef has two distinct positive integers A and B. 

Chef wonders how many distinct values are possible for the expression \mathrm{gcd}(A+X,B+X), where X can take any non-negative integer value.

Help Chef find this value.

Here, \mathrm{gcd} stands for [Greatest Common Divisor].

------ Input Format ------ 

- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first and only line of each test case contains two distinct space separated integers A and B.

------ Output Format ------ 

For each testcase, output the number of distinct values of the expression \mathrm{gcd}(A+X,B+X), where X can take any non-negative integer value.

------ Constraints ------ 

$1 ≤ T ≤ 1000$
$1 ≤ A, B ≤ 10^{9}$
$A \neq B$

----- Sample Input 1 ------ 
2
1 2
12 8

----- Sample Output 1 ------ 
1
3

----- explanation 1 ------ 
Test case 1: Here $\mathrm{gcd}(1+X, 2+X) = 1$ no matter what value of $X$ you choose.

Test case 2: 
- If we take $X = 0$, $\mathrm{gcd}(12, 8) = 4$.
- If we take $X = 1$, $\mathrm{gcd}(13, 9) = 1$.
- If we take $X = 2$, $\mathrm{gcd}(14, 10) = 2$.

It can be shown that no other value of $\mathrm{gcd}$ is possible.

Source: codechef | Difficulty: EASY
Tags: Number theory

Mathematics


Solutions:
import math
for i in range(int(input())):
	(a, b) = map(int, input().split())
	count = 0
	d = abs(a - b)
	r = math.sqrt(d)
	for i in range(1, int(r + 1)):
		if d % i == 0:
			count = count + 1
			if d // i != i:
				count = count + 1
	print(count)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	diff = abs(b - a)
	all = 0
	for i in range(1, int(diff ** (1 / 2)) + 1):
		if diff % i == 0:
			all += 1
			if i != diff ** (1 / 2):
				all += 1
	print(all)


t = int(input())
for i in range(t):
	(a, b) = list(map(int, input().split()))
	x = abs(a - b)
	count = 0
	j = 1
	while j * j <= x:
		if x % j == 0:
			count = count + 1
			if x / j != j:
				count = count + 1
		j = j + 1
	print(count)


for tc in range(int(input())):
	(a, b) = map(int, input().split())
	d = abs(a - b)
	ans = 0
	i = 1
	while i * i <= d:
		if d % i == 0:
			ans += 1
			if d / i != i:
				ans += 1
		i += 1
	print(ans)


def factors(n):
	fact = 1
	fact_sq = 1
	sum1 = 0
	while fact_sq <= n:
		if n % fact == 0:
			if fact_sq == n:
				sum1 += 1
			else:
				sum1 += 2
		fact += 1
		fact_sq = fact * fact
	return sum1
for _ in range(int(input())):
	(A, B) = map(int, input().split())
	print(factors(abs(A - B)))


from collections import Counter
import math
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	n = abs(a - b)
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(n)
	a = Counter(l)
	count = 1
	for i in a:
		count = count * (a[i] + 1)
	print(count)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	c = abs(a - b)
	d = 0
	i = 1
	while i * i <= c:
		if c % i == 0:
			d += 1
			if c / i != i:
				d += 1
		i += 1
	print(d)


t = int(input())
for i in range(0, t):
	(a, b) = map(int, input().split())
	z = abs(a - b)
	k = 0
	for i in range(1, int(z ** 0.5) + 1):
		if z % i == 0:
			k = k + 1
			if i != z ** 0.5:
				k = k + 1
	print(k)


import math
test = int(input())
for i in range(test):
	(din, sas) = list(map(int, input().split()))
	pan = 0
	jeevi = abs(sas - din)
	sinba = math.sqrt(jeevi)
	sinba = int(sinba)
	for i in range(1, sinba + 1):
		if jeevi % i == 0:
			if jeevi // i == i:
				pan = pan + 1
			else:
				pan = pan + 2
	print(pan)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	res = abs(a - b)
	cnt = 0
	i = 1
	while i * i <= res:
		if res % i == 0:
			cnt += 1
			if res / i != i:
				cnt += 1
		i += 1
	print(cnt)


t = int(input())
while t:
	(a, b) = [int(i) for i in input().split()]
	num = abs(a - b)
	ans = 0
	for i in range(1, int(pow(num, 0.5)) + 1):
		if num % i == 0:
			ans += 1
			if i != num // i:
				ans += 1
	print(ans)
	t -= 1


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	n = abs(a - b)
	s = 0
	for i in range(1, int(pow(n, 0.5)) + 1):
		if n % i == 0:
			s += 1
			if i != n // i:
				s += 1
	print(s)


import math

def factor(n):
	sq = int(math.sqrt(n))
	fact = 1
	sum1 = 0
	while fact <= sq:
		if n % fact == 0:
			if fact == math.sqrt(n):
				sum1 += 1
			else:
				sum1 += 2
		fact += 1
	return sum1
a = int(input())
for i in range(a):
	(x, y) = map(int, input().split())
	print(factor(abs(x - y)))


for _ in range(int(input())):
	(A, B) = map(int, input().split())
	m = abs(B - A)
	cnt = 0
	for i in range(1, int(pow(m, 0.5)) + 1):
		if m % i == 0:
			cnt += 1
			if i != m // i:
				cnt += 1
	print(cnt)


import math
t = int(input())
for i in range(t):
	[A, B] = [int(i) for i in input().split()]
	c = 0
	for j in range(1, math.floor(math.sqrt(abs(B - A))) + 1):
		if abs(B - A) % j == 0:
			if abs(B - A) // j != j:
				c += 2
			else:
				c += 1
	print(c)


import math

def pf(num):
	f = [i for i in range(1, int(math.sqrt(num)) + 1) if num % i == 0]
	f2 = [num / i for i in f]
	f3 = list(set(f + f2))
	return len(f3)
t = int(input())
while t:
	t = t - 1
	(a, b) = map(int, input().split())
	diff = abs(b - a)
	print(pf(diff))


import math
t = int(input())
for tc in range(t):
	(a, b) = map(int, input().split())
	c = 0
	for i in range(1, int(math.sqrt(abs(a - b))) + 1):
		if abs(a - b) % i == 0:
			c += 1
			if i != math.sqrt(abs(a - b)):
				c += 1
	print(c)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	z = abs(x - y)
	count = 0
	for i in range(1, int(z ** 0.5) + 1):
		if z % i == 0:
			count += 1
			if i != z ** 0.5:
				count += 1
	print(count)


def find_factors(n):
	i = 1
	total_factors = 0
	while i * i <= n:
		if n % i == 0:
			if n // i == i:
				total_factors += 1
			else:
				total_factors += 2
		i += 1
	return total_factors
T = int(input())
for i in range(T):
	(x, y) = map(int, input().split())
	z = abs(x - y)
	print(find_factors(z))


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	num = abs(a - b)
	ans = 0
	for i in range(1, int(num ** 0.5 + 1)):
		if num % i == 0:
			ans += 1
			if i != num // i:
				ans += 1
	print(ans)


import math
for i in range(int(input())):
	(a, b) = map(int, input().split())
	absolute_val = int(abs(a - b))
	answer = 0
	sqrtt = math.sqrt(absolute_val)
	sqrtt += 1
	sqrtt = int(sqrtt)
	for i in range(1, sqrtt):
		if absolute_val % i == 0:
			answer += 1
			if i != absolute_val // i:
				answer += 1
	print(answer)


t = int(input())
while t > 0:
	t -= 1
	(a, b) = map(int, input().split())
	n = abs(a - b)
	count = 0
	for i in range(1, int(pow(n, 0.5)) + 1):
		if n % i == 0:
			count += 1
			if i != n // i:
				count += 1
	print(count)


import math
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	count = 0
	n = abs(a - b)
	for i in range(1, int(math.sqrt(n)) + 1):
		if n % i == 0:
			count += 1
			if i != n // i:
				count += 1
	print(count)


t = int(input())
for i in range(t):
	(n1, n2) = map(int, input().split())
	num = abs(n1 - n2)
	ans = 0
	for i in range(1, int(pow(num, 0.5)) + 1):
		if num % i == 0:
			ans += 1
			if i != num // i:
				ans += 1
	print(ans)


from collections import defaultdict
from collections import Counter
import math
import bisect

class dsu:

	def __init__(self):
		self.parent = defaultdict(int)
		self.size = [0] * (n + 1)

	def start(self):
		for i in range(1, n + 1):
			self.parent[i] = i

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u != v:
			self.parent[v] = u

	def treeinput(self, m):
		for i in range(m):
			(u, v) = map(int, input().split())
			self.union(u, v)

	def printt(self):
		print(self.parent)

def setbit(arr):
	mask = 1
	bit = [0] * 32
	for i in range(32):
		for k in arr:
			if k & mask:
				bit[i] += 1
		mask <<= 1
	return bit

def prefix_sum(nums):
	prefix = [0]
	for i in range(len(nums)):
		prefix.append(prefix[-1] + nums[i])
	return prefix

def mx_lensubarray(nums):
	res = 0
	j = 0
	mp = defaultdict(int)
	prefix = prefixsum(nums)
	mxSum = 0
	for i in range(len(nums)):
		if nums[i] in mp:
			temp = mp[nums[i]]
		else:
			temp = 0
		j = max(temp, j)
		mp[nums[i]] = i + 1
		res = max(res, i - j + 1)
		mxSum = max(mxSum, prefix[i + 1] - prefix[j])
	return res

def lenOfLongSubarr(self, A, N, k):
	mp = {}
	sm = 0
	mxlen = 0
	for i in range(N):
		sm += A[i]
		if sm == k:
			mxlen = i + 1
		if sm - k in mp:
			mxlen = max(mxlen, i - mp[sm - k])
		if sm not in mp:
			mp[sm] = i
	return mxlen

def no_of_factor(s):
	cnt = 0
	x = int(s ** 0.5)
	for i in range(1, x + 1):
		if not s % i:
			cnt = cnt + 1 if s / i == i else cnt + 2
		i += 1
	return cnt

def primes(n):
	arr = []
	i = 2
	while i * i <= n:
		if not n % i:
			cnt = 0
			while not n % i:
				cnt += 1
				n //= i
			arr.append([i, cnt])
		i += 1
	if n > 1:
		arr.append([n, 1])
	return arr

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	res = []
	for i in range(2, n + 1):
		if prime[i]:
			res.append(i)
	return res

def treeinput():
	adj = [[] for i in range(n + 1)]
	for i in range(n - 1):
		(u, v) = map(int, input().split())
		adj[u].append(v)
		adj[v].append(u)

def dfs_size(node, parent):
	sum_size[node] = 1
	for child in adj[node]:
		if child != parent:
			dfs_size(child, node)
			sum_size[node] += sum_size[child]

def anda(arr):
	res = arr[0]
	for i in arr:
		res &= i
	return res

def ora(arr):
	res = arr[0]
	for i in arr:
		res |= i
	return res

def xora(arr):
	res = 0
	for i in arr:
		res ^= i
	return res

def ispowerof2(n):
	return (n != 0) & (n & n - 1 == 0)
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	print(no_of_factor(abs(b - a)))


def fct(l):
	cou = 0
	for i in range(1, int(l ** 0.5) + 1):
		if l % i == 0:
			cou += 1
			if i != l ** 0.5:
				cou += 1
	return cou
for j in range(int(input())):
	(a, b) = map(int, input().split())
	c = abs(a - b)
	print(fct(c))


t = int(input())
while t:
	(a, b) = map(int, input().split())
	p = abs(a - b)
	if p == 1 or p == 0:
		print(1)
	else:
		count = 0
		for i in range(1, p // 2 + 1):
			if p / i < i:
				break
			if p % i == 0:
				if p // i == i:
					count += 1
				else:
					count += 2
		print(count)
	t -= 1


for w in range(int(input())):
	(a, b) = map(int, input().split())
	l = []
	d = abs(a - b)
	cnt = 0
	for x in range(1, int(d ** 0.5) + 1):
		if d % x == 0:
			if d // x == x:
				cnt += 1
			else:
				cnt += 2
	print(cnt)


def factors(l):
	cou = 0
	for i in range(1, int(l ** 0.5) + 1):
		if l % i == 0:
			cou += 1
			if i != l ** 0.5:
				cou += 1
	return cou
q = int(input())
for i in range(q):
	np = list((int(j) for j in input().split()))
	cnt = abs(np[0] - np[1])
	print(factors(cnt))


def factors(l):
	cou = 0
	for i in range(1, int(l ** 0.5) + 1):
		if l % i == 0:
			cou += 1
			if i != l ** 0.5:
				cou += 1
	return cou
q = int(input())
for i in range(q):
	(a, b) = map(int, input().split())
	cnt = abs(a - b)
	print(factors(cnt))


import math
while True:
	try:
		t = int(input())
		for i in range(t):
			(a, b) = map(int, input().strip().split())
			count = 0
			temp = abs(a - b)
			for i in range(1, int(temp ** 0.5 + 1)):
				if temp % i == 0:
					count += 1
					if i != temp ** 0.5:
						count += 1
			print(count)
	except EOFError:
		break


def factors(n):
	count = 0
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			count += 1
			if i != n ** 0.5:
				count += 1
	return count
T = int(input())
for i in range(T):
	inp = list((int(j) for j in input().split()))
	c = abs(inp[0] - inp[1])
	print(factors(c))


import math
for i in range(int(input())):
	(a, b) = map(int, input().split())
	x = abs(b - a)
	c = 0
	for i in range(1, int(math.sqrt(x)) + 1):
		if x % i == 0:
			if i == x // i:
				c += 1
			else:
				c += 2
	print(c)


import math
for i in range(int(input())):
	(p, q) = [int(s) for s in input().split()]
	t = 0
	k = abs(p - q)
	for x in range(1, int(math.sqrt(abs(p - q))) + 1):
		if k % x == 0:
			t += 1
			if k // x != x:
				t += 1
	print(t)


import math
for i in range(int(input())):
	(a, b) = map(int, input().split(' '))
	ans = 0
	k = abs(a - b)
	for j in range(1, int(math.sqrt(abs(a - b))) + 1):
		if k % j == 0:
			ans += 1
			if k // j != j:
				ans += 1
	print(ans)


def prime_factors(n):
	q = {2: 0}
	while n % 2 == 0:
		q[2] += 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			if i in q:
				q[i] += 1
			else:
				q[i] = 1
			n = n // i
	if n > 2:
		if n in q:
			q[n] += 1
		else:
			q[n] = 1
	return q
ans = []
for _ in range(int(input())):
	(A, B) = [int(x) for x in input().split()]
	ps = prime_factors(abs(A - B))
	an = 1
	for p in ps:
		an *= 1 + ps[p]
	ans.append(an)
print(*ans, sep='\n')


import math

def countDivisors(n):
	cnt = 0
	for i in range(1, int(math.sqrt(n)) + 1):
		if n % i == 0:
			if n / i == i:
				cnt = cnt + 1
			else:
				cnt = cnt + 2
	return cnt
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	c = max(a, b)
	c = c - min(a, b)
	ans = countDivisors(c)
	print(ans)


import math
t = int(input())
for i in range(0, t):
	(a, b) = [int(x) for x in input().split()]
	j = abs(a - b)
	c = 0
	for x in range(1, int(pow(j, 0.5)) + 1):
		if j % x == 0:
			c += 1
			if x != j / x:
				c += 1
	print(c)


import math
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split(' '))
	c = abs(a - b)
	answer = 0
	for j in range(1, int(c ** 0.5) + 1):
		if c % j == 0:
			answer += 2
	if int(c ** 0.5) == c ** 0.5:
		answer -= 1
	print(answer)


from math import gcd
T = int(input())
for i in range(T):
	(A, B) = map(int, input().split(' '))
	C = abs(A - B)
	ans = 0
	for i in range(1, int(C ** 0.5) + 1):
		if C % i == 0:
			ans += 2
	if int(C ** 0.5) == C ** 0.5:
		ans -= 1
	print(ans)


def factors(n):
	q = set()
	for i in range(1, int(n ** 0.5) + 1):
		if not n % i:
			q.add(i)
			q.add(n // i)
	return len(q)
ans = []
for _ in range(int(input())):
	(A, B) = [int(x) for x in input().split()]
	ans.append(factors(abs(A - B)))
print(*ans, sep='\n')


for _ in range(int(input())):
	(A, B) = map(int, input().split())
	c = abs(A - B)
	ans = 0
	for i in range(1, int(pow(c, 0.5)) + 1):
		if c % i == 0:
			ans += 1
			if i != c // i:
				ans += 1
	print(ans)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	ans = 0
	dif = abs(a - b)
	for j in range(1, int(pow(dif, 0.5)) + 1):
		if dif % j == 0:
			ans += 1
			if j != dif // j:
				ans += 1
	print(ans)


import math
t = int(input())
for ti in range(1, t + 1):
	(a, b) = map(int, input().split())
	count = 0
	diff = abs(a - b)
	for i in range(1, int(math.sqrt(diff)) + 1):
		if diff % i == 0:
			if diff // i == i:
				count += 1
			else:
				count += 2
	print(count)


for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	ans = 0
	diff = abs(b - a)
	for i in range(1, int(diff ** 0.5) + 1):
		if diff % i == 0:
			ans += 2
		if i * i == diff:
			ans -= 1
	print(ans)


import math
t = int(input())
for i in range(t):
	(a, b) = list(map(int, list(input().split())))
	n = abs(b - a)
	c = 0
	d = math.sqrt(n)
	for i in range(1, int(d) + 1):
		if n % i == 0:
			if i * i == n:
				c = c + 1
			else:
				c = c + 2
	print(c)


for t in range(int(input())):
	(a, b) = map(int, input().split())
	num = abs(a - b)
	factors = 0
	for i in range(1, int(pow(num, 0.5)) + 1):
		if num % i == 0:
			factors += 1
			if num // i != i:
				factors += 1
	print(factors)


from math import sqrt, ceil, floor, gcd, log2
import sys
from sys import stdin, stdout
from collections import Counter, defaultdict, OrderedDict, deque
from itertools import combinations, groupby
from time import time
from heapq import heappop, heappush, heapify

def div(n):
	a = 0
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			if n // i == i:
				a += 1
			else:
				a += 2
	return a
t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	print(div(abs(a - b)))


import math
t = int(input())
for x in range(t):
	(A, B) = map(int, input().split())
	n = abs(A - B)
	i = 1
	k = 0
	a = int(math.sqrt(n))
	while i <= a:
		if n % i == 0:
			if n // i == i:
				k += 1
			else:
				k += 2
		i = i + 1
	print(k)


T = int(input())
for x in range(T):
	(a, b) = map(int, input().split())
	c = abs(a - b)
	d = int(c ** 0.5)
	e = 0
	for y in range(1, d + 1):
		if c % y == 0:
			e = e + 1
	if d * d == c:
		print(e * 2 - 1)
	else:
		print(e * 2)


import math

def divisors(n):
	i = 1
	lm = math.sqrt(n)
	cnt = 0
	while i < lm:
		if n % i == 0:
			cnt += 2
		i += 1
	if n % lm == 0:
		cnt += 1
	return cnt
t = int(input())
while t > 0:
	t -= 1
	ip = input().split()
	(a, b) = [int(x) for x in ip]
	print(divisors(abs(a - b)))


import math
for _ in range(int(input())):
	n = input()
	(a, b) = map(int, n.split(' '))
	count = 0
	div = abs(b - a)
	for x in range(1, int(math.sqrt(div)) + 1):
		if div % x == 0:
			if div // x == x:
				count += 1
			else:
				count += 2
	print(count)


import math

def isprime(n):
	flag = 0
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			flag = 1
			break
	if flag == 0:
		return 'true'
	else:
		return 'false'
t = int(input())
for x in range(t):
	(A, B) = map(int, input().split())
	n = abs(A - B)
	i = 1
	k = 0
	a = math.sqrt(n)
	if n == 1:
		k = 1
		print(k)
	elif n > 1 and isprime(n) == 'true':
		k = 2
		print(k)
	else:
		while i <= a:
			if n % i == 0:
				if n // i == i:
					k += 1
				else:
					k += 2
			i = i + 1
		print(k)


def prime_factors(n):
	p = 2
	ret = dict()
	while p ** 2 <= n:
		if n % p == 0:
			ret[p] = 0
			while n % p == 0:
				n //= p
				ret[p] += 1
		p += 1
	if n > 1:
		ret[n] = 1
	return ret

def num_divisors(n):
	ret = 1
	mult = prime_factors(n)
	for p in mult:
		ret *= 1 + mult[p]
	return ret
for _ in range(int(input())):
	(A, B) = map(int, input().split())
	print(num_divisors(abs(A - B)))


import math
test_case = int(input())
for case in range(test_case):
	(a, b) = map(int, input().split())
	sub = abs(a - b)
	ans = 0
	for i in range(1, int(math.sqrt(sub)) + 1):
		if sub % i == 0:
			if sub // i == i:
				ans += 1
			else:
				ans += 2
	print(ans)


test_case = int(input())
for case in range(test_case):
	(a, b) = map(int, input().split())
	sub = abs(a - b)
	ans = 0
	limit = int(sub ** 0.5)
	for i in range(1, limit + 1):
		if sub % i == 0:
			if sub // i == i:
				ans += 1
			else:
				ans += 2
	print(ans)


import math
t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	dif = abs(a - b)
	ans = 0
	i = 1
	for i in range(1, int(math.sqrt(dif)) + 1):
		if dif % i == 0:
			ans += 1
			if dif // i != i:
				ans += 1
	print(ans)


import math
for _ in range(int(input())):
	(A, B) = map(int, input().split())
	num = abs(A - B)
	ans = 0
	for i in range(1, int(pow(num, 0.5)) + 1):
		if num % i == 0:
			ans += 1
			if i != num // i:
				ans += 1
	print(ans)


for _ in range(int(input())):
	count = 0
	j = input().split()
	m = int(j[0])
	n = int(j[1])
	req = abs(m - n)
	for i in range(1, int(req ** 0.5) + 1):
		if req % i == 0:
			count += 1
			if i != req // i:
				count += 1
	print(count)