MoEngage has 3 decks. Each deck consists of N cards, numbered from 1 to N. He draws out 1 card from each deck randomly with each card having an equal probability of being drawn.

MoEngage drew cards numbered A and B from the decks 1 and 2 respectively. Now, he wonders what is the probability that he will end up with a *funny hand* after drawing the third card.

A *funny hand* is when 3 consecutive numbered cards are present in your hand.  
Help MoEngage calculate the probability of ending up with a *funny hand* after drawing the third card.

If the final probability of ending up with a *funny hand* is P, you need to print the value P\cdot N. It can be shown that this value is an integer.

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

- First line will contain T, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input consisting of three space-separated integers N, A, and B denoting the size of each deck, the card drawn from the first deck, and the card drawn from the second deck respectively.

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

For each test case, output a single integer, denoting P\cdot N on a separate line.

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

$1 ≤ T ≤ 1000$
$3 ≤ N ≤ 1000$
$1 ≤ A, B ≤ N$

----- Sample Input 1 ------ 
3
4 2 3
6 2 1
5 2 5

----- Sample Output 1 ------ 
2
1
0

----- explanation 1 ------ 
Test case $1$: Drawing the cards $1$ or $4$ will result in a funny hand. Hence, the probability will be $P = \frac{1}{2}$. The value $P \cdot N = \frac{1}{2} \cdot 4 = 2$.

Test case $2$: Drawing the card $3$ will result in a funny hand. Hence, the probability will be $P = \frac{1}{6}$. The value $P \cdot N = \frac{1}{6} \cdot 6 = 1$.

Test case $3$: No matter what card MoEngage draws, he will never end up with a funny hand.

Difficulty: EASY
Tags: Mathematics

Probability

Starter code:

Solutions:
for _ in range(int(input())):
	(N, A, B) = map(int, input().split())
	if A - B > 2 or B - A > 2 or A == B:
		print(0)
	elif A == 1 or B == 1 or A - B == 2 or (B - A == 2) or (A == N) or (B == N):
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	(n, a, b) = tuple(map(int, input().split()))
	if a - b == 1 or b - a == 1 or b - a == 2 or (a - b == 2):
		if a == 1 or b == 1 or b == n or (a == n) or (b - a == 2) or (a - b == 2):
			print(1)
		else:
			print(2)
	else:
		print(0)


i = int(input())
list1 = []
for kk in range(i):
	kk = input().split()
	t = 1
	kk2 = []
	for ele in kk:
		kk2.append(int(ele))
	if abs(kk2[1] - kk2[2]) == 1 and 1 not in kk2[1:] and (kk2[0] not in kk2[1:]):
		t = 2
	if abs(kk2[1] - kk2[2]) > 2 or kk2[1] - kk2[2] == 0:
		t = 0
	list1.append(t)
for ele in list1:
	print(ele)


a = int(input(''))
for i in range(a):
	(N, a, b) = [int(x) for x in input('').split()]
	temp = a
	a = min(a, b)
	b = max(temp, b)
	if abs(b - a) == 1:
		if a == 1 or b == N:
			print(1)
		else:
			print(2)
	elif abs(b - a) == 2:
		print(1)
	else:
		print(0)


