Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well.

Alice and Bob are two friends. Initially, the skill levels of them are zero. They work on alternative days, i.e one of Alice and Bob works on the odd-numbered days(1, 3, 5, \dots) and the other works on the even-numbered days (2, 4, 6, \dots). The skill levels of Alice and Bob increase by A, B respectively on the days they work.

Determine if it is possible that the skill levels of Alice and Bob become exactly P, Q respectively on some day.

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

- The first line contains an integer T, denoting the number of test cases. The T test cases then follow:
- The first and only line of each test case contains four space-separated integers A, B, P, Q.

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

For each test case, print YES if it is possible that the skill levels of Alice and Bob become exactly P, Q on some day, otherwise print NO.

You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).

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

$1 ≤ T ≤ 10^{3}$
$1 ≤ A, B, P, Q ≤ 10^{9}$

------ subtasks ------ 

Subtask 1 (100 points): Original constraints

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

----- Sample Output 1 ------ 
YES
NO 
YES
NO

----- explanation 1 ------ 
Test Case $1$: Alice works on the first day and gains skill level $1$. Bob works on the second day and gains skill level $2$.

Test Case $2$: There is no possible way that the skill levels of Alice and Bob become $3$ and $2$ respectively.

Test Case $3$: Bob works on the first and third day and Alice works on the second day. Hence after the third day, the skill levels of Alice and Bob become $1\cdot4 = 4$ and $2 \cdot 3 = 6$ respectively.

Difficulty: MEDIUM
Tags: Number theory

Mathematics

Starter code:

