Codeforces C. Vanya and Scales

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.