CodeForces - 552C Vanya and Scales —— 进制的理解

题目链接:https://vjudge.net/contest/224393#problem/C

Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 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 ≤ 1091 ≤ m ≤ 109) — 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 99and 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.

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <cstdlib>
 6 #include <string>
 7 #include <vector>
 8 #include <map>
 9 #include <set>
10 #include <queue>
11 #include <sstream>
12 #include <algorithm>
13 using namespace std;
14 typedef long long LL;
15 const double eps = 1e-8;
16 const int INF = 2e9;
17 const LL LNF = 9e18;
18 const int MOD = 1e9+7;
19 const int MAXN = 1e5+10;
20 
21 int w, m, a[110];
22 int main()
23 {
24     while(scanf("%d%d",&w,&m)!=EOF)
25     {
26         int n = 0;
27         memset(a, 0, sizeof(a));
28         while(m)
29         {
30             a[++n] = m%w;
31             m /= w;
32         }
33 
34         bool flag = true;
35         for(int i = 1; i<=n; i++)
36         {
37             if(-1<=a[i]&&a[i]<=1) continue;  
38             else if(a[i]==w-1) a[i] = -1, a[i+1]++;    
39             else if(a[i]==w) a[i] = 0, a[i+1]++;    
40             else { flag = false; break;}
41         }
42 
43         if(flag && a[n+1]<=1) puts("YES");
44         else puts("NO");
45     }
46 }
View Code

猜你喜欢

转载自www.cnblogs.com/DOLFAMINGO/p/8995332.html