Solutions:
for _ in range(int(input())):
	(a, b, p, q) = map(int, input().split())
	if p % a == 0 and q % b == 0:
		c = p // a
		d = q // b
		e = abs(c - d)
		if e == 0 or e == 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	(a, b, p, q) = map(int, input().split())
	if a > p or b > q:
		print('NO')
	elif p % a == 0 and q % b == 0:
		print('YES') if abs(p // a - q // b) <= 1 else print('NO')
	else:
		print('NO')


t = int(input())
for i in range(t):
	(a, b, p, q) = list(map(int, input().split()))
	if p % a == 0 and q % b == 0:
		m = p // a
		n = q // b
		alice = set([2 * m - 1, 2 * m, 2 * m + 1])
		bob = set([2 * n - 1, 2 * n, 2 * n + 1])
		intersection_set = alice.intersection(bob)
		if len(intersection_set) > 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for i in range(int(input())):
	(a, b, p, q) = map(int, input().split())
	d1 = p / a
	d2 = q / b
	if d1 - int(d1) != 0 and d2 - int(d2) != 0:
		print('NO')
	elif d1 - d2 == 1 or d2 - d1 == 1 or d1 == d2:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(0, t):
	(a, b, p, q) = map(int, input().split())
	if p % a == 0 and q % b == 0:
		if abs(p // a - q // b) == 0 or abs(p // a - q // b) == 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if c % a == 0 and d % b == 0:
		s = c // a
		t = d // b
		if abs(s - t) > 1:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if c % a == 0 and d % b == 0:
		x = c // a
		y = d // b
		if abs(x - y) > 1:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


for x in range(int(input())):
	(a, b, p, q) = [int(d) for d in input().split()]
	if p % a == 0 and q % b == 0:
		x = p // a
		y = q // b
		if abs(x - y) > 1:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	l = list(map(int, input().split()))
	(a, b, p, q) = (l[0], l[1], l[2], l[3])
	if p % a or q % b:
		print('NO')
	else:
		rd1 = p // a
		rd2 = q // b
		if rd1 == rd2 or abs(rd1 - rd2) == 1:
			print('YES')
		else:
			print('NO')


def fun(a, b, x, y):
	if x % a == 0:
		A = x / a
		if y % b == 0:
			B = y / b
			if A == B or abs(A - B) == 1:
				print('YES')
			else:
				print('NO')
		else:
			print('NO')
	else:
		print('NO')
n = int(input())
if 1 <= n <= pow(10, 3):
	for i in range(n):
		(a, b, x, y) = map(int, input().split())
		p = pow(10, 9)
		if 1 <= a <= p and 1 <= b <= p and (1 <= x <= p) and (1 <= y <= p):
			if a > b:
				fun(a, b, x, y)
			else:
				fun(b, a, y, x)
		else:
			print('NO')


T = int(input())
for _ in range(T):
	(A, B, P, Q) = [int(i) for i in input().split(' ')]
	if A == P and B == Q:
		print('YES')
		continue
	if A > P or B > Q:
		print('NO')
		continue
	if P % A != 0 or Q % B != 0:
		print('NO')
		continue
	aliceDays = P / A
	bobDays = Q / B
	if abs(aliceDays - bobDays) <= 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(A, B, P, Q) = map(int, input().split())
	if P % A == 0:
		alice = P / A
	else:
		print('NO')
		continue
	if Q % B == 0:
		bob = Q / B
	else:
		print('NO')
		continue
	if abs(alice - bob) > 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	(a, b, p, q) = map(int, input().split())
	if p % a == 0 and q % b == 0:
		x = p // a
		y = q // b
		if x == y or x + 1 == y or x == y + 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for i in range(int(input())):
	(a, b, p, q) = map(int, input().split(' '))
	if p % a == 0 and q % b == 0:
		(c1, c2) = (p / a, q / b)
		if abs(c1 - c2) <= 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	abpq = [int(x) for x in input().split()]
	if abpq[2] % abpq[0] == 0 and abpq[3] % abpq[1] == 0:
		d1 = abpq[2] / abpq[0]
		d2 = abpq[3] / abpq[1]
		if abs(d1 - d2) <= 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


t = int(input())
while t:
	(a, b, p, q) = (int(i) for i in input().split())
	if p % a == 0 and q % b == 0:
		if p // a == q // b or p // a == q // b + 1 or p // a == q // b - 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')
	t -= 1


for _ in range(int(input())):
	(a, b, p, q) = map(int, input().split())
	x = abs(p // a - q // b)
	if p % a or q % b:
		print('NO')
		continue
	if x <= 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	l = list(map(int, input().split()))
	a = l[0]
	b = l[1]
	p = l[2]
	q = l[3]
	if p % a != 0 or q % b != 0:
		print('NO')
	else:
		alice = p // a
		bob = q // b
		if abs(alice - bob) <= 1:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	(a, b, p, q) = [int(X) for X in input().split()]
	if abs(p / a - q / b) <= 1 and p % a == 0 and (q % b == 0):
		print('YES')
	else:
		print('NO')


def main():
	for _ in range(int(input())):
		(a, b, p, q) = map(int, input().split())
		if p % a != 0 or q % b != 0:
			print('NO')
		elif p / a == q / b or p / a == q / b + 1 or p / a == q / b - 1:
			print('YES')
		else:
			print('NO')
main()


import collections as clts
import math
import heapq
MOD = 10 ** 9 + 7

def gcd(x, y):
	if y == 0:
		return x
	else:
		return gcd(y, x % y)

def lcm(x, y):
	return int(x / gcd(x, y) * y)

def take_input(type='list-int'):
	if type == 'list-int':
		return list(map(int, input().split()))
	elif type == 'list-float':
		return list(map(float, input().split()))
	else:
		return None
for _ in range(int(input())):
	(A, B, P, Q) = take_input()
	possible = True
	if P % A == 0 and Q % B == 0:
		diff1 = abs(P // A - Q // B)
		if diff1 > 1:
			possible = False
	else:
		possible = False
	if possible:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	(A, B, P, Q) = list(map(int, input().split()))
	alice = P / A
	bob = Q / B
	if (alice - bob == 1 or alice - bob == 0) and (P % A == 0 and Q % B == 0):
		print('YES')
	elif (bob - alice == 1 or bob - alice == 0) and (P % A == 0 and Q % B == 0):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(A, B, P, Q) = list(map(int, input().split()))
	t1 = P // A
	t2 = Q // B
	if (P % A == 0 and Q % B == 0) and (t1 == t2 or t1 + 1 == t2 or t2 + 1 == t1):
		print('YES')
	else:
		print('NO')


T = int(input())
for _ in range(T):
	(a, b, p, q) = list(map(int, input().split()))
	if p % a != 0 or q % b != 0:
		print('NO')
		continue
	k = p / a
	l = q / b
	if k == l or l - k == 1 or l - k == -1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(a, b, p, q) = list(map(int, input().split()))
	if p % a != 0 or q % b != 0:
		print('NO')
		continue
	da = p / a
	db = q / b
	if da == db or db - da == 1 or db - da == -1:
		print('YES')
	else:
		print('NO')


def solution():
	(A, B, P, Q) = map(int, input().split())
	Alice = P / A
	Bob = Q / B
	if P % A == 0 and Q % B == 0 and (abs(Alice - Bob) <= 1):
		print('YES')
	else:
		print('NO')
for i in range(int(input())):
	solution()


def skillLevel(a, b, p, q):
	if p % a == 0 and q % b == 0 and (abs(p / a - q / b) <= 1):
		print('YES')
	else:
		print('NO')
	return None
for _ in range(int(input())):
	(a, b, p, q) = map(int, input().split())
	skillLevel(a, b, p, q)


t = int(input())
while t:
	t -= 1
	li = [int(x) for x in input().split()]
	x = li[2] / li[0]
	y = li[3] / li[1]
	if li[2] % li[0] == 0 and li[3] % li[1] == 0:
		if abs(x - y) == 1 or abs(x - y) == 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	t1 = c // a
	t2 = c % a
	p1 = d // b
	p2 = d % b
	if t2 == 0 and p2 == 0 and (abs(t1 - p1) <= 1):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(a, b, p, q) = map(int, input().split())
	if int(p / a) == p / a and int(q / b) == q / b:
		alice = p // a
		bob = q // b
		if abs(alice - bob) <= 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for r in range(int(input())):
	(a, b, p, q) = map(int, input().split(' '))
	na = int(p / a)
	nb = int(q / b)
	if p % a != 0 or q % b != 0:
		print('NO')
		continue
	if na == nb:
		print('YES')
	elif na > nb:
		if na - 1 == nb:
			print('YES')
		else:
			print('NO')
	elif nb - 1 == na:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
for _ in range(int(input())):
	(a, b, p, q) = map(int, input().split())
	if q % b == 0 and p % a == 0:
		n = abs(q // b - p // a)
		if n == 1 or n == 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	(a, b, p, q) = map(int, input().split())
	ans = False
	if p % a == 0 and q % b == 0:
		if abs(p / a - q / b) < 2:
			ans = True
	if ans:
		print('YES')
	else:
		print('NO')