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. 非常显然,换句 ...
随机推荐
- Android OpenGL库加载过程源码分析
Android系统采用OpenGL绘制3D图形,使用skia来绘制二维图形:OpenGL源码位于: frameworks/native/opengl frameworks/base/opengl 本文 ...
- Python 获取Facebook用户Friends的爱好类别中的Top10
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...
- R6010 -abort() has been called错误分析及其解决方法
近期使用vs2010编程出现下面问题.在网上收集了大家的意见之后,整理了一下 导致出现这种原因有: 1.非法指针訪问和内存泄漏 2.大家再查查吧.一定是指针出现故障了.设置的指针范围跟你执行的不正确 ...
- [SASS] Make a responsive arrow box
Check the page:http://www.cssarrowplease.com/ In HTML: {{type}} is tow way binding in Angular, three ...
- 《JavaScript 闯关记》之 DOM(上)
DOM(文档对象模型)是针对 HTML 和 XML 文档的一个 API.DOM 描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的某一部分. 节点层次 DOM 可以将任何 HTML 或 XM ...
- 使用repo的本地开发流程
repo下的本地开发流程 单分支开发: 1 本地新建工作目录并初始化repo库: repo init; 2 下载代码(只取服务器当前分支): repo sync -c; 3 创建本地 ...
- 使用react-native做一个简单的应用-05 navigator的使用
今天生病了,难受... Navigator顾名思义就是导航器的意思. 在Android和iOS中,导航器的样式是不一样的 ,有一个比较好的网址可以提供大家学习(网址),由于样式的不统一,于是我决定自己 ...
- Common Table Expressions (CTE)
子查询有时使用起来很麻烦,因为所有的过滤和匹配逻辑都必须集成到子查询表达式中.如果只需要执行一个任务,且只需要使用一次杳询表达式,子查询是很好的选择.但子查询不能被重用,也不能很好地支持多个需求.这个 ...
- SQL日期形式转换
在SQL Server中,有时存储在数据库中的日期格式和我们需要显示在页面上的格式不相同,我们需要转化成需要的格式. 特在此总结了一下常用的日期格式. --当前时间 SELECT GETDATE(); ...
- log4j参数说明
log4j.properties 使用 一.参数意义说明 输出级别的种类 ERROR.WARN.INFO.DEBUG ERROR 为严重错误 主要是程序的错误 WARN 为一般警告,比如session ...