Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.


-----Input-----

The first line contains two integers w, m (2 ≤ w ≤ 10^9, 1 ≤ m ≤ 10^9) — the number defining the masses of the weights and the mass of the item.


-----Output-----

Print word 'YES' if the item can be weighted and 'NO' if it cannot.


-----Examples-----
Input
3 7

Output
YES

Input
100 99

Output
YES

Input
100 50

Output
NO



-----Note-----

Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.

Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.

Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input.

Difficulty: HARD
Tags: Greedy algorithms

Complete search

Number theory

Dynamic programming

Mathematics
Skill types: Dynamic programming

Complete search

Greedy algorithms

Starter code:

Solutions:
(x, y) = list(map(int, input().split(' ')))
ok = []
while y % x in [0, 1, -1 + x]:
	if y % x == 0:
		y //= x
		ok.append(0)
	elif y % x == 1:
		y = (y - 1) // x
		ok.append(1)
	else:
		y = (y + 1) // x
		ok.append(-1)
	if y == 0:
		break
if y == 0:
	print('YES')
else:
	print('NO')


(w, m) = [int(x) for x in input().split()]
for i in range(101):
	if m % w == 0:
		m /= w
	elif (m - 1) % w == 0:
		m = m // w
	elif (m + 1) % w == 0:
		m = (m + 1) / w
	else:
		print('NO')
		exit()
if m == 0:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = 0
d = True
for i in range(100):
	if d and n > 0:
		if m % n ** (a + 1) == n ** a:
			m -= n ** a
			a += 1
		elif m % n ** (a + 1) == 0:
			a += 1
		elif (m + n ** a) % n ** (a + 1) == 0:
			m += n ** a
			a += 1
		else:
			d = False
			print('NO')
if d:
	print('YES')


(w, m) = map(int, input().split())

def b(x, base):
	digits = []
	while x:
		digits.append(x % base)
		x = int(x / base)
	return digits
digs = b(m, w)
f = -1
s = 0
while True:
	for i in range(s, len(digs)):
		if digs[i] not in {0, 1}:
			if digs[i] != w - 1:
				print('NO')
				exit()
			f = i
			break
	if f != -1 and f != len(digs) - 1:
		m += w ** f
		digs = b(m, w)
		s = f
		f = -1
	else:
		break
print('YES')


(w, m) = map(int, input().split())
arr = [0] * 103
i = 0
while m != 0:
	arr[i] = m % w
	m //= w
	i += 1
for j in range(i):
	if arr[j] == 1 or arr[j] == 0:
		pass
	elif arr[j] % w == 0:
		arr[j + 1] += 1
	elif arr[j] == w - 1:
		arr[j + 1] += 1
	else:
		print('NO')
		break
else:
	print('YES')


(w, x) = map(int, input().split())
lst = []
for i in range(0, 30):
	if pow(w, i) < x * 2:
		lst.append(pow(w, i))
n = len(lst)
lst.sort(reverse=True)
ans = 100000

def rec(a, x, i):
	global ans
	if a == x:
		ans = 1
		return 1
	if i == n:
		return
	rec(a, x, i + 1)
	rec(a - lst[i], x, i + 1)
	rec(a + lst[i], x, i + 1)
if w == 2 or w == 3:
	print('YES')
else:
	rec(0, x, 0)
	if ans == 100000:
		print('NO')
	else:
		print('YES')


