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. BZOJ3277: 串(后缀自动机,Parent树,Dfs序)

    Description 字符串是oi界常考的问题.现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中 至少k个字符串的子串(注意包括本身). Input 第一行两个整数n, ...

  2. itchat转发指定的微信群里某个用户的发言到指定的群

    复读机功能, 如果有比较多的用户,超出500人,那就得分开至少两个群,如何把一些消息自动复制到另一个群呢. 自动转发指定用户的发言,转发到别的群 # !/usr/bin/env python # -* ...

  3. hibernate中的事务管理是怎么概念?

    1.JDBC事务 JDBC 事务是用 Connection 对象控制的.JDBC Connection 接口( java.sql.Connection )提供了两种事务模式:自动提交和手工提交. ja ...

  4. KNIMI数据挖掘建模与分析系列_002_利用KNIMI做商超零售关联推荐

    利用KNIMI做商超零售关联推荐 http://blog.csdn.net/shuaihj 一.測试数据 须要測试数据,请留下邮箱 二.训练关联推荐规则 1.读取销售记录(sales.table) 2 ...

  5. 想知道WiFi是什么样子的么?

    据英国<每日邮报>报道.英国纽卡斯尔大学博士生路易斯·赫南日前利用定制的仪器为WiFi信号拍照,绘制出一系列展现人类周围无形网络WiFi连接情况的图,这些盘旋环绕的明亮光束,宛如幽灵一般缠 ...

  6. amazeui学习笔记一(开始使用1)--主干

    amazeui学习笔记一(开始使用1)--主干 一.总结 1.英语:学好英语,编程轻松很多 2. layouts compatibility change log web app collection ...

  7. event事件对象 兼容写法:var oEvent=ev||event;和取消事件冒泡

    要在整个页面添加事件要使用document,例如要捕抓鼠标位置 document.onclink=function(ev)  //FireFox Chrome默认都是有一个值传进来的 { var oE ...

  8. VUE笔记 - 列表过渡动画 v-enter, v-leave-to | v-enter-active, v-leave-active | v-move

    本例要结合过渡动画四个过程的示意图一起理解. https://cn.vuejs.org/v2/guide/transitions.html 疑问: v-for="(item,i) in li ...

  9. GO语言学习(十九)Go 错误处理

    Go 错误处理 Go 语言通过内置的错误接口提供了非常简单的错误处理机制. error类型是一个接口类型,这是它的定义: type error interface { Error() string } ...

  10. Mongodb总结6-数据库启动、停止、备份等命令

    #启动Mongodb默认启动,需要在/data/db,Windows下对应的目录是Mongod.exe所在磁盘分区的根目录,例如Mongodb存放在D:/Mongodb,那么对应的路径就是D:/dat ...