C. Vanya and Scales

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/552/problem/C

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

3 7

Sample Output

YES

HINT

题意

给你100个砝码,第i个砝码质量是w^i,然后问你能不能在有m的情况下,左边和右边都放砝码,使得这个天平平衡

题解:

dfs直接暴力就好了……

对于这个砝码来说,只有3种选择,放左边,不放,放右边

由于2和3直接输出yes,所以答案也就没多少了

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//**************************************************************************************
ll w,m;
int flag;
void dfs(ll a,ll b,ll c)
{ if(c==m)
{
flag=;
return;
}
if(flag||a==-)
return;
if(abs(b)<=m*w)
{
dfs(,b*w,c+b);
dfs(,b*w,c-b);
dfs(,b*w,c);
} return;
}
int main()
{
flag=;
w=read(),m=read();
if(w<=)
{
cout<<"YES"<<endl;
return ;
}
dfs(,,);
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}

最新文章

  1. SegmentControl 那些令人烦恼的事儿
  2. Execute SQL Task 参数和变量的映射
  3. 64位python安装MySQL-python 1.2.5
  4. LPC1768之GPIO
  5. Silverlight中动画的性能浅析
  6. JAVA_HttpClientUtils
  7. 【原创】QT5-卸载精灵v1.0-卸载windows软件-简易版
  8. Hyper-v 安装CentOS
  9. Mac os 进行Android开发笔记(1)
  10. 推荐三款日期选择插件(My97DatePicker+jquery.datepicker+Mobiscroll)
  11. 网页授权——扫二维码获取openid
  12. php里面的变量的使用
  13. logstash/conf.d文件编写
  14. tomcat中显示本地图片①(未解决)
  15. opencv+qt+vtk,编程时报错&#39;detail&#39;:ambiguous symbol
  16. 利用OpenLayers创建wkt字符串
  17. 【learning】vim爆改记 (如何让vim用起来像devc++)
  18. Ubuntu 下新建用户后无法sudo
  19. bootstrap.min.css.map HTTP/1.1&quot; 404 1699
  20. 使用js合并table中的单元格

热门文章

  1. EditText 光标不显示问题
  2. php codeigniter (CI) oracle 数据库配置-宋正河整理
  3. lua Date和Time
  4. 【LeetCode】226 - Invert Binary Tree
  5. 初识 istringstream、ostringstream、stringstream 运用
  6. Javascript类型转换表
  7. EF selection expression 与 Linq备忘
  8. windows端口被占用
  9. The OAuth 2.0 Authorization Framework-摘自https://tools.ietf.org/html/rfc6749
  10. 【转】类中如何引用server.MapPath()