A continued fraction of height n is a fraction of form $a_{1} + \frac{1}{a_{2} + \frac{1}{\ldots + \frac{1}{a_{n}}}}$. You are given two rational numbers, one is represented as [Image] and the other one is represented as a finite fraction of height n. Check if they are equal.


-----Input-----

The first line contains two space-separated integers p, q (1 ≤ q ≤ p ≤ 10^18) — the numerator and the denominator of the first fraction.

The second line contains integer n (1 ≤ n ≤ 90) — the height of the second fraction. The third line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^18) — the continued fraction.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.


-----Output-----

Print "YES" if these fractions are equal and "NO" otherwise.


-----Examples-----
Input
9 4
2
2 4

Output
YES

Input
9 4
3
2 3 1

Output
YES

Input
9 4
3
1 2 4

Output
NO



-----Note-----

In the first sample $2 + \frac{1}{4} = \frac{9}{4}$.

In the second sample $2 + \frac{1}{3 + \frac{1}{1}} = 2 + \frac{1}{4} = \frac{9}{4}$.

In the third sample $1 + \frac{1}{2 + \frac{1}{4}} = \frac{13}{9}$.

Difficulty: MEDIUM_HARD
Tags: Mathematics

Implementation

Complete search
Skill types: Complete search

Starter code:

