forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
61 lines (49 loc) · 1.35 KB
/
C.cpp
File metadata and controls
61 lines (49 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
using namespace std;
vector<long long> all;
bool can;
void attempt(int id, long long need) {
if (can) return ;
if (need == 0) {
can = true;
return ;
}
if (id == all.size()) return ;
int lb;
if (id == 0) lb = 0; else lb = -1;
int ub = 1;
FOR(mult,lb,ub) {
attempt(id+1, need-mult*all[id]);
}
}
bool solve(long long w, long long m) {
if (w <= 3) return true;
if (m == 1) return true;
all.clear();
long long ln = 1;
all.push_back(1);
while (ln <= m) {
ln *= w;
all.push_back(ln);
}
reverse(all.begin(), all.end());
can = false;
attempt(0, m);
return can;
}
int main() {
ios :: sync_with_stdio(false);
long long w, m;
while (cin >> w >> m) {
cout << (solve(w, m) ? "YES" : "NO") << endl;
}
return 0;
}