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

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.

and 'NO' if it cannot.

题目大意:给你一个m给你一个w,用w的0次方1次方2次方。。。。,来使左端放着m克重物的天平平衡

解题思路:万万没想到这是一个进制转换,看到w的n次方就应该想到是进制转换,把m转换成w进制,换好的进制每一位就代表右端每一种砝码放置的个数,比方说第一个样例把7转换为3进制为21,那么就代表3的0次方砝码需要1个,3的1次方砝码需要两个,只需要把这三个砝码放在右边天平就可以平衡,但问题是,每个砝码只能放一次,就需要在左端放砝码,使左端转换为w进制之后为只有1,0的情况,由于只能每个砝码使用一次,所以只能将m转换为w进之后,把那些需要某种砝码个数为w-1的再加一个同种砝码,使其进一位,但是如果出现在1和w-1之间的情况,则该情况无法实现

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long int n,m;
    scanf("%lld %lld",&n,&m);
    int flag=1;
    long long int f=0;
    while(m>0)
    {
        if(m%n+f==1||m%n+f==0)
        {
            f=0;
            m=m/n;
            continue;
        }
        else if(m%n+f==n-1||m%n+f==n)
        {
            f=1;
            m=m/n;
            continue;
        }
        else
        {
            flag=0;
            break;
        }
    }
    if(flag)
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/w13884794538/article/details/81219353