class
GFG {
static
int
found =
0
;
static
void
solve(
int
idx,
int
itemWt,
int
wts[],
int
N) {
if
(found ==
1
) {
return
;
}
if
(itemWt ==
0
) {
found =
1
;
return
;
}
if
(idx > N) {
return
;
}
solve(idx +
1
, itemWt, wts, N);
solve(idx +
1
, itemWt + wts[idx], wts,
N);
solve(idx +
1
, itemWt - wts[idx], wts,
N);
}
static
boolean
checkItem(
int
a,
int
W) {
if
(a ==
2
|| a ==
3
) {
return
true
;
}
int
wts[] =
new
int
[
100
];
int
totalWts =
0
;
wts[
0
] =
1
;
for
(
int
i =
1
;; i++) {
wts[i] = wts[i -
1
] * a;
totalWts++;
if
(wts[i] > 1e9) {
break
;
}
}
solve(
0
, W, wts, totalWts);
if
(found ==
1
) {
return
true
;
}
return
false
;
}
public
static
void
main(String[] args) {
int
a =
2
, W =
5
;
if
(checkItem(a, W)) {
System.out.println(
"YES"
);
}
else
{
System.out.println(
"NO"
);
}
a =
4
; W =
11
;found =
0
;
if
(checkItem(a, W)) {
System.out.println(
"YES"
);
}
else
{
System.out.println(
"NO"
);
}
a =
4
; W =
7
; found =
0
;
if
(checkItem(a, W)) {
System.out.println(
"YES"
);
}
else
{
System.out.println(
"NO"
);
}
}
}