for _ in range(int(input())):
	(n, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	ans = 0
	if b - a == 2:
		print(1)
	elif b - a == 1:
		if a - 1:
			ans += 1
		if b + 1 <= n:
			ans += 1
		print(ans)
	else:
		print(0)


t = int(input())
for i in range(t):
	(n, a, b) = map(int, input().split())
	if abs(a - b) in (1, 2):
		if (a == 1 or b == 1 or a == n or (b == n)) or abs(a - b) == 2:
			print(1)
		else:
			print(2)
	else:
		print(0)


t = int(input())
for i in range(t):
	(n, a, b) = input().split()
	n = int(n)
	a = int(a)
	b = int(b)
	if abs(a - b) == 2:
		print(1)
	elif abs(a - b) == 1 and min(a, b) == 1:
		print(1)
	elif abs(a - b) == 1 and max(a, b) == n:
		print(1)
	elif abs(a - b) == 1:
		print(2)
	else:
		print(0)


for _ in range(int(input())):
	(n, a, b) = map(int, input().split())
	if abs(a - b) == 1:
		if max(a, b) + 1 <= n and min(a, b) - 1 > 0:
			print(2)
		elif max(a, b) + 1 <= n or min(a, b) - 1 > 0:
			print(1)
		else:
			print(0)
	elif abs(a - b) == 2:
		print(1)
	else:
		print(0)


for _ in range(int(input())):
	(x, y, z) = map(int, input().split())
	if abs(y - z) == 1:
		print(1) if min(y, z) == 1 or max(y, z) == x else print(2)
	elif abs(y - z) == 2:
		print(1)
	else:
		print(0)


t = int(input())
for _ in range(t):
	(n, a, b) = map(int, input().split())
	diff = abs(a - b)
	if diff > 2 or diff == 0:
		print(0)
	elif diff == 2 and a <= n and (b <= n):
		print(1)
	elif a == 1 or b == 1 or a == n or (b == n):
		print(1)
	else:
		print(2)


for tc in range(int(input())):
	(n, a, b) = map(int, input().split())
	dif = abs(a - b)
	minc = min(a, b)
	maxc = max(a, b)
	cards = 0
	prob = 0
	if dif > 2 or dif == 0:
		prob = 0
	elif dif == 2:
		prob = 1
	else:
		if minc - 1 > 0:
			prob += 1
		if n - maxc > 0:
			prob += 1
	print(prob)


t = int(input())
for i in range(t):
	(n, a, b) = map(int, input().split())
	c = 0
	if abs(a - b) == 1:
		if min(a, b) == 1 or max(a, b) == n:
			c += 1
		else:
			c += 2
	elif abs(a - b) == 2:
		c += 1
	print(c)


for _ in range(int(input())):
	(n, a, b) = map(int, input().split())
	l = [a, b]
	l.sort()
	if l[1] - l[0] == 2:
		print(1)
	elif l[1] - l[0] == 1:
		if l[0] > 1 and l[1] < n:
			print(2)
		elif l[0] == 1 and l[1] == n:
			print(0)
		else:
			print(1)
	else:
		print(0)


t = int(input())
for i in range(t):
	(n, a, b) = map(int, input().split())
	k = abs(a - b)
	x = min(a, b)
	y = max(a, b)
	if k == 1:
		if x != 1 and y != n:
			print(2)
		elif x == 1 and y != n or (x != 1 and y == n):
			print(1)
		elif x == 1 and y == n:
			print(0)
	elif k == 2:
		print(1)
	else:
		print(0)


t = int(input())
for _ in range(t):
	(n, a, b) = map(int, input().split())
	l = [a, b]
	l = sorted(l)
	if l[1] - l[0] == 1:
		if l[0] == 1 or l[1] == n:
			print('1')
		else:
			print('2')
	elif l[1] - l[0] == 2:
		print('1')
	else:
		print('0')


t = int(input())
while t != 0:
	(n, a, b) = map(int, input().split())
	p = 0
	if abs(a - b) == 1:
		if a != 1 and b != 1:
			p = 1
		if a != n and b != n:
			p += 1
	elif abs(a - b) == 2:
		p = 1
	print(p)
	t -= 1


for _ in range(int(input())):
	(n, a, b) = map(int, input().split())
	p = 0
	if abs(a - b) == 1:
		if a == 1 or b == 1:
			p = 1
		elif a == n or b == n:
			p = 1
		else:
			p = 2
	elif abs(a - b) == 2:
		p = 1
	else:
		p = 0
	print(p)


for i in range(int(input())):
	(n, a, b) = map(int, input().split())
	l = []
	lst = list(range(1, n + 1))
	l.extend([a - 1, a + 1, b - 1, b + 1])
	l1 = []
	ls = []
	for i in l:
		if i in lst and i != a and (i != b):
			l1.append(i)
	l1 = list(set(l1))
	z = []
	for i in l1:
		z = [a, b, i]
		z.sort()
		ls.extend([z])
	c = 0
	for i in range(len(ls)):
		if ls[i][0] == ls[i][1] - 1 and ls[i][1] == ls[i][2] - 1:
			c += 1
	if abs(a - b) > 2 or a == b:
		print(0)
	else:
		print(c)


for _ in range(int(input())):
	(n, a, b) = map(int, input().split())
	c = 0
	l = [a, b]
	if abs(a - b) > 2 or a == b:
		print(0)
	else:
		for i in range(1, n + 1):
			if i not in l:
				l.append(i)
				l.sort()
				if l[2] == l[1] + 1 == l[0] + 2:
					c += 1
				l.remove(i)
		print(c)


for _ in range(int(input())):
	(n, a, b) = map(int, input().split())
	(l, r) = (min(a, b), max(a, b))
	d = r - l
	res = 0
	if d == 2:
		res = 1
	elif d == 1:
		if l > 1:
			res += 1
		if r < n:
			res += 1
	print(res)


for i in range(int(input())):
	(n, C, D) = list(map(int, input().split()))
	if abs(C - D) >= 3 or abs(C - D) <= 0:
		print('0')
	elif (min(C, D) > 1 and max(C, D) < n) and abs(C - D) == 1:
		print('2')
	else:
		print('1')


for k in range(int(input())):
	(w, q, r) = list(map(int, input().split()))
	if abs(q - r) >= 3 or abs(q - r) <= 0:
		print('0')
	elif (min(q, r) > 1 and max(q, r) < w) and abs(q - r) == 1:
		print('2')
	else:
		print('1')


for i in range(int(input())):
	(aa, bb, cc) = map(int, input().split())
	if abs(bb - cc) == 1:
		if (bb == 1 or cc == 1) or (bb == aa or cc == aa):
			print(1)
		else:
			print(2)
	elif abs(bb - cc) == 2:
		print(1)
	else:
		print(0)


r = int(input())
for i in range(r):
	(n, c, f) = map(int, input().split())
	if abs(f - c) > 2 or f - c == 0:
		print(0)
	elif abs(f - c) == 2 or c == 1 or f == 1 or (c == n) or (f == n):
		print(1)
	else:
		print(2)


for j in range(int(input())):
	(m, x, z) = list(map(int, input().split()))
	if abs(x - z) >= 3 or abs(x - z) <= 0:
		print('0')
	elif (min(x, z) > 1 and max(x, z) < m) and abs(x - z) == 1:
		print('2')
	else:
		print('1')


for j in range(int(input())):
	(w, a, b) = list(map(int, input().split()))
	if abs(a - b) >= 3 or abs(a - b) <= 0:
		print('0')
	elif (min(a, b) > 1 and max(a, b) < w) and abs(a - b) == 1:
		print('2')
	else:
		print('1')


for teesko in range(int(input())):
	(apple, ball, cat) = map(int, input().split())
	if abs(ball - cat) == 1:
		if (ball == 1 or cat == 1) or (ball == apple or cat == apple):
			print(1)
		else:
			print(2)
	elif abs(ball - cat) == 2:
		print(1)
	else:
		print(0)


for i in range(int(input())):
	(ag, bg, cg) = map(int, input().split())
	if abs(bg - cg) == 1:
		if (bg == 1 or cg == 1) or (bg == ag or cg == ag):
			print(1)
		else:
			print(2)
	elif abs(bg - cg) == 2:
		print(1)
	else:
		print(0)


for i in range(int(input())):
	(n, a, b) = map(int, input().split())
	s = min(a, b)
	q = max(a, b)
	r = abs(a - b)
	if (s > 1 and q < n) and (a + 1 == b or b + 1 == a):
		print('2')
	elif r > 2 or r < 1:
		print('0')
	else:
		print('1')


for i in range(int(input())):
	(v, q, j) = list(map(int, input().split()))
	if abs(q - j) >= 3 or abs(q - j) <= 0:
		print('0')
	elif (min(q, j) > 1 and max(q, j) < v) and abs(q - j) == 1:
		print('2')
	else:
		print('1')


j = int(input())
for i in range(j):
	(r, a, b) = map(int, input().split())
	if abs(b - a) > 2 or b - a == 0:
		print(0)
	elif abs(b - a) == 2 or a == 1 or b == 1 or (a == r) or (b == r):
		print(1)
	else:
		print(2)


for i in range(int(input())):
	(qn, qa, qb) = list(map(int, input().split()))
	if abs(qa - qb) >= 3 or abs(qa - qb) <= 0:
		print('0')
	elif (min(qa, qb) > 1 and max(qa, qb) < qn) and abs(qa - qb) == 1:
		print('2')
	else:
		print('1')


t = int(input())
for i in range(t):
	(n, k, m) = map(int, input().split())
	p = min(k, m)
	q = max(k, m)
	r = abs(k - m)
	if (p > 1 and q < n) and (k + 1 == m or m + 1 == k):
		print('2')
	elif r > 2 or r < 1:
		print('0')
	else:
		print('1')


for i in range(int(input())):
	(d, e, f) = map(int, input().split())
	if abs(e - f) == 1:
		if (e == 1 or f == 1) or (e == d or f == d):
			print(1)
		else:
			print(2)
	elif abs(e - f) == 2:
		print(1)
	else:
		print(0)


for i in range(int(input())):
	(g, y, z) = map(int, input().split())
	if abs(y - z) == 1:
		if (y == 1 or z == 1) or (y == g or z == g):
			print(1)
		else:
			print(2)
	elif abs(y - z) == 2:
		print(1)
	else:
		print(0)


for i in range(int(input())):
	(o, p, x) = list(map(int, input().split()))
	if abs(p - x) >= 3 or abs(p - x) <= 0:
		print('0')
	elif (min(p, x) > 1 and max(p, x) < o) and abs(p - x) == 1:
		print('2')
	else:
		print('1')


for i in range(int(input())):
	(x, y, c) = map(int, input().split())
	if abs(y - c) == 1:
		if (y == 1 or c == 1) or (y == x or c == x):
			print(1)
		else:
			print(2)
	elif abs(y - c) == 2:
		print(1)
	else:
		print(0)


for i in range(int(input())):
	(i, j, k) = map(int, input().split())
	if abs(j - k) == 1:
		if (j == 1 or k == 1) or (j == i or k == i):
			print(1)
		else:
			print(2)
	elif abs(j - k) == 2:
		print(1)
	else:
		print(0)


T = int(input())
for i in range(T):
	(N, A, B) = map(int, input().split())
	if abs(A - B) > 2:
		print(0)
	elif abs(A - B) == 1:
		if A == 1 or B == 1 or A == N or (B == N):
			print(1)
		else:
			print(2)
	elif abs(A - B) == 2:
		print(1)
	else:
		print(0)