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
Copy
3 7
output
Copy
YES
input
Copy
100 99
output
Copy
YES
input
Copy
100 50
output
Copy
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.
题意:
有砝码重为w1次方,w2次方,w3次方....,问能不能称出质量为m的物品。
思路:
可以列出一个式子a1*w1+a2*w2+a3*w3+..=m;
根据题意 ai的值只能为0,1,-1.
这个式子有点类似进制转换,但关键是-1的处理其实-1可以理解为m分解成当前位有w-1个w需要加一个w。处理好这个就好写代码
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long
using namespace std;
int main()
{
int w,m;
cin>>w>>m;
if(w<=2)
{
cout<<"YES"<<endl;
return 0;
}
while(m)
{
if(!((m-1)%w)) m--;
else if(!((m+1)%w)) m++;
else if(m%w)
{
cout<<"NO"<<endl;
return 0;
}
m/=w;
}
cout<<"YES"<<endl;
return 0;
}

本文探讨了如何使用一系列特定质量的砝码称量出指定质量的物品。通过数学公式和编程实现,解决了砝码质量为w的幂次方时能否称量出质量m物品的问题。文章提供了详细的思路解析及C++代码示例。
315

被折叠的 条评论
为什么被折叠?



