codeforces 552 C Vanya and Scales

时间:2022-08-28 16:23:35

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

非常显然,换句话说就是问,是否存在m=wa+wb+……+wf-wc-wd-……-we。再进行化简就能够得到。是在问,是否存在m=((((wh±1)wi±1)±1)wj±1)wk……。

那么非常显然可以进行搜索,比方说用广搜,每次当前的m±1中若有因子w就所有约掉,然后丢到队列中,否则就不用管,仅仅要队列中可以出现1,那么这样的情况就是存在的。反之就是不存在。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int cnt(int w,int x)
{
while(x%w==0)
x/=w;
return x;
}
queue<int>qq;
bool bfs(int w,int m)
{
int t=cnt(w,m);
if(t!=m)
qq.push(t);
else
qq.push(m);
while(qq.size())
{
t=qq.front();
qq.pop();
if(t==1)
return 1;
int t1=cnt(w,t+1);
if(t1!=t+1)
qq.push(t1);
t1=cnt(w,t-1);
if(t1!=t-1)
qq.push(t1);
}
return 0;
}
int main()
{
int w,m;
cin>>w>>m;
if(bfs(w,m))
puts("YES");
else
puts("NO");
}
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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

Sample test(s)
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.

codeforces 552 C Vanya and Scales的更多相关文章

  1. 【30&period;23&percnt;】【codeforces 552C】Vanya and Scales

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

  2. codeforces 552 E&period; Vanya and Brackets 表达式求值

    题目链接 讲道理距离上一次写这种求值的题已经不知道多久了. 括号肯定是左括号在乘号的右边, 右括号在左边. 否则没有意义. 题目说乘号只有15个, 所以我们枚举就好了. #include <io ...

  3. codeforces&num;552 D&period; Vanya and Triangles(几何)

    题意:给出n个不同的点,问能组成多少个不同的三角形 题解:对于每个点对,我们生成一个直线,用a*x+b=y表示,用map记录ab,这样就确定了一个直线,这样我们就能算出有多少点是共线的,这样复杂度就是 ...

  4. Codeforces Round &num;308 &lpar;Div&period; 2&rpar; C&period; Vanya and Scales dfs

    C. Vanya and Scales Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/552/p ...

  5. CodeForces 552C Vanya and Scales

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

  6. 暴力&sol;进制转换 Codeforces Round &num;308 &lpar;Div&period; 2&rpar; C&period; Vanya and Scales

    题目传送门 /* 题意:问是否能用质量为w^0,w^1,...,w^100的砝码各1个称出重量m,砝码放左边或在右边 暴力/进制转换:假设可以称出,用w进制表示,每一位是0,1,w-1.w-1表示砝码 ...

  7. codeforces C&period; Vanya and Scales

    C. Vanya and Scales Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w10 ...

  8. Codeforces Round &num;308 &lpar;Div&period; 2&rpar;----C&period; Vanya and Scales

    C. Vanya and Scales time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Vanya and Scales&lpar;思维&rpar;

    Vanya and Scales time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. 数据集偏斜 - class skew problem - 以SVM松弛变量为例

    原文 接下来要说的东西其实不是松弛变量本身,但由于是为了使用松弛变量才引入的,因此放在这里也算合适,那就是惩罚因子C.回头看一眼引入了松弛变量以后的优化问题: 注意其中C的位置,也可以回想一下C所起的 ...

  2. &lbrack;Java编程思想-学习笔记&rsqb;第4章 控制执行流程

    4.1  return 关键字return有两方面的用途:一方面指定一个方法结束时返回一个值:一方面强行在return位置结束整个方法,如下所示: char test(int score) { if ...

  3. shell脚本的执行方式

    编写好的shell脚本(如:test),可以采取两种方式进行运行: 一. $ sh test 一般不采用这种调用方式,尤其不采用"sh<test"的调用方式,因为这种方式将禁 ...

  4. 格式化分区,报&sol;dev&sol;sdb1 is apparently in use by the system&semi; will not make a filesystem here&excl;

    [root@RAC2 ~]# mke2fs /dev/sdb1mke2fs 1.39 (29-May-2006)/dev/sdb1 is apparently in use by the system ...

  5. Android 之JDBC

    JDBC(Java DataBase Connectivity)是使用 Java 存取数据库系统的标准解决方案,它将不同数据库间各自差异API与标准 SQL语句分开看待,实现数据库无关的 Java操作 ...

  6. 二叉排序树&lpar;BST&rpar;构造与应用

             二叉排序树(BST)构造与应用       本文取自<数据结构与算法>(C语言版)(第三版).出版社是清华大学出版社.       本博文作为学习资料整理. 源码是VC+ ...

  7. 七牛php-sdk使用-文件上传

    使用七牛进行文件上传可以有多种方式: 直接form表单上传,需要自己按照文档做配置 使用七牛jssdk,部署较简单,大文件分片上传 php-sdk后台上传 首先,所有的上传方法以及所有的跟七牛接口相关 ...

  8. 用DriverStudio开发USB驱动程序

    很多写Windows Device Driver的开发人员基本上都是使用Windows DDK进行开发的.但是,现在也有不少人都开始借助一些辅助工具.笔者去年开始接触到DriverStudio,发现它 ...

  9. Lua常用封装方法

    Lua 获取随机值 --获取随机值,指定上限和下限 function getRandom(min,max) -- 接收一个整数n作为随即序列的种子 math.randomseed(os.time()) ...

  10. 20165207 Exp2 后门原理与实践

    20165207 Exp2 后门原理与实践 〇.实验准备 两个虚拟机,一个kali一个win7.kali的ip是192.168.43.72,win7的ip是192.168.43.116,在win7关掉 ...