C. Vanya and Scales
Vanya have a scales for weighing loads and weights of masses w0, w1, w2, ..., w 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's possible to place an item of mass m and some we Ights on the left pan of the scales, and some weights on the right pan of the scales so, the pans of the scales were I N balance.
Input
The first line contains integers w, m (2≤ w ≤109, 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.
Sample Test (s) 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 has an item of mass 7 and a weight of mass 3, and the second pan can has both weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can has an item of mass and the weight of mass 1, and the second pan can has the weight of mass .
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input.
Test instructions: Given two numbers, M and W, existing w^0, w^1, w^2...w^100 a weight of mass, with some of the mass, when the weight of M can be weighed.
It is a formula like this:
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespacestd;intMain () {intM, W; CIN>>w>>m; while(m) {if(! (M-1) (%w)) m--; Else if(! ((m+1) (%w)) m++; Else if(m%W) {cout<<"NO"<<Endl; return 0; } m/=W; } cout<<"YES"<<Endl; return 0;}
Codeforces C. Vanya and Scales