def can_be_weigthed(m, w, i):
	if i == 0:
		return m == 1
	else:
		if m == 1:
			return True
		if m % w in {0, 1, w - 1}:
			return can_be_weigthed(m // w + (m % w == w - 1), w, i - 1)
		else:
			return False
(w, m) = [int(x) for x in input().split()]
answer = {True: 'YES', False: 'NO'}
print(answer[can_be_weigthed(m, w, 100)])


(w, m) = map(int, input().split())
p = 1
for i in range(100):
	if m == 0:
		break
	if (m - p) % (p * w) == 0:
		m -= p
	elif (m + p) % (p * w) == 0:
		m += p
	p *= w
if m == 0:
	print('YES')
else:
	print('NO')


from math import *
(w, m) = map(int, input().split())
arr = []
p = 1
for i in range(101):
	arr.append(p)
	p *= w
arr.reverse()
for i in range(101):
	val = abs(m - arr[i])
	m = min(m, val)
print('NO' if m else 'YES')


(n, m) = [int(x) for x in input().split()]
balans = m
if n == 2 or m == 1:
	print('YES')
	return
cur = 1
while True:
	cur2 = cur * n
	if balans % cur2 == cur:
		balans -= cur
	elif balans % cur2 == cur2 - cur:
		balans += cur
	elif balans % cur2 == 0:
		balans += 0
	else:
		print('NO')
		return
	if balans == 0:
		print('YES')
		return
	cur = cur2


(w, m) = map(int, input().split())
mass = []
while m > 0:
	mass.append(m % w)
	m //= w
mass.append(0)
for i in range(len(mass) - 1):
	if mass[i] >= w - 1:
		mass[i + 1] += 1
		mass[i] = 0
ok = 1
for i in range(len(mass)):
	if mass[i] > 1:
		ok = 0
print('NO' if ok == 0 and w != 2 else 'YES')


(base, num) = map(int, input().split())
if base == 3 or base == 2:
	print('YES')
	quit()
newNum = []
while num > 0:
	newNum = [num % base] + newNum
	num //= base
newNum = [0] + newNum
for i in range(len(newNum) - 1, 0, -1):
	if newNum[i] == base or newNum[i] == base - 1:
		newNum[i - 1] += 1
		continue
	if newNum[i] > 1:
		print('NO')
		quit()
print('YES')


def solucion():
	(base, peso) = map(int, input().split())
	auxiliar = peso
	SoloPongoPesoEnUnPlato = 'True'
	data = []
	PesoAgregadoAPlatoDelArticulo = []
	while auxiliar != 0:
		resto = auxiliar % base
		auxiliar = int(auxiliar / base)
		data.append(resto)
		if resto > 1:
			SoloPongoPesoEnUnPlato = 'False'
	if SoloPongoPesoEnUnPlato == 'True':
		print('YES')
	else:
		potencia = 0
		carry = 0
		for x in data:
			if carry == 1:
				x = x + 1
				carry = 0
			if x > 1:
				if x == base:
					x = 0
					data[potencia] = x
					PesoAgregadoAPlatoDelArticulo.append(0)
					carry = 1
				elif int(x) != base - 1:
					return print('NO')
				else:
					data[potencia] = 0
					PesoAgregadoAPlatoDelArticulo.append(1)
					carry = 1
			else:
				data[potencia] = x
				PesoAgregadoAPlatoDelArticulo.append(0)
			potencia = potencia + 1
			if carry == 1 and potencia == len(data):
				data.append(0)
		indice = 0
		while indice < len(data) - 1:
			if data[indice] == PesoAgregadoAPlatoDelArticulo[indice] and data[indice] == 1:
				return print('NO')
			indice = indice + 1
		print('YES')
solucion()


(w, m) = list(map(int, input().split()))
for u in (w ** (100 - i) for i in range(101)):
	m = min(m, abs(m - u))
print('YES' if m == 0 else '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())
(w, m) = invr()
bits = [0] * 40
bit = 0
while m:
	bits[bit] = m % w
	bit += 1
	m //= w
for i in range(bit):
	if bits[i] != 1 and bits[i] != 0 and (bits[i] != w) and (bits[i] != w - 1):
		print('NO')
		exit(0)
	elif bits[i] == w or bits[i] == w - 1:
		bits[i + 1] += 1
print('YES')


(w, m) = [int(i) for i in input().split()]
if w == 0:
	if m <= 101:
		print('YES')
	else:
		print('NO')
	exit(0)
a = []
m1 = m
while m1 > 0:
	a.append(m1 % w)
	m1 //= w
if len(a) > 101:
	print('NO')
	exit(0)
s = True
for i in range(len(a)):
	if a[i] != 0 and a[i] != 1:
		s = False
if s:
	print('YES')
	exit(0)
s = True
for i in range(len(a) - 1):
	if a[i] != 0 and a[i] != 1:
		a[i] -= w
		a[i + 1] += 1
		if a[i] != -1 and a[i] != 0:
			print('NO')
			exit(0)
if a[-1] > 1:
	if len(a) > 100:
		print('NO')
		exit(0)
	a[-1] -= w
	if a[-1] != 0 and a[-1] != -1:
		print('NO')
		exit(0)
print('YES')


(w, m) = map(int, input().split())

def isOK(w, m):
	wr = convert_base_w(m, w)
	for i in range(101):
		if wr[i] == 0 or wr[i] == 1:
			continue
		elif wr[i] == w - 1 or wr[i] == w:
			wr[i + 1] += 1
		else:
			return False
	return True

def convert_base_w(m, w):
	wr = [0] * 101
	for i in range(101):
		(m, r) = divmod(m, w)
		wr[i] = r
	return wr
if isOK(w, m):
	print('YES')
else:
	print('NO')


import os, sys
from io import BytesIO, IOBase

def main():
	(w, m) = map(int, input().split())
	if w == 2:
		print('YES')
		exit()
	weights = [1]
	while weights[-1] < 10 ** 9:
		weights.append(weights[-1] * w)
	se = set()
	ls = []
	for i in weights:
		for j in range(len(ls)):
			x = i + ls[j]
			if x not in se:
				se.add(x)
				ls.append(x)
		if i not in se:
			se.add(i)
			ls.append(i)
	for i in ls:
		if i - m in se or i == m:
			print('YES')
			break
	else:
		print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


(w, m) = tuple(map(int, input().split()))
f = True
while m > 0:
	if m % w == 0:
		m //= w
	elif m % w == 1:
		m //= w
	elif m % w == w - 1:
		m = m // w + 1
	else:
		f = False
		break
print('YES' if f else 'NO')


(w, m) = map(int, input().split())
o = []
while m > 0:
	o.append(m % w)
	m //= w
o.append(0)
f = 0
for i in range(len(o) - 1):
	o[i + 1] += o[i] // w
	o[i] %= w
	if o[i] == 0:
		continue
	if o[i] == 1:
		continue
	elif o[i] < w - 1:
		f = 1
	else:
		o[i + 1] += 1
print('YES' if f == 0 else 'NO')


(w, m) = map(int, input().split())
while m > 1:
	c = m % w
	if c not in {0, 1, w - 1}:
		print('NO')
		exit()
	m //= w
	if c == w - 1:
		m += 1
print('YES')


M = 10
l = set()
r = set()

def genl(i, s):
	if i == M:
		l.add(s)
		return
	genl(i + 1, s - a[i])
	genl(i + 1, s)
	genl(i + 1, s + a[i])

def genr(i, s):
	if i == M:
		r.add(s)
		return
	genr(i + 1, s - b[i])
	genr(i + 1, s)
	genr(i + 1, s + b[i])
(w, n) = map(int, input().split())
if w == 2:
	print('YES')
	exit(0)
a = []
for i in range(M):
	a.append(w ** i)
b = []
for i in range(M):
	b.append(w ** (M + i))
genl(0, 0)
genr(0, 0)
for e in l:
	if n - e in r:
		print('YES')
		exit(0)
print('NO')


from heapq import heapify, heappush, heappop
from collections import Counter, defaultdict, deque, OrderedDict
from sys import setrecursionlimit, maxsize
from bisect import bisect_left, bisect, insort_left, insort
from math import ceil, log, factorial, hypot, pi
from fractions import gcd
from copy import deepcopy
from functools import reduce
from operator import mul
from itertools import product, permutations, combinations, accumulate, cycle
from string import ascii_uppercase, ascii_lowercase, ascii_letters, digits, hexdigits, octdigits
prod = lambda l: reduce(mul, l)
prodmod = lambda l, mod: reduce(lambda x, y: mul(x, y) % mod, l)

def read_list(t):
	return [t(x) for x in input().split()]

def read_line(t):
	return t(input())

def read_lines(t, N):
	return [t(input()) for _ in range(N)]

def convert_base(num, base):
	converted = []
	while num:
		(num, r) = divmod(num, base)
		converted.append(r)
	return converted

def solve(w, m):
	w_m = convert_base(m, w)
	w_m += [0] * (101 - len(w_m))
	for i in range(101):
		if w_m[i] == 0 or w_m[i] == 1:
			continue
		elif w_m[i] == w or w_m[i] == w - 1:
			w_m[i + 1] += 1
		else:
			return False
	return True
(w, m) = read_list(int)
print('YES' if solve(w, m) else 'NO')


(w, m) = [int(i) for i in input().split()]
k = 0
while k <= 100:
	if m == 0:
		print('YES')
		exit(0)
	d = m % w
	if d == 0:
		m = m / w
		k += 1
	elif d == 1:
		k += 1
		m = (m - 1) / w
	elif d == w - 1:
		m = (m + 1) / w
		k += 1
	else:
		print('NO')
		exit(0)
print('NO')


(w, m) = map(int, input().split())
for i in range(33):
	m = min(m, abs(w ** (32 - i) - m))
print('NO' if m else 'YES')


(w, m) = map(int, input().split())
base = [0] * 101
base[0] = m
for i in range(len(base)):
	if base[i] < w:
		break
	base[i + 1] = base[i] // w
	base[i] = base[i] % w
found = True
for i in range(len(base)):
	if base[i] > 1:
		if -1 <= base[i] - w <= 1:
			base[i] -= w
			base[i + 1] += 1
		else:
			found = False
			break
if found:
	print('YES')
else:
	print('NO')


(M, N) = map(int, input().split())
used = {}

def check(K):
	while K:
		KP = 0
		while K:
			if K % M == 0:
				K /= M
			elif K % M == 1 and KP not in used:
				K = (K - 1) / M
			else:
				return False
			KP += 1
	return True
while N:
	if check(N):
		break
	P = 0
	while M ** P < N:
		P += 1
	high = M ** P
	low = M ** (P - 1) if P else 0
	if P in used and P - 1 in used:
		print('NO')
		exit()
	elif P in used:
		used[P - 1] = True
		N = N - low
	elif P - 1 in used:
		used[P] = True
		N = high - N
	elif high - N < N:
		used[P] = True
		N = high - N
	else:
		used[P - 1] = True
		N = N - low
print('YES')


(a, b) = map(int, input().split())
l = []
x = 1
c = 0
while b != 0:
	c = b % a
	b = b // a
	l.append(c)
l.append(0)
l.append(0)
l.append(0)
br = 0
t = 0
for x in l:
	if x != 0 and x != 1:
		if x == a - 1:
			l[br] = -1
			l[br + 1] += 1
		elif x == a:
			l[br] = 0
			l[br + 1] += 1
		else:
			t += 1
			break
	br += 1
if t == 0:
	print('YES')
else:
	print('NO')


(w, m) = map(int, input().split())

def solve(m, curw):
	cursum = 0
	if m == 0:
		return 1
	for i in range(curw):
		cursum += w ** i
		if cursum == m:
			return 1
		if cursum > m or w ** i == m:
			return solve(abs(w ** i - m), i)
	return 0
print('YES' if solve(m, 100) else 'NO')


(base, massa) = map(int, input().split())
for i in range(100, -1, -1):
	if abs(massa - base ** i) < massa:
		massa = abs(massa - base ** i)
if massa == 0:
	print('YES')
else:
	print('NO')


import math
(w, m) = map(int, input().split())
if w == 2:
	print('YES')
else:
	n = math.ceil(math.log(1000000000.0, w))
	for mask in range(1 << n):
		s = m
		p = 1
		for i in range(n):
			if mask & 1 << i:
				s += p
			p *= w
		while s > 0:
			if s % w > 1:
				break
			s //= w
		else:
			print('YES')
			break
	else:
		print('NO')


x = [0] * 101
(ww, m) = map(int, input().split())
if ww == 2:
	print('YES')
	exit()
w = ww
c = 0
while m:
	x[c] = m % w
	m //= w
	c += 1
an = 1
w = ww
vis = [0] * 101
for i in range(101):
	if x[i] == w - 1:
		vis[i] = 1
		x[i] = 0
		if i + 1 > 100:
			an = 0
			break
		else:
			x[i + 1] += 1
	elif x[i] == w:
		x[i] = 0
		x[i + 1] += 1
for i in range(101):
	if x[i] > 1:
		an = 0
		break
	elif x[i] == 1 and vis[i] != 0:
		an = 0
		break
print('YES' if an else 'NO')


import math
(w, m) = map(int, input().split())
while m > 0:
	if m % w == 1:
		m -= 1
		m /= w
	elif m % w == w - 1:
		m += 1
		m /= w
	elif m % w == 0:
		m /= w
	else:
		print('NO')
		quit()
print('YES')


(a, b) = list(map(int, input().split(' ')))
if a == 2 or a == 3:
	print('YES')
	return
else:
	while b != 0:
		if b % a == 0:
			b //= a
		elif b % a == 1:
			b -= 1
			b //= a
		elif b % a == a - 1:
			b += 1
			b //= a
		else:
			print('NO')
			return
print('YES')


(w, m) = map(int, str.split(input()))
bits = [0] * 100
for i in range(100):
	(m, bits[i]) = divmod(m, w)
for i in range(99):
	if 1 < bits[i] < w - 1:
		print('NO')
		exit()
	if bits[i] == w - 1:
		bits[i] = 0
		bits[i + 1] += 1
		carry = 0
		for j in range(i + 1, 100):
			(carry, bits[j]) = divmod(bits[j] + carry, w)
print('YES')


(w, m) = map(int, input().split())
s = 0
p = 1
for i in range(101):
	s += p
	p *= w
m += s
bad = False
while m > 0:
	if m % w > 2:
		print('NO')
		exit()
	m //= w
print('YES')


(w, m) = map(int, input().split(' '))
f = 0
if w == 2:
	print('YES')
else:
	st = 1
	while f == 0:
		if m % (st * w) != 0:
			if (m - st) % (st * w) == 0:
				m -= st
			elif (m + st) % (st * w) == 0:
				m += st
			else:
				print('NO')
				f = 1
			if m == 0:
				print('YES')
				f = 1
		st *= w


(w, m) = [int(x) for x in input().split()]
a = [w ** i for i in range(101)]
total = sum(a) + m
for i in reversed(a):
	cnt = 0
	while total >= i and cnt < 2:
		cnt += 1
		total -= i
if total == 0:
	print('YES')
else:
	print('NO')


(x, y) = map(int, input().split())
(x, y) = (y, x)
s = []
while x > 0:
	d = x % y
	s.append(d)
	x //= y
s.append(x)
carry = 0
for n in range(len(s)):
	s[n] += carry
	if s[n] == y:
		carry = 1
		continue
	if 1 < s[n] < y - 1:
		print('NO')
		exit(0)
	carry = 0
	if s[n] == y - 1:
		carry = 1
print('YES')


__author__ = 'artbataev'

def tryWeight(w, m, num):
	if num < 1:
		return False
	if m == 1:
		return True
	if m % w == 0:
		return tryWeight(w, m // w, num - 1)
	if (m + 1) % w == 0:
		return tryWeight(w, (m + 1) // w, num - 1)
	if (m - 1) % w == 0:
		return tryWeight(w, (m - 1) // w, num - 1)
	return False
(w, m) = list(map(int, input().split()))
if tryWeight(w, m, 101):
	print('YES')
else:
	print('NO')


R = lambda : list(map(int, input().split()))
(w, m) = R()
while m:
	k = m % w
	if k == 1:
		m -= 1
	elif k == w - 1:
		m += 1
	elif k == 0:
		pass
	else:
		print('NO')
		exit(0)
	m //= w
print('YES')


(w, m) = map(int, input().split())
A = []
while m:
	A.append(m % w)
	m //= w
sz = len(A)
A.append(0)
for i in range(sz):
	x = A[i]
	if x != 0 and x != 1 and (x != w - 1) and (x != w):
		print('NO')
		exit()
	if x == w - 1 or x == w:
		A[i + 1] += 1
print('YES')


__author__ = 'victormion'
(w, m) = input().strip().split()
w = int(w)
m = int(m)

def solve(w, m):
	return w <= 3 or m == 1 or trySolve(w, m - 1) or trySolve(w, m) or trySolve(w, m + 1)

def trySolve(w, m):
	return m % w == 0 and solve(w, m // w)
if solve(w, m):
	print('YES')
else:
	print('NO')


import sys

def seek(x, terms, sums, index):
	if x == 0 or sums[index] == x:
		print('YES')
		sys.exit()
	if sums[index] < abs(x):
		return
	if x > 0:
		seek(x - terms[index], terms, sums, index - 1)
	else:
		seek(x + terms[index], terms, sums, index - 1)
	seek(x, terms, sums, index - 1)
(base, x) = map(int, input().split())
terms = [1]
sums = [1]
while terms[-1] < x:
	terms.append(base * terms[-1])
	sums.append(sums[-1] + terms[-1])
seek(x, terms, sums, len(terms) - 1)
print('NO')


def powmod(x, p, m):
	if p <= 0:
		return 1
	if p <= 1:
		return x % m
	return powmod(x * x % m, p // 2, m) * (x % m) ** (p % 2) % m

def to_basex(num, x):
	while num > 0:
		yield (num % x)
		num //= x

def from_basex(it, x):
	ret = 0
	p = 1
	for d in it:
		ret += d * p
		p *= x
	return ret
(w, m) = (int(x) for x in input().split())
mbx = list(to_basex(m, w))
for i in range(len(mbx)):
	mbx[i] += 1
m = from_basex(mbx, w)
mbx = list(to_basex(m, w))
ans = 'YES' if max(mbx) < 3 else 'NO'
print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)

def ctd(chr):
	return ord(chr) - ord('a')
mod = 998244353
INF = float('inf')

def main():
	(w, m) = RL()
	if w <= 3:
		print('YES')
	else:
		while m:
			now = m % w
			if now not in [1, 0, w - 1]:
				print('NO')
				break
			elif now == w - 1:
				m = m // w + 1
			else:
				m = m // w
		else:
			print('YES')
main()


def main():
	(w, m) = [int(i) for i in input().split()]
	if w == 2:
		print('YES')
		return
	s = 0
	for i in range(21):
		for sign in range(-1, 2):
			if (m - s - sign * w ** i) % w ** (i + 1) == 0:
				s += sign * w ** i
				break
	if abs(s) == m:
		print('YES')
	else:
		print('NO')
main()


(w, m) = map(int, input().split())
a = []
while m:
	a.append(m % w)
	m //= w
a.append(0)
for (i, ai) in enumerate(a):
	if ai <= 1:
		pass
	elif ai >= w - 1:
		a[i + 1] += 1
	else:
		print('NO')
		exit()
print('YES')


(n, m) = map(int, input().split())
for k in range(33):
	m = min(m, abs(n ** (32 - k) - m))
print(['NO', 'YES'][m == 0])


def number_tree(sum_weights, i, m):
	if sum_weights == m:
		return True
	if i == -1:
		return False
	result = number_tree(sum_weights + weights[i], i - 1, m)
	if result:
		return result
	result = number_tree(sum_weights, i - 1, m)
	if result:
		return result
	if sum_weights != 0:
		result = number_tree(sum_weights - weights[i], i - 1, m)
		if result:
			return result
	return False
line = input()
(w, m) = map(int, line.split(' '))
if w == 2 or w == 3:
	print('YES')
else:
	weights = [1]
	for power in range(99):
		if weights[power] > m:
			break
		weights.append(weights[power] * w)
	ret = number_tree(0, power, m)
	if ret:
		print('YES')
	else:
		print('NO')


def to_w(w1, m1):
	t = 100
	while w ** t > m:
		t -= 1
	at = [0] * (t + 1)
	for i in range(t, -1, -1):
		at[i] = m1 // w1 ** i
		m1 = m1 % w1 ** i
	return at
(w, m) = list(map(int, input().split()))
n = to_w(w, m)
for i in range(len(n)):
	if n[i] != 0 and n[i] != 1 and (n[i] != w) and (n[i] != w - 1):
		print('NO')
		exit()
	if n[i] == w - 1:
		if i != len(n) - 1:
			n[i + 1] = n[i + 1] + 1
			n[i] = 0
		else:
			n.append(1)
	if n[i] == w:
		if i != len(n) - 1:
			n[i] = 0
			n[i + 1] += 1
		else:
			n.append(1)
print('YES')


def can_weigh(w, m):
	while m > 0:
		if (m - 1) % w == 0:
			m -= 1
		elif (m + 1) % w == 0:
			m += 1
		elif m % w != 0:
			return 'NO'
		m //= w
	return 'YES'
(w, m) = map(int, input().split())
result = can_weigh(w, m)
print(result)


(w, n) = map(int, input().split(' '))
r = 1
if w <= 3:
	n = 0
while n:
	if not n % w in {0, 1, w - 1}:
		r = 0
	if n % w == w - 1:
		n = n // w + 1
	else:
		n = n // w
if r:
	print('YES')
else:
	print('NO')


def f(m, t):
	w = 0
	nonlocal n
	while n ** w * n < m:
		w += 1
	if n ** w * n == m or (m == 1 and t != 0):
		return True
	else:
		s1 = m - n ** w
		j = False
		k = False
		if w < t:
			j = f(abs(s1), w)
		if w + 1 < t:
			k = f(abs(n ** w * n - m), w + 1)
		if k == True or j == True:
			return True
		else:
			return False
(n, m) = list(map(int, input().split()))
if f(m, 101):
	print('YES')
else:
	print('NO')


def base(n, b):
	global st
	if n < b:
		st.append(n)
		return
	base(n // b, b)
	st.append(n % b)
	return
st = []

def proC(m, w):
	global st
	st = []
	base(m, w)
	s = 0
	c = 0
	arr = st[::-1]
	for i in range(len(arr)):
		s = (int(arr[i]) + c) % w
		c = (int(arr[i]) + c) // w
		if s == 0 or s == 1:
			continue
		if s == w - 1:
			c = 1
		else:
			print('NO')
			return
	print('YES')
(w, m) = list(map(int, input().split()))
proC(m, w)


def solve(num, p):
	return True if num == 0 else False if num >= p * 2 else solve(min(num, abs(p - num)), p // m)
(m, num) = map(int, input().split())
p = 1
while p < num:
	p *= m
print('YES' if solve(num, p) else 'NO')


def main():
	(w, m) = map(int, input().split())
	while m > 1:
		c = m % w
		if c not in {0, 1, w - 1}:
			return print('NO')
		m //= w
		if c == w - 1:
			m += 1
	print('YES')
main()


import sys
from math import log2, floor, ceil, sqrt, gcd
import bisect
sys.setrecursionlimit(10 ** 5)
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 1000000007
(w, m) = Ri()
arr = []
while m > 0:
	rem = m % w
	arr.append(rem)
	m = m // w
rem = 0
flag = True
for i in range(len(arr)):
	arr[i] += rem
	if arr[i] == w:
		rem = 1
		continue
	else:
		rem = 0
	if arr[i] <= 1:
		continue
	if arr[i] == w - 1:
		rem = 1
	else:
		flag = False
		break
if rem == 1:
	arr.append(1)
if not flag:
	NO()
elif len(arr) > 101:
	NO()
else:
	YES()


from sys import stdout
from sys import stdin

def get():
	return stdin.readline().strip()

def getf():
	return [int(i) for i in get().split()]

def put(a, end='\n'):
	stdout.write(str(a) + end)

def putf(a, sep=' ', end='\n'):
	stdout.write(sep.join([str(i) for i in a]) + end)

def putff(a, sep=' ', end='\n'):
	[putf(i, sep, end) for i in a]

def transf(a, w):
	ans = []
	while a > 0:
		ans.append(a % w)
		a //= w
	return ans[::-1]

def solve(a, w):
	ans = []
	p = 0
	for i in range(len(a) - 1, 0, -1):
		if a[i] + p >= w:
			a[i] = (a[i] + p) % w
			p = 1
		else:
			a[i] += p
			p = 0
		if a[i] in [0, 1]:
			ans.append(0)
			pass
		elif a[i] == w - 1:
			p = 1
			ans.append(1)
		else:
			return 'NO'
	ans.reverse()
	for i in ans:
		if i not in [0, 1]:
			return 'NO'
	return 'YES'

def main():
	(w, m) = getf()
	print(solve([0] + transf(m, w), w))
main()


(w, m) = map(int, input().split())
f = 0
ff = 0
while m != 0:
	i = 0
	ma = 0
	while w ** i < m:
		ma += w ** i
		i += 1
	if w ** i - ma <= m:
		ff = 1
		m = w ** i - m
	elif ma >= m:
		ff = 2
		m = m - w ** (i - 1)
	else:
		f = 1
		print('NO')
		break
if not f:
	print('YES')


(w, m) = map(int, input().split())
a = [0] * 60
c = 0
while m > 0:
	a[c] = m % w
	c += 1
	m //= w
for i in range(59):
	if a[i] not in [0, 1, w - 1, w]:
		print('NO')
		break
	elif a[i] in [w - 1, w]:
		a[i + 1] += 1
else:
	print('YES')


import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

def binval(m, w):
	b = []
	while m > 0:
		b.append(m % w)
		m //= w
	return b

def solve(W, M):
	b = binval(M, W)
	if all([v <= 1 for v in b]):
		return True
	b += [0]
	for i in range(len(b) - 1):
		v = b[i]
		if v > 1:
			v -= W
			if v != 0 and v != -1:
				return False
			b[i + 1] += 1
	return True
(W, M) = map(int, input().split())
s = solve(W, M)
print('YES' if s else 'NO')


(w, m) = map(int, input().split(' '))
an = []
while m != 0:
	mod = m % w
	m //= w
	an.append(mod)
an += [0] * 100
for _ in range(100):
	for i in range(100):
		an[i + 1] += an[i] // w
		an[i] %= w
		if an[i] == w - 1:
			an[i] = -1
			an[i + 1] += 1
if all(map(lambda x: x == 0 or x == 1 or x == -1, an)):
	print('YES')
else:
	print('NO')


__author__ = 'Andrey'

def solve(m, w, max_power=100):
	r = m % w
	if r not in {w - 1, 0, 1}:
		return False
	if m < w:
		if max_power < 1:
			return False
		else:
			return True
	if r % w == 0:
		new_m = m
	elif (r - 1) % w == 0:
		new_m = m - 1
	else:
		new_m = m + 1
	return solve(new_m // w, w, max_power - 1)
(w, m) = map(int, input().split())
print('YES' if solve(m, w) else 'NO')


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

def Y():
	print('YES')

def run(w, m):
	if w <= 3:
		Y()
		return
	mm = m
	for i in range(100):
		if m % w == 0:
			m //= w
		elif m % w == 1:
			m = (m - 1) // w
		elif m % w == w - 1:
			m = (m + 1) // w
		else:
			break
		if m == 0:
			Y()
			return
	print('NO')

def main(info=0):
	(w, m) = VI()
	run(w, m)

def __starting_point():
	main()
__starting_point()


(w, m) = map(int, input().split())
bb = True
while m > 0 and bb:
	x = m % w
	if x == 1:
		m -= 1
	elif x == w - 1:
		m += 1
	elif x != 0:
		bb = False
	m //= w
if bb:
	print('YES')
else:
	print('NO')


numbers = list(map(int, input().split()))
w = numbers[0]
m = numbers[1]
no = False
if w <= 3:
	m = 0
while m:
	x = m % w
	if x <= 1:
		m = m // w
	elif x == w - 1:
		m = m // w + 1
	else:
		no = True
		break
if no is False:
	print('YES')
else:
	print('NO')


def f(w, m):
	if m == 0:
		return True
	if m % w == 0:
		return f(w, m // w)
	if (m - 1) % w == 0:
		return f(w, (m - 1) // w)
	if (m + 1) % w == 0:
		return f(w, (m + 1) // w)
	return False
(w, m) = map(int, input().split())
if f(w, m):
	print('YES')
else:
	print('NO')


def convert(base, n):
	l = [0 for i in range(100)]
	c = 99
	while n > 0:
		a = n // base
		b = n % base
		l[c] = b
		c -= 1
		n = a
	return l

def arr_eq(a, b):
	for i in range(99, 0, -1):
		if a[i] != b[i]:
			return False
	return True

def inc_arr(arr, ptr, base):
	arr[ptr] = 0
	while ptr > 0:
		if arr[ptr - 1] == base - 1:
			arr[ptr - 1] = 0
		else:
			arr[ptr - 1] += 1
			return arr
		ptr -= 1
	return arr

def weight(sc1, sc2, base):
	ptr = 99
	while not arr_eq(sc1, sc2) and ptr > -1:
		if sc1[ptr] == 1:
			sc2[ptr] = 1
		elif sc1[ptr] == base - 1:
			sc1 = inc_arr(sc1, ptr, base)
		elif sc1[ptr] == 0:
			pass
		else:
			return False
		ptr -= 1
	return arr_eq(sc1, sc2)
inp = input().split()
base = int(inp[0])
n = int(inp[1])
l = convert(base, n)
(sc1, sc2) = (l, [0 for i in range(100)])
i = weight(sc1, sc2, base)
if i:
	print('YES')
else:
	print('NO')


def rec(remain, prev_index):
	if remain == 0:
		return True
	if prev_index == 0:
		return False
	index = -1
	for i in range(prev_index):
		if remain > 0 and w ** (i + 1) - remain >= 0:
			index = i
			break
		elif remain < 0 and w ** (i + 1) + remain >= 0:
			index = i
			break
	if index == -1:
		return False
	if remain > 0:
		w1 = w ** index
		w2 = w1 * w
	else:
		w1 = -w ** index
		w2 = w1 * w
	if index + 1 == prev_index:
		return rec(remain - w1, index)
	return rec(remain - w1, index) or rec(remain - w2, index + 1)
(w, m) = [int(a) for a in input().split()]
if rec(m, 101):
	print('YES')
else:
	print('NO')


R = lambda : map(int, input().split())
(w, m) = R()
bs = [1]
while bs[-1] < 10 ** 10:
	bs.append(bs[-1] * w)
arr = [0] * len(bs)
for i in range(len(bs) - 1, -1, -1):
	if m >= bs[i]:
		arr[i] = m // bs[i]
		m %= bs[i]
for i in range(len(bs)):
	if 1 < arr[i] < w - 1:
		print('NO')
		exit(0)
	if arr[i] > 1 and (arr[i] == w - 1 or arr[i] == w):
		arr[i + 1] += 1
print('YES')


(n, m) = map(int, input().split())
flag = 0
while m > 0:
	if (m - 1) % n == 0:
		m -= 1
	elif (m + 1) % n == 0:
		m += 1
	if m % n != 0:
		flag = -1
		break
	m //= n
if flag == 0:
	print('YES')
else:
	print('NO')


def main():
	(w, m) = map(int, input().split())
	ww = 1
	while ww < m:
		ww *= w
	while m and ww:
		m = min(m, m - ww, m + ww, key=abs)
		ww //= w
	print(('YES', 'NO')[bool(m)])
main()


(w, m) = map(int, input().split())

def f(m, w):
	while m != 0:
		x = (m + 1) % w
		if x == 0:
			m = (m + 1) // w
		elif x < 3:
			m = m // w
		else:
			return False
	return True
if w == 2:
	print('YES')
elif f(m, w):
	print('YES')
else:
	print('NO')


(w, m) = map(int, input().split())
for i in range(100, -1, -1):
	if abs(m - w ** i) < m:
		m = abs(m - w ** i)
print('YES' if m == 0 else 'NO')


(w, m) = list(map(int, input().split()))

def bt(w, p, final, cur):
	if final == cur:
		return True
	if p == 0:
		return False
	if abs(cur + p - final) <= (p - 1) / (w - 1):
		if bt(w, p // w, final, cur + p):
			return True
	if abs(cur - p - final) <= (p - 1) / (w - 1):
		if bt(w, p // w, final, cur - p):
			return True
	if bt(w, p // w, final, cur):
		return True
	return False
if bt(w, w ** 35, m, 0):
	print('YES')
else:
	print('NO')


import os
import sys
from io import BytesIO, IOBase

def main():
	(w, m) = map(int, input().split())
	if w <= 3:
		print('YES')
		exit()
	arr = [1, w]
	for _ in range(2, 101):
		arr.append(arr[-1] * w)
		if arr[-1] > m << 1:
			arr.pop()
			break
	n = len(arr)

	def solve(i, right):
		if m == right:
			return 1
		if i == n:
			return 0
		ans = solve(i + 1, right + arr[i])
		if ans:
			return ans
		ans = solve(i + 1, right - arr[i])
		if ans:
			return ans
		ans = solve(i + 1, right)
		return ans
	if solve(0, 0):
		print('YES')
	else:
		print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import unittest
import sys
import re

class Scales:
	N = 100

	def __init__(self, args):
		self.args = args
		self.w = args[0]
		self.m = args[1]
		self.maxwp = self.calc_maxwp()
		self.it_min = 0
		self.it_max = int(3 ** (self.maxwp + 1)) - 1
		self.yes = 0

	def calc_maxwp(self):
		for p in range(self.N + 1):
			if self.w ** p > self.m:
				return p

	def list2dec(self, it):
		result = 0
		for (n, i) in enumerate(it):
			result += i * int(3 ** n)
		return result

	def dec2list(self, dec):
		result = []
		remainder = dec
		for n in range(self.maxwp + 1):
			pow = int(3 ** (self.maxwp - n))
			div = remainder // pow
			remainder -= div * pow
			result.insert(0, div)
		return result

	def step(self):
		mid = (self.it_max + self.it_min) // 2
		if mid in (self.it_max, self.it_min):
			return 0
		w = self.calc_weight(mid)
		if w > self.m:
			self.it_max = mid
		elif w < self.m:
			self.it_min = mid
		else:
			self.yes = 1
			return 0
		return 1

	def calc_weight(self, dec):
		result = 0
		it = self.dec2list(dec)
		for i in range(len(it)):
			s = it[i]
			w = self.w ** i
			if s == 2:
				result += w
			if s == 0:
				result -= w
		return result

	def calculate(self):
		while self.step():
			pass
		return 'YES' if self.yes else 'NO'

def decode_inputs(inputs):
	ilist = [int(i) for i in inputs[0].split()]
	return ilist

def calculate(inputs):
	return Scales(decode_inputs(inputs)).calculate()

def main():
	inputs = [input()]
	print(calculate(inputs))

class unitTests(unittest.TestCase):

	def test_decode_inputs(self):
		self.assertEqual(decode_inputs(['2 5']), [2, 5])

	def test_Scales_class__basic_functions(self):
		d = Scales([3, 7])
		self.assertEqual(d.w, 3)
		self.assertEqual(d.m, 7)
		self.assertEqual(d.maxwp, 2)
		self.assertEqual(d.list2dec([1, 0, 2]), 19)
		self.assertEqual(d.dec2list(19), [1, 0, 2])
		d = Scales([2, 3])
		self.assertEqual(d.it_min, 0)
		self.assertEqual(d.it_max, 26)
		d = Scales([2, 3])
		self.assertEqual(d.step(), 1)
		self.assertEqual(d.it_min, 13)
		self.assertEqual(d.it_max, 26)
		d = Scales([3, 7])
		self.assertEqual(d.calc_weight(d.list2dec([0, 1, 2])), 8)

	def test_calculate(self):
		self.assertEqual(calculate(['3 7']), 'YES')
		self.assertEqual(calculate(['100 99']), 'YES')
		self.assertEqual(calculate(['2 92600']), 'YES')
if sys.argv[-1] == '-ut':
	unittest.main(argv=[' '])
main()


import sys
input = sys.stdin.readline
(w, m) = map(int, input().split())
for i in range(33):
	m = min(m, abs(m - w ** (32 - i)))
if m == 0:
	print('YES')
else:
	print('NO')


(w, m) = map(int, input().split())
while m > 0:
	digit = m % w
	if digit == 1:
		m -= 1
	elif digit == w - 1:
		m += 1
	elif digit != 0:
		m = -1
	if m > 0:
		m //= w
print('YES' if m == 0 else 'NO')


import os
import sys
from io import BytesIO, IOBase

def main():
	(w, m) = map(int, input().split())
	an = []
	while m:
		an.append(m % w)
		m //= w
	f = 1
	for i in an:
		if i > 1:
			f = 0
			break
	if f:
		print('YES')
	else:
		an.append(0)
		l = len(an)
		v = [0] * l
		for i in range(l - 1):
			if an[i] > 1:
				if an[i] == w - 1:
					v[i] = 1
					an[i] = 0
					an[i + 1] += 1
				elif an[i] == w:
					an[i + 1] += 1
					an[i] = 0
				else:
					print('NO')
					return
		for i in range(l):
			if an[i] and v[i]:
				print('NO')
				return
		print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math
import sys

def test(d):
	if d == 1:
		return 1
	if d % w == 0:
		k = round(d / w)
		return test(k)
	if (d - 1) % w == 0:
		k = round((d - 1) / w)
		return test(k)
	if (d + 1) % w == 0:
		k = round((d + 1) / w)
		return test(k)
	return 0
inp = list(map(int, input().split()))
w = inp[0]
m = inp[1]
a = 1
for i in range(0, 100):
	if a > m:
		d = a - m
		break
	a *= w
ans = test(d)
if ans == 1:
	print('YES')
else:
	print('NO')


from sys import exit
(w, m) = map(int, input().split())
if w in (2, 3):
	print('YES')
	exit()
while True:
	if m in (0, 1):
		print('YES')
		exit()
	prev = 1
	cur = w
	while cur < m:
		prev = cur
		cur *= w
	if cur == m:
		print('YES')
		exit()
	sum1k = (cur - 1) // (w - 1)
	if sum1k < m < cur - sum1k:
		print('NO')
		exit()
	m = min(m - prev, cur - m)


line = input()
(w, m) = map(int, line.split(' '))
while m > 0:
	if (m - 1) % w == 0:
		m -= 1
	elif (m + 1) % w == 0:
		m += 1
	elif m % w != 0:
		print('NO')
		exit(0)
	m //= w
print('YES')


(W, M) = input().split(' ')
W = int(W)
M = int(M)
if W == 2 or W == 3:
	print('YES')
else:
	N = 16
	A = [0] * (N + 1)
	A[0] = 1
	for I in range(1, N):
		A[I] = A[I - 1] * W
		if A[I] > 10000000001000000000:
			N = I
			break
	S = set()
	ok = False
	for msk in range(1 << N):
		curr = 0
		for I in range(N):
			if msk & 1 << I > 0:
				curr += A[I]
		if curr == M or curr - M in S:
			ok = True
			break
		S.add(curr)
	if ok:
		print('YES')
	else:
		print('NO')


__author__ = 'lenovo-pc'
(w, m) = map(int, input().split(' '))
ans = True
while m:
	k = m % w
	if k == 1:
		m -= 1
	elif k == w - 1:
		m += 1
	elif k == 0:
		pass
	else:
		ans = False
	m //= w
if ans:
	print('YES')
else:
	print('NO')


import sys
(w, m) = list(map(int, sys.stdin.readline().split()))
a = sum((pow(w, i) for i in range(101))) + m
while a > 0:
	if a % w > 2:
		print('NO')
		sys.exit(0)
	a //= w
print('YES')


(M, N) = map(int, input().split())
used = {}
P = 0
while M ** P < N:
	P += 1
for p in range(P, -1, -1):
	k = M ** p
	if abs(k - N) < N:
		N = abs(k - N)
if N == 0:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

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

def getStr():
	return input().strip()

def intToBase(x, base):
	res = []
	while x > 0:
		res.append(x % base)
		x //= base
	return res
(W, M) = getVars()
s = intToBase(M, W)
s.append(0)
res = 'YES'
for i in range(len(s)):
	if s[i] == 0 or s[i] == 1:
		continue
	if s[i] == W - 1:
		k = i
		s[k] = 0
		while s[k] == 0:
			s[k + 1] = (s[k + 1] + 1) % W
			k += 1
		continue
	res = 'NO'
	break
print(res)


__author__ = 'taras-sereda'
from itertools import takewhile
from functools import partial

def vanjva_v():
	(w, m) = map(int, input().split())
	base_w = partial(pow, w)
	powers = list(takewhile(lambda x: x <= m, map(base_w, range(101))))
	powers.append(base_w(len(powers)))
	if w == 2 or w == 3:
		return 'YES'
	while m != 0 and powers:
		min_dif = min(map(lambda x: abs(m - x), powers))
		remains = sorted(filter(lambda x: abs(x - m) == min_dif, powers), reverse=True)
		taken = remains[0]
		if m < taken:
			m = taken - m
		else:
			m -= taken
		powers.remove(taken)
	return 'YES' if not m else 'NO'
	s_powers = sorted(powers, key=lambda x: -(x - m))
print(vanjva_v())


(w, x) = map(int, input().split())
a = []
cur = w ** 50
while cur:
	if x >= cur:
		a.append(x // cur)
		x %= cur
	else:
		a.append(0)
	cur //= w
for i in range(50, 1, -1):
	if a[i] > 1:
		a[i] -= w
		a[i - 1] += 1
	if a[i] not in {-1, 0, 1}:
		print('NO')
		exit(0)
print('YES')


(base, need) = map(int, input().split())
if base == 2:
	print('YES')
else:
	digits = []
	while need:
		digits.append(need % base)
		need //= base
	res_found = False
	for i in range(len(digits)):
		if digits[i] == base - 1:
			digits[i] = 0
			try:
				digits[i + 1] += 1
				j = i + 1
				while digits[j] == base:
					digits[j] = 0
					try:
						digits[j + 1] += 1
					except:
						digits.append(1)
					j += 1
			except:
				digits.append(1)
		elif digits[i] > 1:
			print('NO')
			res_found = True
			break
	if not res_found:
		print('YES')