CodeForces - 552C Vanya and Scales (进制转换&技巧)好题
2016-04-21 20:51
681 查看
CodeForces - 552C Vanya and Scales
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 Input 3 7 Output YES Input 100 99 Output YES Input 100 50 Output NO Hint 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. Source Codeforces Round #308 (Div. 2) //题意:输入w,m。 表示给你一个w,你可以使用w^0,w^1,w^2w^3.........w^100;这一百零一个数,现在又给你一个数m,让你将m放到天平的右边,现在问你能否通过在两边天平上添加那101个砝码使得天平平衡。 //思路: 将m转换成w进制数,再枚举每一位上的数,如果第i位为w或者是w-1(加一的话就可以进位)的话可以将它转换为0,第i+1位进1。这些位上的数如果全能转换成1或者0的话,就表示m可以被表示,否则不能被表示。 #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #define ll long long #define N 20 using namespace std; int a ; int main() { int w,m,n,i,j,k; while(scanf("%d%d",&w,&m)!=EOF) { k=0; while(m) { a[k++]=m%w; m/=w; } int flag=1; for(i=0;i<k;i++) { if(a[i]==w) { a[i]-=w; a[i+1]++; } if(a[i]<=1) continue; else if(a[i]==w-1) { a[i]=0; a[i+1]++; } else flag=0; } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; } |
相关文章推荐
- ppt制作干货
- vector 介绍
- 测试作业1
- Json和XML解析
- 解题报告:HDU4704Sum 费马小定理+快速幂
- 巴什博奕(Bash Game)
- websocket
- MySQL中group by max如何提取最大的一条记录
- java 读取properties + 类反射的应用
- JSP ffmpeg实现视频截图
- 为什么最近要弄CANopen以及资料来源
- 手把手教你使用Git(简单,实用)
- Array.asList()注意
- C#—异常应用(正则表达式)
- Java 重写(Override)与重载(Overload)
- CSS3动画
- 第七周---补
- 你最喜欢做什么?
- UVa 10006 - Carmichael Numbers
- Dism 错误 32