现在有一个重量为m的物品 放到天平上 利用一些砝码使得天平平衡
砝码的重量为w的i次方 问有没有一种方式使得天平平衡
每个砝码只有三种选择 不放 放左边 放右边
其实就是等式a0*w^0+a1*w^1+……+an*w^n=m有没有解的情况 ai的值只能是0 1 -1
对于这个等式 让m不断的除以w 则模m得到的值只能是 0 1 w-1(-1)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
int main()
{
//fread;
int w,m;
while(scanf("%d%d",&w,&m)!=EOF)
{
if(w<=3)
{
puts("YES");
continue;
}
int flag=1;
while(m)
{
int x=m%w;
if(x<=1)
m/=w;
else if(x==w-1)
m=m/w+1;
else
{
puts("NO");
flag=0;
break;
}
}
if(flag)
puts("YES");
}
return 0;
}