C - Vanya and Scales

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 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。

题解:这道题是有关进制转换的,例如7的二进制转换为十进制位2的0+2的1次方+2的2次方,当我们把m转换为w进制,不就和题目相关,我们假设有一个称,遍历w进制转换后的每一个位置,如果是1或0的话就跳过这次循环相当于把砝码放在右边,如果其他的值的话就要分情况了,如果是w-1,那么我们就把砝码放在左边,然后这个位置就变为0了,然后就看后边,如果这个位置不是w-1的话,那么称不能平衡。

代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    __int64 a,b;
    int h[2005];
    int c,d,e;
    scanf("%I64d %I64d",&a,&b);
    c=-1;
    while(b!=0)
    {
        h[++c]=b%a;
        b=b/a;
    }
    e=1;
    for(d=0;d<=c;d++)
    {
        if(h[d]==0||h[d]==1)
        {
            continue;
        }
        h[d]=a-h[d];
        h[d+1]+=1;
        if(h[d]!=1&&h[d]!=0)
        {
            e=0;
            break;
        }
    }
    if(e)
        printf("YES\n");
    else printf("NO\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/The_city_of_the__sky/article/details/81221467