Solutions:
from fractions import *
f = Fraction
(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
a.reverse()
res = f(a[0], 1)
for i in range(1, n):
	res = a[i] + 1 / res
print('YES' if res == f(p, q) else 'NO')


from fractions import gcd
(p, q) = [int(x) for x in input().split()]
n = int(input())
A = [int(x) for x in input().split()]
g = gcd(p, q)
(p, q) = (p // g, q // g)
poss = True
for a in A:
	if q == 0 or p < a * q:
		poss = False
		break
	p -= a * q
	g = gcd(p, q)
	(p, q) = (q // g, p // g)
print('YES' if poss and q == 0 else 'NO')


def main():
	from fractions import Fraction
	(p, q) = map(int, input().split())
	(_, aa) = (input(), list(map(int, input().split())))
	x = Fraction(aa.pop())
	while aa:
		x = aa.pop() + 1 / x
	print(('NO', 'YES')[x == Fraction(p, q)])
main()


from fractions import Fraction
(p, q) = map(int, input().split(' '))
n = int(input())
a = list(map(int, input().split(' ')))

def fract(x):
	if x == n - 1:
		return Fraction(1, a[x])
	elif x >= n:
		return 0
	else:
		return Fraction(1, a[x] + fract(x + 1))
print('YES' if Fraction(p, q) == a[0] + fract(1) else 'NO')


from decimal import *
getcontext().prec = 75
p = input().split()
n = int(input())
c = input().split()
a = '0'
for i in range(n - 1, 0, -1):
	a = str(Decimal(a) + Decimal(c[i]))
	a = str(Decimal('1') / Decimal(str(a)))
a = str(Decimal(a) + Decimal(c[0]))
b = str(Decimal(p[0]) / Decimal(p[1]))
if a == b:
	print('YES')
else:
	print('NO')


import fractions
(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
y = fractions.Fraction(p, q)
x = fractions.Fraction(0)
for it in reversed(a):
	if x != 0:
		x = 1 / x
	x += it
print(['NO', 'YES'][x == y])


from fractions import *
from sys import *
from copy import *
from time import *

class Fraction:

	def __init__(self, x=0, y=1):
		if type(x) == str:
			if x.find('/') != -1:
				a = list(map(int, x.split('/')))
			else:
				a = list(map(int, x.split()))
			self.num = a[0]
			if len(a) == 1:
				self.den = 1
			else:
				self.den = a[1]
		elif type(x) == Fraction:
			(self.num, self.den) = (x.num, x.den)
		else:
			(self.num, self.den) = (x, y)
		self.reduce()

	def __str__(self):
		if self.den == 1:
			return str(int(self.num))
		return str(int(self.num)) + '/' + str(int(self.den))

	def reduce(self):
		d = gcd(abs(self.num), abs(self.den))
		if self.den < 0:
			(self.den, self.num) = (-self.den, -self.num)
		(self.num, self.den) = (self.num // d, self.den // d)

	def __lt__(self, other):
		if type(other) == Fraction:
			return self.num * other.den < other.num * self.den
		else:
			return self.num < self.den * other

	def __le__(self, other):
		if type(other) == Fraction:
			return self.num * other.den <= other.num * self.den
		else:
			return self.num <= self.den * other

	def __gt__(self, other):
		if type(other) == Fraction:
			return self.num * other.den > other.num * self.den
		else:
			return self.num > self.den * other

	def __ge__(self, other):
		if type(other) == Fraction:
			return self.num * other.den >= other.num * self.den
		else:
			return self.num >= self.den * other

	def __eq__(self, other):
		if type(other) == Fraction:
			return self.num * other.den == other.num * self.den
		else:
			return self.num == self.den * other

	def __ne__(self, other):
		if type(other) == Fraction:
			return self.num * other.den != other.num * self.den
		else:
			return self.num != self.den * other

	def __mul__(self, other):
		if type(other) == float:
			return self.num * other / self.den
		elif type(other) == Poly:
			return NotImplemented
		else:
			other = Fraction(other)
			return Fraction(self.num * other.num, self.den * other.den)

	def rmul(self, other):
		return other * self

	def __rmul__(self, other):
		if type(other) == float:
			return self.num * other / self.den
		else:
			other = Fraction(other)
			return Fraction(self.num * other.num, self.den * other.den)

	def __imul__(self, other):
		if type(other) == float:
			self = self.num * other / self.den
		else:
			other = Fraction(other)
			self.num *= other.num
			self.den *= other.den
			self.reduce()
		return self

	def __truediv__(self, other):
		if type(other) == float:
			return self.num / (self.den * other)
		else:
			other = Fraction(other)
			return self * Fraction(other.den, other.num)

	def __rtruediv__(self, other):
		if type(self) == float:
			return other.num / (other.den * self)
		else:
			self = Fraction(self)
			return other * Fraction(self.den, self.num)

	def __itruediv__(self, other):
		if type(other) == float:
			self = self.num / (self.den * other)
		else:
			other = Fraction(other)
			self = self * Fraction(other.den, other.num)
			self.reduce()
		return self

	def __pow__(self, other):
		if type(other) == Fraction:
			return (self.num / self.den) ** (other.num / other.den)
		elif type(other) == int:
			if other > 0:
				return Fraction(self.num ** other, self.den ** other)
			else:
				return 1 / Fraction(self.num ** (-other), self.den ** (-other))
		else:
			return (self.num / self.den) ** other

	def __rpow__(self, other):
		return other ** (self.num / self.den)

	def __ipow__(self, other):
		if other > 0:
			self.num **= other
			self.den **= other
		else:
			self.num **= -other
			self.den **= -other
			(self.num, self.den) = (self.den, self.num)
		return self

	def __add__(self, other):
		if type(other) == float:
			return self.num / self.den + other
		elif type(other) == Poly:
			return NotImplemented
		else:
			other = Fraction(other)
			return Fraction(self.num * other.den + other.num * self.den, self.den * other.den)

	def __radd__(self, other):
		return self + other

	def __iadd__(self, other):
		if type(other) == float:
			self = self.num / self.den + other
		else:
			other = Fraction(other)
			self.num = self.num * other.den + other.num * self.den
			self.den = self.den * other.den
			self.reduce()
		return self

	def __sub__(self, other):
		if type(other) == float:
			return self.num / self.den - other
		elif type(other) == Poly:
			return NotImplemented
		else:
			other = Fraction(other)
			return Fraction(self.num * other.den - other.num * self.den, self.den * other.den)

	def __rsub__(self, other):
		if type(self - other) == Fraction:
			return Fraction(-(self - other).num, (self - other).den)
		else:
			return -(self - other)

	def __isub__(self, other):
		if type(other) == float:
			self = self.num / self.den - other
		else:
			other = Fraction(other)
			self.num = self.num * other.den - other.num * self.den
			self.den = self.den * other.den
			self.reduce()
		return self

	def __pos__(self):
		return self

	def __neg__(self):
		return Fraction(-self.num, self.den)

	def __abs__(self):
		return Fraction(-self.num * int(self.num < 0) + self.num * (self.num >= 0), self.den)

	def __int__(self):
		return self.num // self.den

	def __float__(self):
		return self.num / self.den

	def __round__(self, x=0):
		return round(float(self), x)

class Poly:

	def __init__(self, s=0):
		if type(s) in [int, float, Fraction]:
			self.list = [s]
		elif type(s) == str:
			(self.list, s) = ([], s.split())
			for elem in S:
				self.list += eval(elem)
		elif type(s) == Poly:
			self.list = s.list
		else:
			self.list = list(s)

	def __str__(self):
		(P, S, power, first) = ([chr(8304), chr(185), chr(178), chr(179)] + [chr(i) for i in range(8308, 8314)], '', len(self.list) - 1, True)
		for i in range(len(self.list) - 1, -1, -1):
			current = self.list[i]
			if current != 0:
				if current > 0:
					S += ' + ' * int(not first)
				else:
					S += ' - ' * int(not first) + '-' * int(first)
				current = abs(current)
				if current != 1 or (current == 1 and power == 0):
					if type(current) == Fraction:
						if current.den == 1:
							S += str(int(current.num))
						else:
							S += '(' + str(current) + ')'
					elif type(current) == float:
						S += str(round(current, 3))
					else:
						S += str(current)
				S += 'x' * (power != 0)
				for elem in str(power):
					S += P[int(elem)] * (not (elem in ['0', '1'] and len(str(power)) == 1))
				first = len(S) == 0
			power -= 1
		if S == '':
			return '0'
		return S

	def __neg__(self):
		result = []
		for elem in self.list:
			result.append(-elem)
		return Poly(result)

	def __add__(self, other):
		result = []
		if type(other) == Poly:
			for i in range(max(len(self.list), len(other.list))):
				if i < len(self.list):
					if i < len(other.list):
						result.append(self.list[i] + other.list[i])
					else:
						result.append(self.list[i])
				elif i < len(other.list):
					result.append(other.list[i])
				else:
					result.append(0)
			return Poly(result)
		else:
			lolka = [self.list[0] + other] + self.list[1:]
			return Poly(lolka)

	def __radd__(self, other):
		return self + other

	def __iadd__(self, other):
		self.list = (self + other).list
		return self

	def __sub__(self, other):
		result = []
		if type(other) == Poly:
			for i in range(max(len(self.list), len(other.list))):
				if i < len(self.list):
					if i < len(other.list):
						result.append(self.list[i] - other.list[i])
					else:
						result.append(self.list[i])
				elif i < len(other.list):
					result.append(-other.list[i])
				else:
					result.append(0)
			return Poly(result)
		else:
			lolka = [self.list[0] - other] + self.list[1:]
			return Poly(lolka)

	def __rsub__(self, other):
		return -(self - other)

	def __isub__(self, other):
		self.list = (self - other).list
		return self

	def __or__(self, other):
		if len(self.list) == 1:
			return self.list[0]
		result = self.list[len(self.list) - 1]
		for i in range(len(self.list) - 2, -1, -1):
			result = result * other + self.list[i]
		return result

	def __mul__(self, other):
		(result, p) = ([], Poly(other))
		for i in range(len(self.list)):
			for j in range(len(p.list)):
				if len(result) > i + j:
					result[i + j] += self.list[i] * p.list[j]
				else:
					result += [0] * (i + j - len(result)) + [self.list[i] * p.list[j]]
		return Poly(result)

	def __rmul__(self, other):
		return self * other

	def __imul__(self, other):
		self.list = (self * other).list
		return self

	def __pow__(self, other):
		if other == 0:
			return Poly(1)
		if other % 2 == 1:
			return Poly(self ** (other - 1) * self)
		else:
			lolka = self ** (other // 2)
			return Poly(lolka * lolka)

	def __ipow__(self, other):
		self.list = (self ** other).list
		return self

	def __divmod__(self, other):
		result = [0] * 100
		p = deepcopy(self.list)
		q = Poly(other).list
		(power1, power2) = (len(p) - 1, len(q) - 1)
		while power1 > -1 and p[power1] == 0:
			power1 -= 1
		while power2 > -1 and q[power2] == 0:
			power2 -= 1
		while power1 > -1 and power2 > -1 and (power1 >= power2):
			minus = [0] * (power1 - power2)
			if type(p[power1]) == int and type(q[power2]) == int:
				if p[power1] % q[power2] == 0:
					current = p[power1] // q[power2]
				else:
					current = Fraction(p[power1], q[power2])
			else:
				current = p[power1] / q[power2]
			for elem in q:
				minus += [elem * current]
			for i in range(max(len(p), len(minus))):
				if i < len(p):
					if i < len(minus):
						p[i] -= minus[i]
				elif i < len(minus):
					p.append(-minus[i])
				else:
					p.append(0)
			result[power1 - power2] = current
			while power1 > -1 and p[power1] == 0:
				power1 -= 1
			while power2 > -1 and q[power2] == 0:
				power2 -= 1
		return (Poly(result), Poly(p))

	def __rdivmod__(self, other):
		return divmod(Poly(other), self)

	def __floordiv__(self, other):
		return divmod(self, other)[0]

	def __rfloordiv__(self, other):
		return divmod(Poly(other), self)[0]

	def __ifloordiv__(self, other):
		self.list = divmod(self, other)[0].list
		return self

	def __mod__(self, other):
		return divmod(self, other)[1]

	def __rmod__(self, other):
		return divmod(Poly(other), self)[1]

	def __imod__(self, other):
		self.list = divmod(self, other)[1].list
		return self
(p, q) = list(map(int, input().split()))
lolka = Fraction(p, q)
n = int(input())
A = list(map(int, input().split()))
Answer = Fraction(A[-1])
for i in range(len(A) - 2, -1, -1):
	Answer = 1 / Answer
	Answer += A[i]
if Answer == lolka:
	print('YES')
else:
	print('NO')


from fractions import Fraction

def frac(lis, i):
	if i == len(lis) - 1:
		return lis[i]
	else:
		return lis[i] + 1 / frac(lis, i + 1)

def main():
	linesp = input().split(' ')
	(p, q) = (int(linesp[0]), int(linesp[1]))
	one = Fraction(p, q)
	n = int(input())
	A = []
	for s in input().split(' '):
		A.append(Fraction(int(s)))
	two = frac(A, 0)
	if one == two:
		print('YES')
	else:
		print('NO')
main()


def s():

	def gcd(a, b):
		if a < b:
			(a, b) = (b, a)
		if b:
			return gcd(b, a % b)
		else:
			return a
	pq = list(map(int, input().split()))
	g = gcd(*pq)
	pq[0] //= g
	pq[1] //= g
	input()
	a = list(map(int, input().split()))
	r = [1, 0]
	for i in reversed(a):
		(r[0], r[1]) = (r[1], r[0])
		r[0] += r[1] * i
		g = gcd(r[0], r[1])
		r[0] //= g
		r[1] //= g
	print('YES' if pq == r else 'NO')
s()


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
(p, q) = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
g = gcd(p, q)
p = p // g
q = q // g
k = A[n - 1]
l = 1
for i in range(n - 2, -1, -1):
	(k, l) = (l, k)
	k += A[i] * l
	g = gcd(k, l)
	k = k // g
	l = l // g
if k == p and q == l:
	print('YES')
else:
	print('NO')


(p, q) = list(map(int, input().split()))
n = int(input())
a = list(map(int, input().split()))
fract1 = []
(x, y) = (p, q)
while y > 0:
	if y == 1:
		fract1 += [x - 1, 1]
	else:
		fract1 += [x // y]
	(x, y) = (y, x % y)
fract2 = []
(x, y) = (p, q)
while y > 0:
	fract2 += [x // y]
	(x, y) = (y, x % y)
if fract1 == a or fract2 == a:
	print('YES')
else:
	print('NO')


from fractions import *
(p, q) = map(int, input().split())
a1 = Fraction(p, q)
n = int(input())
aaa = list(map(int, input().split()))
d2 = Fraction(aaa[-1], 1)
for i in range(2, n + 1):
	d2 = 1 / d2 + aaa[-i]
print('YES' if a1 == d2 else 'NO')


from fractions import *
(p, q) = map(int, input().split())
n = int(input())
a = [int(x) for x in input().split()]
a = [0] + a
ans = Fraction(a[n])
for i in range(n - 1, 0, -1):
	ans = Fraction(1, ans)
	ans += Fraction(a[i], 1)
print('YES' if Fraction(p, q) == ans else 'NO')


import sys

def gcd(a, b):
	return a if b == 0 else gcd(b, a % b)

def main():
	(p, q) = map(lambda x: int(x), sys.stdin.readline().split())
	n = int(sys.stdin.readline())
	a = list(map(lambda x: int(x), sys.stdin.readline().split()))
	b = [1] * n
	gg = gcd(p, q)
	p = p // gg
	q = q // gg
	while len(a) > 1:
		x = a[-2]
		y = a[-1]
		z = b[-1]
		a = a[:-1]
		b = b[:-1]
		b[-1] = y
		a[-1] = x * y + z
		gg = gcd(b[-1], a[-1])
		a[-1] = a[-1] // gg
		b[-1] = b[-1] // gg
	print('YES\n' if p == a[0] and q == b[0] else 'NO\n')
main()


(p, q) = input().split()
p = int(p)
q = int(q)
n = int(input())
A = input().split()
a = 1
b = int(A[n - 1])
i = n - 2
while i >= 0:
	aux = b
	b = a + b * int(A[i])
	a = aux
	i = i - 1
print('YES' if p * a == q * b else 'NO')


(p, q) = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
for i in range(n):
	(p, q) = (q, p - A[i] * q)
if p != 0 and q == 0:
	print('YES')
else:
	print('NO')


def foo(x, y, z):
	I = []
	I.append(z)
	I.append(x * z + y)
	return I
(p, q) = map(int, input().split())
n = int(input())
da = map(int, input().split())
a = []
for val in da:
	a.append(val)
a.reverse()
la = len(a)
if la == 1:
	if p == q * a[0]:
		print('YES')
	else:
		print('NO')
else:
	y = 0
	z = 1
	for idx in range(0, la):
		I = foo(a[idx], y, z)
		y = I[0]
		z = I[1]
	if p * y == q * z:
		print('YES')
	else:
		print('NO')


def ratios(p, q, n, lst):
	for i in range(n):
		if p <= 0:
			return 'NO'
		b = q * lst[i]
		p -= b
		g = p
		p = q
		q = g
	if q == 0:
		return 'YES'
	return 'NO'
(P, Q) = [int(j) for j in input().split()]
m = int(input())
a = [int(x) for x in input().split()]
print(ratios(P, Q, m, a))


def gcd(x, y):
	while x > 0 and y > 0:
		if x > y:
			x %= y
		else:
			y %= x
	return x + y
(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
p1 = 1
q1 = a[n - 1]
for i in range(n - 2, -1, -1):
	p1 += a[i] * q1
	z = p1
	p1 = q1
	q1 = z
z = p1
p1 = q1
q1 = z
g = gcd(p, q)
if g != 0:
	p /= g
	q /= g
g = gcd(p1, q1)
if g != 0:
	p1 /= g
	q1 /= g
if p == p1 and q == q1:
	print('YES')
else:
	print('NO')


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def red(pr):
	(a, b) = pr
	g = gcd(a, b)
	return (a // g, b // g)

def rev(pr):
	(a, b) = pr
	return (b, a)

def add(pr, i):
	(a, b) = pr
	a += b * i
	return red((a, b))
import sys
args = list(map(int, sys.stdin.read().split()))
(p, q) = (args[0], args[1])
args = args[2:]
n = args[0]
args = args[1:]
a = args[::-1]
s = (a[0], 1)
a = a[1:]
for i in a:
	s = add(rev(s), i)
if red(s) == red((p, q)):
	print('YES')
else:
	print('NO')


import re
import itertools
from collections import Counter, deque
from fractions import gcd

class Task:
	(p, q) = (0, 0)
	a = []
	answer = ''

	def getData(self):
		(self.p, self.q) = [int(x) for x in input().split(' ')]
		input()
		self.a = [int(x) for x in input().split(' ')]

	def solve(self):
		(p, q) = self.toFraction(self.a)
		if self.p * q == self.q * p:
			self.answer = 'YES'
		else:
			self.answer = 'NO'

	def toFraction(self, a):
		if len(a) == 1:
			return (a[0], 1)
		(q, p) = self.toFraction(a[1:])
		d = gcd(a[0] * q + p, q)
		return ((a[0] * q + p) // d, q // d)

	def printAnswer(self):
		print(self.answer)
task = Task()
task.getData()
task.solve()
task.printAnswer()


(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
g = 0
for i in range(n):
	if p <= 0:
		print('NO\n')
		exit(0)
	b = q * a[i]
	p -= b
	g = p
	p = q
	q = g
if q == 0:
	print('YES')
else:
	print('NO')


def result(x, y, numbers):
	numbers.reverse()
	(a, b) = (1, 0)
	for number in numbers:
		(b, a) = (a, number * a + b)
	return a * y == b * x
(x, y) = [int(item) for item in input().split()]
n = int(input())
numbers = [int(item) for item in input().split()]
if result(x, y, numbers):
	print('YES')
else:
	print('NO')


(p, q) = map(int, input().split())
(n, t) = (int(input()), list(map(int, input().split())))
t.reverse()
(a, b) = (1, t[0])
for c in t[1:]:
	(a, b) = (b, c * b + a)
print('YNEOS'[a * p != b * q::2])


3
(p, q) = tuple(map(int, input().strip().split()))
n = int(input().strip())
a = list(map(int, input().strip().split()))
a.reverse()

def gcd(a, b):
	while a:
		(a, b) = (b % a, a)
	return b
g = gcd(p, q)
(p, q) = (p // g, q // g)
(u, v) = (0, 1)
for i in a:
	(v, u) = (v * i + u, v)
	g = gcd(u, v)
	(u, v) = (u // g, v // g)
if p == v and q == u:
	print('YES')
else:
	print('NO')


from sys import *
from math import *

def toContF(a, b):
	r = []
	while b > 0:
		r.append(a // b)
		a %= b
		(a, b) = (b, a)
	return r

def main():
	(p, q) = list(map(int, input().split()))
	b = toContF(p, q)
	n = int(input())
	a = list(map(int, input().split()))
	if len(a) > 1 and a[-1] == 1:
		a[-2] += 1
		a = a[:-1]
	if len(a) != len(b):
		print('NO')
		return 0
	for (i, j) in zip(a, b):
		if i != j:
			print('NO')
			return 0
	print('YES')
main()


(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))

def dfs(i):
	if i == n - 1:
		return (1, a[i])
	x = dfs(i + 1)
	return (x[1], a[i] * x[1] + x[0])
x = dfs(0)
if x[0] * p == x[1] * q:
	print('YES')
else:
	print('NO')


import sys
try:
	file = open('input.txt', 'r')
	sys.stdin = file
	file = open('output.txt', 'w')
	sys.stdout = file
except:
	...
s = input().split()
(p, q) = (int(s[0]), int(s[1]))
n = int(input())
s = input().split()
arr = [int(i) for i in s]
if n == 1:
	if p / q == arr[0]:
		print('YES')
	else:
		print('NO')
else:
	(d1, d2) = (1, arr[len(arr) - 1])
	t = len(arr) - 1
	while t > 1:
		d1 = arr[t - 1] * d2 + d1
		(d1, d2) = (d2, d1)
		t -= 1
	d1 = d2 * arr[0] + d1
	if p * d2 == q * d1:
		print('YES')
	else:
		print('NO')


arr = [0 for i in range(100)]

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def compute(a, b, c):
	ret = [a * c[0] + b * c[1], c[0]]
	return tuple(ret)

def dfs(idx):
	if idx == n - 2:
		return compute(arr[idx], 1, tuple([arr[idx + 1], 1]))
	return compute(arr[idx], 1, dfs(idx + 1))
(p, q) = list(map(int, input().split()))
n = int(input())
arr = list(map(int, input().split()))
if n >= 2:
	ans = dfs(0)
else:
	ans = tuple([arr[0], 1])
d = gcd(ans[0], ans[1])
ans = tuple([ans[0] // d, ans[1] // d])
d = gcd(p, q)
ans2 = tuple([p // d, q // d])
if ans[0] == ans2[0] and ans[1] == ans2[1]:
	print('YES')
else:
	print('NO')


from sys import stdin
(p, q) = map(int, input().split())
n = int(input())
l = list(stdin.read().split())
for i in l:
	if q == 0:
		print('NO')
		exit()
	(p, q) = (q, p - int(i) * q)
print('YES' if q == 0 else 'NO')


(num, den) = [int(x) for x in input().split()]
n = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
for i in range(n[0] - 1):
	num -= den * arr[i]
	(num, den) = (den, num)
if den and num % den == 0:
	num = num // den
	den = 1
if num == arr[n[0] - 1] and den == 1:
	print('YES')
else:
	print('NO')


from fractions import Fraction
(q, p) = list(map(int, input().split()))
is_equal_to = Fraction(q, p)
n = int(input())
a = list(map(int, input().split()))
pos = -1

def continued_fraction(n, a):
	if n == 1:
		return a[0]
	score = a[-1]
	for i in range(n - 2, 0, -1):
		score = a[i] + Fraction(1, score)
	return a[0] + Fraction(1, score)
print('YES' if continued_fraction(n, a) == is_equal_to else 'NO')


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
(p, q) = list(map(int, input().split()))
N = int(input())
A = list(map(int, input().split()))
znam = 1
chisl = A[N - 1]
for i in range(N - 1, 0, -1):
	(chisl, znam) = (znam, chisl)
	chisl += znam * A[i - 1]
if p * znam == q * chisl:
	print('YES')
else:
	print('NO')


(p, q) = map(int, input().split())
height = int(input())
fraction = list(map(int, input().split()))

def maxFraction(firstNum, firstDem, secNum, secDem):
	a = firstNum
	b = firstDem
	c = secNum
	d = secDem
	Y = a * d
	X = b * c
	return X == Y
if height == 1 and fraction[0] == p // q and (p % q == 0):
	print('YES')
elif height == 1 and p % q != 0:
	print('NO')
elif height == 2 and fraction[0] == p // q - 1 and (fraction[1] == 1) and (fraction[0] + fraction[1] == p // q) and (p % q == 0):
	print('YES')
elif fraction[0] != p // q:
	print('NO')
else:
	cur = 1
	dem = fraction[height - 1]
	flag = True
	for i in range(height - 2, 0, -1):
		(cur, dem) = (dem, fraction[i] * dem + cur)
		if dem > q:
			print('NO')
			flag = False
			break
	if maxFraction(p % q, q, cur, dem):
		print('YES')
	elif flag:
		print('NO')


import math
import sys

def gcd(x, y):
	if y == 0:
		return x
	else:
		return gcd(y, x % y)
(p, q) = list(map(int, sys.stdin.readline().split()))
n = int(input())
a = [0] * n
b = list(map(int, sys.stdin.readline().split()))
l = 0
for i in b:
	a[l] = i
	l = l + 1
(x, y) = (a[n - 1], 1)
for i in range(n - 2, -1, -1):
	(x, y) = (y, x)
	x = x + y * a[i]
g1 = gcd(p, q)
g2 = gcd(x, y)
p = p // g1
q = q // g1
x = x // g2
y = y // g2
if x == p and y == q:
	print('YES')
else:
	print('NO')


(p, q) = map(int, input().split())
n = int(input())
arr = list(map(int, input().split()))
(nxt_p, nxt_q) = (arr[-1], 1)
for i in range(n - 2, -1, -1):
	(nxt_p, nxt_q) = (nxt_q + nxt_p * arr[i], nxt_p)
if p * nxt_q == q * nxt_p:
	print('YES')
else:
	print('NO')


def gcd(a, b):
	if b > a:
		q = a
		a = b
		b = q
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def sokrat(p, q):
	d = gcd(p, q)
	return (p / d, q / d)
(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
(g, h) = (a[n - 1], 1)
for i in range(2, n + 1):
	(g, h) = (a[n - i] * g + h, g)
if p * h == g * q:
	print('YES')
else:
	print('NO')


import fractions
import sys

def __starting_point():

	def _(f):
		for l in f:
			for i in l.split():
				yield int(i)
	g = _(sys.stdin)
	pq = fractions.Fraction(next(g), next(g))
	(n, a) = (next(g), [])
	for i in range(n):
		a.append(next(g))
	r = fractions.Fraction(a.pop())
	while a:
		r = fractions.Fraction(a.pop()) + fractions.Fraction(1, r)
	if r == pq:
		print('YES')
	else:
		print('NO')
__starting_point()


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def red(frac):
	gcdfrac = gcd(frac[0], frac[1])
	return (frac[0] // gcdfrac, frac[1] // gcdfrac)

def add(i, frac):
	(a, b) = frac
	a += b * i
	return red((a, b))
import sys
args = list(map(int, sys.stdin.read().split()))
(p, q) = (args[0], args[1])
n = args[2]
s = (args[2 + n], 1)
for i in range(0, n - 1):
	s = add(args[1 + n - i], (s[1], s[0]))
if s == red((p, q)):
	print('YES')
else:
	print('NO')


s = input()
a = s.split()
p = int(a[0])
q = int(a[1])
n = input()
s = input()
se = s.split()
for c in se:
	e = int(c)
	t1 = p - e * q
	t2 = q
	p = t2
	q = t1
if q == 0:
	print('YES')
else:
	print('NO')


from sys import stdin

def read():
	return map(int, stdin.readline().split())

def gcd(a, b):
	return a if b == 0 else gcd(b, a % b)

def simp(a, b):
	g = gcd(a, b)
	return (a // g, b // g)

def calc(a):
	if len(a) == 1:
		return (a[0], 1)
	(y, x) = calc(a[1:])
	return simp(a[0] * y + x, y)
(num, den) = read()
(num, den) = simp(num, den)
read()
print('YES' if calc(list(read())) == (num, den) else 'NO')


from fractions import gcd
(p, q) = map(int, input().split())
gc = gcd(p, q)
p //= gc
q //= gc
n = int(input())
a = list(map(int, input().split()))

def add(a, b):
	res = []
	a[0] *= b[1]
	b[0] *= a[1]
	temp = b[1] * a[1]
	top = a[0] + b[0]
	gc = gcd(top, temp)
	temp //= gc
	top //= gc
	res.append(top)
	res.append(temp)
	return res

def go(i):
	res = []
	if i == n - 1:
		res.append(a[i])
		res.append(1)
		return res
	temp = list(go(i + 1))
	temp = temp[::-1]
	ta = []
	ta.append(a[i])
	ta.append(1)
	temp = add(ta, temp)
	return temp
res = go(0)
if res[0] == p and res[1] == q:
	print('YES')
else:
	print('NO')


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
(p, q) = map(int, input().split())
n = int(input())
data = list(map(int, input().split()))
a = 1
b = data[-1]
for i in range(2, n + 1):
	a = b * data[n - i] + a
	(a, b) = (b, a)
(a, b) = (b, a)
nod = gcd(p, q)
p = p // nod
q = q // nod
if p == a and q == b:
	print('YES')
else:
	print('NO')


(p, q) = map(int, input().split())
n = int(input())
l = list(map(int, input().split()))
for i in l:
	if q == 0 or i > p // q:
		print('NO')
		exit()
	p -= i * q
	(p, q) = (q, p)
print('YES' if q == 0 else 'NO')


(P, Q) = map(int, input().split())
n = int(input())
a = [int(i) for i in input().split()]
p = []
q = []
p.append(a[0])
q.append(1)
if n == 1:
	if p[0] * Q == q[0] * P:
		print('YES')
	else:
		print('NO')
else:
	p.append(a[1] * a[0] + 1)
	q.append(a[1])
	for i in range(2, n):
		p.append(a[i] * p[i - 1] + p[i - 2])
		q.append(a[i] * q[i - 1] + q[i - 2])
	if p[n - 1] * Q == q[n - 1] * P:
		print('YES')
	else:
		print('NO')


import sys
import math
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
(p, q) = invr()
g = math.gcd(p, q)
p //= g
q //= g
n = inp()
ara = inara()
a = ara[-1]
b = 1
for i in range(n - 2, -1, -1):
	(a, b) = (b, a)
	a += b * ara[i]
	g = math.gcd(a, b)
	a //= g
	b //= g
if p == a and q == b:
	print('YES')
else:
	print('NO')


from math import gcd
(q, p) = map(int, input().split())
n = int(input())
a = [*map(int, input().split())]
d = gcd(p, q)
p //= d
q //= d
(m, b) = (1, a[-1])
i = n - 2
while i >= 0:
	m = b * a[i] + m
	d = gcd(m, b)
	m //= d
	b //= d
	i -= 1
	(m, b) = (b, m)
print('YES' if m == p and b == q else 'NO')


from fractions import Fraction

def __starting_point():
	(p, q) = list(map(int, input().split()))
	n = int(input())
	l = list(map(int, input().split()))
	f = Fraction(l[-1], 1)
	for x in l[-2::-1]:
		f = 1 / f
		f += x
	print(['NO', 'YES'][f == Fraction(p, q)])
__starting_point()


from sys import stdin
inFile = stdin
tokens = []
tokens_next = 0

def next_str():
	global tokens, tokens_next
	while tokens_next >= len(tokens):
		tokens = inFile.readline().split()
		tokens_next = 0
	tokens_next += 1
	return tokens[tokens_next - 1]

def nextInt():
	return int(next_str())
from decimal import Decimal
p = nextInt()
q = nextInt()
n = nextInt()
a = [nextInt() for i in range(n)]
while len(a) > 1:
	(p, q) = (q, p - a[0] * q)
	a = a[1:]
print('YES' if p == a[0] * q else 'NO')


from fractions import gcd
(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
g = gcd(p, q)
p = p // g
q = q // g
a = a[::-1]
num = 1
den = a[0]
for i in range(1, n):
	num = den * a[i] + num
	g = gcd(num, den)
	num = num // g
	den = den // g
	(num, den) = (den, num)
if q == num and p == den:
	print('YES')
else:
	print('NO')


(p, q) = [int(x) for x in input().split()]
n = int(input())
a = [int(x) for x in input().split()]
(x, y) = (1, 0)
for i in a[::-1]:
	(x, y) = (y + i * x, x)
if x * q == y * p:
	print('YES')
else:
	print('NO')


from math import gcd
(p, q) = map(int, input().split())
(p, q) = (p // gcd(p, q), q // gcd(p, q))
n = int(input())
(a, b) = (1, 0)
for c in reversed(list(map(int, input().split()))):
	(a, b) = (b, a)
	a += b * c
	(a, b) = (a // gcd(a, b), b // gcd(a, b))
print('YES' if a == p and q == b else 'NO')


(p, q) = map(int, input().split())
n = int(input())
w = [int(x) for x in input().split()]
if n >= 2:
	if p % q != 0:
		r = [0] * n
		i = 0
		while i + 1 <= n:
			if q != 0:
				r[i] = int(p / q)
				(p, q) = (q, p - int(p / q) * q)
				i += 1
			else:
				break
		if r == w or (r[n - 1] == 0 and w[n - 1] == 1 and (w[n - 2] == r[n - 2] - 1)):
			print('YES')
		else:
			print('NO')
	elif w == [int(p / q)] or w == [int(p / q) - 1, 1]:
		print('YES')
	else:
		print('NO')
elif p % q == 0 and w[0] == p / q:
	print('YES')
else:
	print('NO')


s = input().split(' ')
p = int(s[0])
q = int(s[1])
s = input()
n = int(s)
s = input().split(' ')
ok = True
for i in range(n):
	x = int(s[i])
	p = p - q * x
	if ok == False:
		continue
	if p == 0 and i != n - 1:
		ok = False
	elif p < 0:
		ok = False
	t = p
	p = q
	q = t
if ok and (p == 0 or q == 0):
	print('YES')
else:
	print('NO')


def solve(p, q, a, st, n):
	if st == n - 1:
		return p == q * a[st]
	return solve(q, p - a[st] * q, a, st + 1, n)
(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
if solve(p, q, a, 0, n):
	print('YES')
else:
	print('NO')


import fractions
F = fractions.Fraction
I = input
T = int
S = str.split
(p, q) = S(I())
s = F(T(p), T(q))
I()
a = S(I())
(p, a) = (T(a[-1]), a[:-1])
for x in a[::-1]:
	p = F(1, p) + F(T(x), 1)
print('YNEOS'[p != s::2])


def main():
	var_input = input()
	my_list = list(var_input.split(' '))
	p = int(my_list[0])
	q = int(my_list[1])
	n = int(input())
	var_input = input()
	a = list(var_input.split(' '))
	for index in range(len(a)):
		p -= q * int(a[index])
		if index != len(a) - 1 and p == 0:
			print('NO')
			return
		(p, q) = (q, p)
	if q != 0:
		print('NO')
	else:
		print('YES')
main()


(p, q) = map(int, input().split())
n = int(input())
try:
	for v in map(int, input().split()):
		if q and p // q == v:
			(p, q) = (q, p % q)
		elif q and p // q == v + 1:
			(p, q) = (q, p - q * v)
		else:
			raise Exception
	print('NO' if q else 'YES')
except:
	print('NO')


def gcd(x, y):
	if y == 0:
		return x
	else:
		return gcd(y, x % y)
(p, q) = [int(i) for i in input().split()]
n = int(input())
a = [int(i) for i in input().split()]
(x, y) = (1, 0)
for i in a[::-1]:
	(x, y) = (y + i * x, x)
(a, b) = (gcd(p, q), gcd(x, y))
p = p // a
q = q // a
x = x // b
y = y // b
if x == p and y == q:
	print('YES')
else:
	print('NO')


(P, Q) = [int(x) for x in input().split()]
N = int(input())
A = [int(x) for x in input().split()]
(U, V) = (1, 0)
for x in A[::-1]:
	(U, V) = (V, U)
	(U, V) = (V * x + U, V)
if U * Q == P * V:
	print('YES')
else:
	print('NO')


l = input()
p = int(l.split()[0])
q = int(l.split()[1])
n = int(input())
v = [int(x) for x in input().split()]

def calc(f, n):
	if n <= 0:
		return f
	aux = f[1]
	f[1] = f[1] * v[n] + f[0]
	f[0] = aux
	return calc(f, n - 1)
equal = False
if n > 1:
	f = calc([1, v[n - 1]], n - 2)
	f[0] = v[0] * f[1] + f[0]
	equal = f[1] * p - f[0] * q == 0
else:
	equal = v[0] * q - p == 0
if equal:
	print('YES')
else:
	print('NO')


(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
p0 = a[-1]
q0 = 1
for i in range(n - 2, -1, -1):
	(p0, q0) = (q0 + p0 * a[i], p0)
if p * q0 == q * p0:
	print('YES')
else:
	print('NO')


(p, q) = tuple(map(int, input().split()))
n1 = input().split()
n = int(n1[0])
a = list(map(int, input().split()))
x = 1
y = a[n - 1]
for i in range(n - 2, -1, -1):
	x1 = a[i] * y + x
	x = y
	y = x1
if p * x == q * y:
	print('YES')
else:
	print('NO')


import sys
(p, q) = list(map(int, input().split()))
n = int(input())
num = list(map(int, input().split()))
(l1, l2) = ([], [])
i = 0
if q == 1:
	if num == [p] or num == [p - 1, 1]:
		print('YES')
	else:
		print('NO')
else:
	while q > 0:
		l1.append(p // q)
		(p, q) = (q, p % q)
	if len(l1) > 1 and l1[-1] > 1:
		l2 = l1[:]
		l2[-1] -= 1
		l2.append(1)
	if l1 == num or l2 == num:
		print('YES')
	else:
		print('NO')


from fractions import Fraction
pq = Fraction(*map(int, input().split()))
input()
a = list(map(int, input().split()))
a.reverse()
f = Fraction(a[0], 1)
for i in a[1:]:
	f = i + Fraction(1, f)
if pq == f:
	print('YES')
else:
	print('NO')


(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
(x, y) = (a[n - 1], 1)
for i in range(n - 1)[::-1]:
	(x, y) = (a[i] * x + y, x)
print(['NO', 'YES'][p * y == q * x])


x = input('').split(' ')
a = int(x[0])
b = int(x[1])
c = int(input(''))
bad = input('').split(' ')
bad = [int(x) for x in bad]
num = 0
denom = 0
if c == 1:
	if bad[0] * b == a:
		print('YES')
	else:
		print('NO')
elif c == 2:
	e = bad[0]
	f = bad[1]
	if a * f == b * (e * f + 1):
		print('YES')
	else:
		print('NO')
else:
	for g in range(len(bad) - 1, -1, -1):
		if g == len(bad) - 1:
			denom = bad[g]
			num = 1
		elif g == 0:
			num = bad[0] * denom + num
		else:
			newdenom = denom * bad[g] + num
			num = denom
			denom = newdenom
	if num * b == denom * a:
		print('YES')
	else:
		print('NO')


import math
(p, q) = map(int, input().split())
n = int(input())
arr = list(map(int, input().split()))
arr = arr[::-1]
while arr:
	if len(arr) == 1:
		if q * arr[-1] == p:
			print('YES')
			exit(0)
	(p, q) = (q, p - arr[-1] * q)
	g = math.gcd(p, q)
	p //= g
	q //= g
	arr.pop()
print('NO')


(p, q) = list(map(int, input().split()))
n = int(input())
ara = list(map(int, input().split()))
b = 0
a = 1
l = 0
for i in ara[::-1]:
	l = a
	a = i * a + b
	b = l
if a * q == b * p:
	print('YES')
else:
	print('NO')


from sys import stdin, stdout
from fractions import gcd
inputIdx = 0
input = stdin.read().strip().split()

def nextToken():
	global inputIdx, input
	if inputIdx == len(input):
		exit()
	token = input[inputIdx]
	inputIdx += 1
	return token

def simplify(p, q):
	g = gcd(p, q)
	return (p // g, q // g)

def main():
	while True:
		p = int(nextToken())
		q = int(nextToken())
		(p, q) = simplify(p, q)
		n = int(nextToken())
		a = [int(nextToken()) for i in range(n)]
		a = a[::-1]
		num = a[0]
		den = 1
		for i in range(1, n):
			(num, den) = (den, num)
			num = num + den * a[i]
			(num, den) = simplify(num, den)
		if p == num and q == den:
			print('YES')
		else:
			print('NO')
main()


import fractions
F = fractions.Fraction
(p, q) = map(int, input().split())
n = int(input())
lis = list(map(int, input().split()))
if n == 1:
	if F(p, q) == F(lis[0], 1):
		print('YES')
	else:
		print('NO')
else:
	num = 1
	den = lis[-1]
	for i in range(n - 2, -1, -1):
		numt = den * lis[i] + num
		num = den
		den = numt
	if F(q, p) == F(num, den):
		print('YES')
	else:
		print('NO')


def gcd(a, b):
	while b != 0:
		(a, b) = (b, a % b)
	return a

def solve(p, q, a):
	if len(a) == 1:
		return q * a[0] == p
	pp = q
	qq = p - q * a[0]
	aa = a[1:]
	g = gcd(pp, qq)
	pp //= g
	qq //= g
	return solve(pp, qq, aa)
(p, q) = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
if solve(p, q, a):
	print('YES')
else:
	print('NO')