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;
}

目录
相关文章
|
机器学习/深度学习 算法 搜索推荐
从理论到实践,Python算法复杂度分析一站式教程,助你轻松驾驭大数据挑战!
【10月更文挑战第4天】在大数据时代,算法效率至关重要。本文从理论入手,介绍时间复杂度和空间复杂度两个核心概念,并通过冒泡排序和快速排序的Python实现详细分析其复杂度。冒泡排序的时间复杂度为O(n^2),空间复杂度为O(1);快速排序平均时间复杂度为O(n log n),空间复杂度为O(log n)。文章还介绍了算法选择、分而治之及空间换时间等优化策略,帮助你在大数据挑战中游刃有余。
382 3
|
JSON 小程序 数据安全/隐私保护
小程序动态调试-解密加密数据与签名校验
本文主要讲解微信小程序加密、验签的情况下如何进行动态调试已获取签名以及加密信息
|
Java Go 调度
Go Runtime功能初探
Go Runtime功能初探
205 2
|
JavaScript Java 测试技术
基于Java的法律咨询系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的法律咨询系统的设计与实现(源码+lw+部署文档+讲解等)
208 0
|
测试技术 Python
一道简单的Python面试题,却涵盖诸多考点,快来试试吧!
一道简单的Python面试题,却涵盖诸多考点,快来试试吧!
183 0
|
SQL 关系型数据库 MySQL
Mysql 12 复制1
<div class="markdown_views"> <p>备份,灾难恢复,大规模,水平扩展,高性能的基础,也是高可用,可扩展,及数据仓库的基础。</p> <h2 id="简述">简述</h2> <h3 id="开销">开销</h3> <p>Mysql有两种复制方式:基于行的复制和基于语句的复制。 <br> 都是通过在主库上记录二进制日志,在备库上重放日志实现
1830 0
|
Android开发
安卓开发----TextView控件属性列表(转)
文章原地址: http://wwzcraig.blog.163.com/blog/static/64622969201373184343118/   android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。
955 0