CodeForces - 552C Vanya and Scales (进制转换&技巧)好题
来源:互联网 发布:厦门拓羽网络 编辑:程序博客网 时间:2024/06/15 05:24
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
YES
100 99
YES
100 50
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
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#define ll long long#define N 20using namespace std;int a[N];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]++;}elseflag=0;}if(flag)printf("YES\n");elseprintf("NO\n");}return 0;}
- CodeForces - 552C Vanya and Scales (进制转换&技巧)好题
- CodeForces 552C:Vanya and Scales【技巧】
- codeforces 552C Vanya and Scales(进制转化)
- Codeforces Round #308 (Div. 2) C. Vanya and Scales (数学常识,进制转换)
- C. Vanya and Scales (进制转换)
- Codeforces 552C Vanya and Scales
- codeforces 552 C Vanya and Scales
- [CodeForces 552C]Vanya and Scales[math]
- CodeForces 552C-Vanya and Scales【思维】
- Codeforces 552C. Vanya and Scales【巧】
- CodeForces 552C Vanya and Scales
- codeforces 552C Vanya and Scales 【思维】
- 【30.23%】【codeforces 552C】Vanya and Scales
- CodeForces 552C. Vanya and Scales(进制+思维)
- cf#308-C - Vanya and Scales- 数学/进制转换
- codeforces #308C C. Vanya and Scales(数制转换)
- Codeforces Round #308 (Div. 2) C. Vanya and Scales(数制转换)
- C. Vanya and Scales(Codeforces Round #308 (Div. 2))
- 程序员装逼速成手册-----嘻嘻,感觉亮点在最后呀啦啦啦啦~~~
- CF116D 一开始往右扫,每换下一行要换方向扫,问扫完草至少经过的步数
- 数据结构求一个矩阵的马鞍点
- 初识Python
- Java 工程师成神之路
- CodeForces - 552C Vanya and Scales (进制转换&技巧)好题
- 使用 P3P 规范让 IE 跨域接受第三方 cookie
- Scala进阶源码实战之六——类型变量
- hive inser into 与 hive overwrite区别
- Spark SQL
- 自定义排序数组二分法插入实现
- 趣味素数-素数 java
- Hibernate中的int和 Serializable
- java中常用的集合极其特点