您的位置:首页 > 其它

CodeForces - 552C Vanya and Scales (进制转换&技巧)好题

2016-04-21 20:51 681 查看
CodeForces
- 552C

Vanya and Scales

Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u
Submit Status

Description

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.

Sample Input

Input
3 7


Output
YES


Input
100 99


Output
YES


Input
100 50


Output
NO


Hint

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.

Source

Codeforces Round #308 (Div. 2)

//题意:输入w,m。

表示给你一个w,你可以使用w^0,w^1,w^2w^3.........w^100;这一百零一个数,现在又给你一个数m,让你将m放到天平的右边,现在问你能否通过在两边天平上添加那101个砝码使得天平平衡。

//思路:

将m转换成w进制数,再枚举每一位上的数,如果第i位为w或者是w-1(加一的话就可以进位)的话可以将它转换为0,第i+1位进1。这些位上的数如果全能转换成1或者0的话,就表示m可以被表示,否则不能被表示。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define ll long long
#define N 20
using namespace std;
int a
;
int main()
{
int w,m,n,i,j,k;
while(scanf("%d%d",&w,&m)!=EOF)
{
k=0;
while(m)
{
a[k++]=m%w;
m/=w;
}
int flag=1;
for(i=0;i<k;i++)
{
if(a[i]==w)
{
a[i]-=w;
a[i+1]++;
}
if(a[i]<=1)
continue;
else if(a[i]==w-1)
{
a[i]=0;
a[i+1]++;
}
else
flag=0;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: