time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

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

【题目链接】:http://codeforces.com/contest/552/problem/C

【题解】



先将m转换成w进制;

如果最后w进制的每个位都为0或1,则直接输出YES;

对于w进制的m,如果其中的某一位存在w-1,则尝试将w-1变成-1,然后把后一位递增1;

如果某一位中存在2..w-2中的数则无解.

可以用样例的3 7来解释;

7 = 3^0 + 2*3^1;

因为2==3-1

则将2*3^1处理一下

变成(3-1)*3^1

就变成了1*3^2-1*3^1



7 = 3^0-3^1+3^2

这时每个数的系数都为1或-1,显然符合题意.

可以看到我们在进行这个转换的时候把第x位系数为w-1变成了-1,然后第x+1位的权值递增了1;即又添加了一个3^2;

那为什么系数为2..w-2的时候不能进行这样的转换?

比如w=4的时候系数为2

比如

2*4^3

这个时候会变成

(4-2)*4^3;

即4^4-2*4^3;

可以看到4^3的系数不会是-1.则不符合题意.因为每种砝码都只能选一次.移到左边后要用两次4^3.

当做技巧吧。不好想。



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int w,m,cnt = 0;
int a[MAXN]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(w);rei(m);
while (m>0)
{
a[++cnt] = m%w;
m/=w;
}
rep1(i,1,cnt)
{
if (a[i] == w)
{
a[i+1]++;
a[i] = 0;
}
else
if (a[i]==w-1)
{
a[i] = -1;
a[i+1]++;
}
else
if (a[i] == 1 || a[i]==0)
continue;
else
{
puts("NO");
return 0;
}
}
puts("YES");
return 0;
}

【30.23%】【codeforces 552C】Vanya and Scales的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. codeforces 552 C Vanya and Scales

    这个题的意思就是给出一个数m.以及一个以1为首元素.w为比例常数的等比数列,数列长度为101,数列中每一个数字最多仅仅能用一次.问是否存在xa+wb+--=wc+wd+--+we+m. 非常显然,换句 ...

  3. CodeForces 552C Vanya and Scales

    Vanya and Scales Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  4. 【20.23%】【codeforces 740A】Alyona and copybooks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【23.33%】【codeforces 557B】Pasha and Tea

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【30.93%】【codeforces 558E】A Simple Task

    time limit per test5 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  7. 【23.39%】【codeforces 558C】Amr and Chemistry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【30.36%】【codeforces 740D】Alyona and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【30.49%】【codeforces 569A】Music

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. AJAX - 封装AJAX GET 数组join( )方法 键值对取value POST请求参数注意点

    function objToStr(obj){ obj.t = new Date().getTime(); // 给obj动态增加了一个属性 // 这个给对象添加属性的方法, 会被直接加到键值对里?? ...

  2. Python 之Numpy应用

    NumPy 数据类型 numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型.下表列举了常用 NumPy ...

  3. Git安装及密钥的生成并上传本地文件到GitHub上

    之前用的GitHub,不太熟练,一直在上传的过程中遇到了一些问题,看了网上诸多教程,总觉得很乱,特参考一些资料,总结了一篇完整的操作步骤,从下载安装到上传文件,亲测有效 1.下载Git软件:https ...

  4. ODBC总结

    引用头文件:sql.h.sqlext.h.sqltypes.h 添加库文件:odbc32.lib   odbccp32.lib 1.定义环境变量: SQLHENV henv =nullptr://环境 ...

  5. 洛谷——P1073 最优贸易 ([NOIP2009] )

    https://www.luogu.org/problem/show?pid=1073 题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多 ...

  6. 建立一个 Openshift "Do-It-Yourself" 应用

    建立一个 Openshift "Do-It-Yourself" 应用 Openshift 的  "Do-It-Yourself" 就是自己可以编译定制 WEB ...

  7. 指示函数(indicator function) 的作用

    1. counter 指示函数常用于次数(满足某一断言或条件)的统计: 2. 二维的离散指示函数 ⇒ assignment solution xij∈{0,1},∑jxij=1 ∑jxij=1:行和为 ...

  8. 支持10W高并发请求的IIS Web服务器常用设置

    支持高并发的IIS Web服务器常用设置   适用的IIS版本:IIS 7.0, IIS 7.5, IIS 8.0 适用的Windows版本:Windows Server 2008, Windows ...

  9. WEB前端--深入进去

    在网站开发这条道路上做专做精,一个专题一个专题的深入探索,一个盲区一个盲区的理解和记忆,终有大成的那一天的.

  10. GO语言学习(十五)Go 语言指针

    Go 语言指针 Go 语言中指针是很容易学习的,Go 语言中使用指针可以更简单的执行一些任务. 接下来让我们来一步步学习 Go 语言指针. 我们都知道,变量是一种使用方便的占位符,用于引用计算机内存地 ...