Vanya and ScalesTime
limit:1000MS
Memory Limit:262144KB
64bit IO Format:%i64d &%i6 4u SubmitStatusPracticecodeforces 552C
Description
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 massm and some Wei Ghts on the left pan of the scales, and some weights on the right pan of the scales so then the pans of the scales were in 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 Input
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
Hint
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.
1#include <stdio.h>2#include <string.h>3 intMain ()4 {5 intw,m;6 intI,J,FLG;7 while(SCANF ("%d%d", &w,&m)! =EOF)8 {9flg=1;Ten while(m) One { A inta=m%W; - if(a==1|| a==0) - { them=m/W; - } - Else if(a==w-1) - { +m=m/w+1; - } + Else A { atflg=0; - Break; - } - } - if(FLG) -printf"yes\n"); in Else -printf"no\n"); to } + return 0; -}
View Code
Codeforces 552C Vanya and Scales