http://codeforces.com/problemset/problem/552/C

C. Vanya and Scales
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

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.


/**
CF 552C 进制转换
题目大意:给定一个天平。砝码的重量为w的0~100次幂。每种砝码仅仅有一个,砝码能够放在左盘或者右盘。 给定物品的重量m。问是否有一种方案让天平两端平衡
解题思路:把m化为w进制。改进制数仅仅能有0,1或者w-1。若为w-1那么相当于在物品所放的盘里加一个砝码,然后在还有一盘加上w倍的砝码就可以。 直接w进制数当前位
清0,将下一位+1就可以。最后看w进制数是否正好是一个01串就可以
*/
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
using namespace std;
int bit[40],n,m;
int main()
{
while(~scanf("%d%d",&n,&m))
{
int k=0;
memset(bit,0,sizeof(bit));
while(m)
{
bit[k++]=m%n;
m/=n;
}
int flag=1;
for(int i=0;i<k;i++)
{
if(bit[i]>=n)
{
bit[i]-=n;
bit[i+1]++;
}
if(bit[i]==n-1)
{
bit[i]=0;
bit[i+1]++;
}
else if(bit[i]>1)
{
flag=0;
break;
}
}
if(flag==0)
puts("NO");
else
puts("YES");
}
return 0;
}

CF 552C 进制转换的更多相关文章

  1. SQL Server 进制转换函数

    一.背景 前段时间群里的朋友问了一个问题:“在查询时增加一个递增序列,如:0x00000001,即每一个都是36进位(0—9,A--Z),0x0000000Z后面将是0x00000010,生成一个像下 ...

  2. [No000071]C# 进制转换(二进制、十六进制、十进制互转)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. JS中的进制转换以及作用

    js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现: //10进制转为16进制 ().toString() // =>&q ...

  4. 结合stack数据结构,实现不同进制转换的算法

    #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...

  5. 进制转换( C++字符数组 )

    注: 较为简便的方法是用 整型(int)或浮点型(long.double 注意:该类型不一定能够准确存储数据) 来存放待转换的数值,可直接取余得到每一位数值 较为稳定的方法是用 字符数组储存待转换的数 ...

  6. JS 进制转换

    十进制转换成其他进制 objectname.toString([radix])   objectname 必选项.要得到字符串表示的对象. radix 可选项.指定将数字值转换为字符串时的进制. 例如 ...

  7. php的进制转换

    学习了php的进制转换,有很多的知识点,逻辑,也有最原始的笔算,但是我们还是习惯使用代码来实现进制的转换,进制的转换代码有如下:二进制(bin)八进制( oct)十进制( dec)十六进制( hex) ...

  8. C++ 中数串互转、进制转换的类

    /******************************************************************** created: 2014/03/16 22:56 file ...

  9. 【String与基本类型之间的转换】以及【进制转换】

    1. 基本数据类型---->字符串类型: 方法一:使用连接一个空字符串,例如  基本数据类型+“” : 方法二:静态方法 String.valueOf(),具体有: String.valueOf ...

随机推荐

  1. MCU开发之I2C通信

    程序状态字PSW是8位寄存器,用于存放程序运行的状态信息,PSW中各位状态通常是在指令执行的过程中自动形成的,但也可以由用户根据需要采用传送指令加以改变.各个标志位的意义如下: PSW.7(Cy):进 ...

  2. BestCoder Round #50 (div.1) 1001 Distribution money (HDU OJ 5364)

    题目:Click here 题意:bestcoder上面有中文题目 #include <iostream> #include <cstdio> #include <cst ...

  3. [POJ 2184]--Cow Exhibition(0-1背包变形)

    题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  4. 多线程之Future模式

    详细参见葛一名老师的<Java程序性能优化> Futrue模式:对于多线程,如果线程A要等待线程B的结果,那么线程A没必要等待B,直到B有结果,可以先拿到一个未来的Future,等B有结果 ...

  5. iPhone开发技巧之日志保存教程

    http://mobile.51cto.com/iphone-283337.htm Objective-C开发程序的时候,有专门的日志操作类NSLog,它将指定的输出到标准的错误输出上(stderr) ...

  6. django-model-utils

    一个普通例子: todos = Todo.objects.filter(owner=request.user).filter(is_done=False).filter(priority=1) 弊端: ...

  7. django1.6读书笔记一

    reporter是Article中的一个外键,我们可以有多篇文章指向同一个reporter,然后通过使用article_set.all()就可以返回其所有的headline了,也可以添加条件来筛选. ...

  8. django中tag的用法

    在app里建一个子的python包,包含__init__.py,包名为templatetags,里面新建一个tags.py(这个名字可以随意) from django import templater ...

  9. SD-关于定价日期的设置

    最近看了一篇关于定价日期的文章,我觉得写得很不错,特将自己的理解摘抄如下: 关于SD的定价日期在SAP系统中有三个配置与其相关,以及手工输入定价日期,具体如下: 1.订单类型的“定价日期建议“ 这个字 ...

  10. QScriptEngine

    其实你有好多没有介绍 比如qt文字 我一直很迷惑qt的文字的长宽 qt文字的字间距 等等这些东西还有QProcess QProcess可能是qt调用c#的唯一方法了QScript要比你想象的重要,一个 ...