Vanya and Scales(思维)
1 second
256 megabytes
standard input
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.
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.
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
3 7
YES
100 99
YES
100 50
NO
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.
题解:很棒的一个题,初一看完全没思路,到最后也没搞定。。。这个题的意思是给w的(0-100)次方,0-100次方只用一次,使天平平衡,
转化一下就是一个w进制的数能否通过加减w的(0-100)次方,0-100次方只用一次,使之等于0;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<algorithm>
using namespace std;
int a[];
int main(){
int w, m;
while(~scanf("%d%d", &w, &m)){
int k = ;
while(m){
a[k++] = m % w;
m /= w;
}
int ans = ;
for(int i = ; i < k; i++){
if(a[i] == w){
a[i] = ;
a[i + ]++;
continue;
}
if(a[i] <= ){
continue;
}
else if(a[i] == w - ){
a[i + ]++;
a[i] = ;
continue;
}
else
ans = ;
}
if(ans)
puts("YES");
else
puts("NO");
}
return ;
}
Vanya and Scales(思维)的更多相关文章
- codeforces C. Vanya and Scales
C. Vanya and Scales Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w10 ...
- CodeForces 552C Vanya and Scales
Vanya and Scales Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- Codeforces Round #308 (Div. 2)----C. Vanya and Scales
C. Vanya and Scales time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #308 (Div. 2) C. Vanya and Scales dfs
C. Vanya and Scales Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/552/p ...
- 暴力/进制转换 Codeforces Round #308 (Div. 2) C. Vanya and Scales
题目传送门 /* 题意:问是否能用质量为w^0,w^1,...,w^100的砝码各1个称出重量m,砝码放左边或在右边 暴力/进制转换:假设可以称出,用w进制表示,每一位是0,1,w-1.w-1表示砝码 ...
- Codeforces 552C Vanya and Scales(进制转换+思维)
题目链接:http://codeforces.com/problemset/problem/552/C 题目大意:有101个砝码重量为w^0,w^1,....,w^100和一个重量为m的物体,问能否在 ...
- Vanya and Scales CodeForces - 552C (思维)
大意: $101$个砝码, 重$w^0,w^1,...,w^{100}$, 求能否称出重量$m$. w<=3时显然可以称出所有重量, 否则可以暴力双端搜索. #include <iostr ...
- C. Vanya and Scales
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- codeforces 552 C Vanya and Scales
这个题的意思就是给出一个数m.以及一个以1为首元素.w为比例常数的等比数列,数列长度为101,数列中每一个数字最多仅仅能用一次.问是否存在xa+wb+--=wc+wd+--+we+m. 非常显然,换句 ...
随机推荐
- GCD 倒计时
今天在Code4App上看了一个GCD倒计时的Demo,觉得不错代码贴出来备用 -(void)startTime{ __block ; //倒计时时间 dispatch_queue_t queue = ...
- Ruby新手教程和技巧
Ruby真的比Java更好? Ruby On Rails 创始人:对Java 说再见 这两周以来环绕Java发生的两件大事:EclipseCon 和TheServerSide Java Sympo ...
- boost::asio 的同、异步方式
转自:http://blog.csdn.net/zhuky/archive/2010/03/10/5364574.aspx Boost.Asio是一个跨平台的网络及底层IO的C++编程库,它使用现代C ...
- 大到可以小说的Y组合子(零)
问:啊!我想要一个匿名的递归… 答:Y(音同Why)… … … 问:作为一位命令式语言的使用者,为什么会突然折腾起Y组合子呢? 答:的确,这事儿要从很久以前的几次搁浅开始说起…上学的时候,从来没有接触 ...
- ArcEngine颜色可视化
AE中利用.NET中的ColorDialog对话框,将color对象转化为ArcEngine中的IRgbColor (1)在实现颜色选择之前,需定义这两种颜色之间的转换函数 //Color转换为Rgb ...
- Sublime 学习记录(二) package control 组件
i. 按Ctrl + ` 调出console (如果有QQ输入法会有冲突需要关闭热键) ii. 粘贴以下代码到底部命令行并运行 import urllib.requ ...
- c# 调用 友盟api
今天要使用友盟的推送API来给我的app进行推送信息,调试了好久,老是返回500错误,最终在友盟的技术人员支持下完成了此操作,在此多谢友盟技术和客服人员. 把发方法和注意事项贴出来供大家参考. pub ...
- mysql忘记密码时如何修改root用户密码
1.关闭正在运行的MySQL服务. 2.打开DOS窗口,转到mysql\bin目录. 3. 输入mysqld --skip-grant-tables 回车.--skip-grant-tables 的意 ...
- 修改 “嗨加游-Prefix.pch” 或者 “嗨加游-Info.plist ” 方法
如果要更改Info.plist与Prefix.pch文件实际路径,也就是实际文件的位置(不是在工程中的组织路径),需要到Build Settings中修改对应的配置,不然工程就找不到对应的Info.p ...
- iconv编码转换指令
看到一个不错的指令iconv,可以对文件编码进行转换,记录如下: iconv --list 列出所有支持转换的编码 icon -f code1 -t code2 filename -o newfile ...