codeforces C. Vanya and Scales

                        C. Vanya and Scales

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 test(s)

input

3 7

output

YES

input

100 99

output

YES

input

100 50

output

NO

Note

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.

题意:给定两个数,m和w,现有w^0, w^1, w^2...w^100各一个重量的mass, 用其中的一些mass,时候能称出m的重量。

也就是有如下图的式子:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
    int m, w;
    cin>>w>>m;
    while(m){
        if(!((m-1)%w)) m--;
        else if(!((m+1)%w)) m++;
        else if(m%w){
            cout<<"NO"<<endl;
            return 0;
        }
        m/=w;
    }
    cout<<"YES"<<endl;
    return 0;
}
时间: 2024-10-25 01:56:23

codeforces C. Vanya and Scales的相关文章

CodeForces 552C Vanya and Scales

Vanya and Scales Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 552C Description Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some

Codeforces 552C Vanya and Scales(进制转换+思维)

题目链接:http://codeforces.com/problemset/problem/552/C 题目大意:有101个砝码重量为w^0,w^1,....,w^100和一个重量为m的物体,问能否在天平两边放物品和砝码使其平衡.解题思路:将m化为w进制的数,接下来从低到高遍历没一位:如果第i位是0那OK:如果是1那就要把砝码wi放在天平另一边抵消:如果是w-1那就要把砝码wi放到天平同一边,使其变为0并进位,这时第i+1位的权+1:而如果是其他情况,那么肯定不能平衡了. 代码: 1 #incl

Codeforces 552C Vanya and Scales(思路)

题目大概说有101个质量w0.w1.w2.....w100的砝码,和一个质量m的物品,问能否在天平两边放物品和砝码使其平衡. 哎,怎么没想到..注意到w0.w1.w2.....w100—— 把m转化成w进制数,枚举每一位: 如果第i位是0那OK: 如果是1那就要把砝码wi放在天平另一边抵消: 如果是w-1那就要把砝码wi放到天平同一边,使其变为0并进位,这时第i+1位的权+1: 而如果是其他情况,那么肯定不能平衡了. 1 #include<cstdio> 2 #include<cstri

暴力/进制转换 Codeforces Round #308 (Div. 2) C. Vanya and Scales

题目传送门 1 /* 2 题意:问是否能用质量为w^0,w^1,...,w^100的砝码各1个称出重量m,砝码放左边或在右边 3 暴力/进制转换:假设可以称出,用w进制表示,每一位是0,1,w-1.w-1表示砝码与物品放在一起,模拟判断每位是否ok 4 详细解释:http://blog.csdn.net/u011265346/article/details/46556361 5 总结:比赛时压根没往进制去想,连样例也不知道是怎么回事..中文不行啊:( 6 */ 7 #include <cstdi

codeforces 492E. Vanya and Field(exgcd求逆元)

题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走了n步后,x从0~n-1,y从0~n-1都访问过,但x,y不相同. 所以,x肯定要经过0点,所以我只需要求y点就可以了. i,j为每颗苹果树的位置,设在经过了a步后,i到达了0,j到达了M. 则有 1----------------------(i + b * dx) % n = 0 2------

Codeforces 492E Vanya and Field(拓展欧几里得)

题目链接:Codeforces 492E Vanya and Field 通过拓展欧几里得算法求出每个位置在移动过程中,在x为0时,y的位置.统计相应y坐标最多的即为答案. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1e6 + 5; const int maxm = 1e5 + 5; typedef long long l

codeforces 552 C Vanya and Scales

这个题的意思就是给出一个数m.以及一个以1为首元素.w为比例常数的等比数列,数列长度为101,数列中每一个数字最多仅仅能用一次.问是否存在xa+wb+--=wc+wd+--+we+m. 非常显然,换句话说就是问,是否存在m=wa+wb+--+wf-wc-wd----we.再进行化简就能够得到.是在问,是否存在m=((((wh±1)wi±1)±1)wj±1)wk--. 那么非常显然可以进行搜索,比方说用广搜,每次当前的m±1中若有因子w就所有约掉,然后丢到队列中,否则就不用管,仅仅要队列中可以出现

CodeForces - 552E Vanya and Brackets

Vanya and Brackets Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Description Vanya is doing his maths homework. He has an expression of form , where x1, x2, ..., xn are digits from 1 to 9, and sign represents either a p

Codeforces 552E Vanya and Brackets(贪心 + 表达式计算)

题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左端或者某个乘号右边,右括号的位置肯定是最右段或者某个乘号左边. 而乘号最多只有15个,那么暴力枚举就可以了. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b);