C. Vanya and Scales

C. Vanya and Scales
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ 109, 1 ≤ 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 99 and 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.

 思路:如果是不只一个的话,那么就是转换成k进制。

但是限制了一个,那么将这个数转化为k进制,由于两边都能放,所以这个数可以看成是两个数的差,并且这两个数是由0,1组成的,那么就可以转换成当前这个数加上一个只有0,1组成的数是否能组成另一个全0,1的数,要判断的这个数的位上当前是0,1就不用加1,也就是

另一个数这位为0,否则如果等于n-1,就需要加1,否则无论加或不加1都不能得到当前位为0,或1,也就不能达到。

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<stdlib.h>
 4 #include<queue>
 5 #include<string.h>
 6 #include<iostream>
 7 #include<math.h>
 8 #include<queue>
 9 #include<vector>
10 using namespace std;
11 typedef long long LL;
12 const LL mod = 1e9+7;
13 int ak[100000];
14 int main(void)
15 {
16         LL n,m;
17         while(scanf("%lld %lld",&n,&m)!=EOF)
18         {
19                 int flag = 0;
20                 int cn = 0;
21                 while(m)
22                 {
23                         ak[cn++] = m%n;
24                         m/=n;
25                 }
26                 int d = 0;
27                 if(n==2)printf("YES\n");
28                 else
29                 {for(int i = 0; i < cn; i++)
30                 {
31                       ak[i]+=d;
32                       if(ak[i]==n-1)
33                       {
34                           ak[i]++;
35                       }
36                       d = ak[i]/n;
37                       ak[i]=ak[i]%n;
38                       if(ak[i]!=0&&ak[i]!=1)
39                         flag = 1;
40                 }
41                 if(flag)printf("NO\n");
42                 else printf("YES\n");}
43         }
44         return 0;
45 }

代码库

posted @ 2016-10-25 23:04  sCjTyC  阅读(166)  评论(0编辑  收藏  举报