CF 552C 进制转换
http://codeforces.com/problemset/problem/552/C
1 second
256 megabytes
standard input
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.
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.
Print word 'YES' if the item can be weighted and 'NO'
if it cannot.
3 7
YES
100 99
YES
100 50
NO
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 进制转换的更多相关文章
- SQL Server 进制转换函数
一.背景 前段时间群里的朋友问了一个问题:“在查询时增加一个递增序列,如:0x00000001,即每一个都是36进位(0—9,A--Z),0x0000000Z后面将是0x00000010,生成一个像下 ...
- [No000071]C# 进制转换(二进制、十六进制、十进制互转)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- JS中的进制转换以及作用
js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现: //10进制转为16进制 ().toString() // =>&q ...
- 结合stack数据结构,实现不同进制转换的算法
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...
- 进制转换( C++字符数组 )
注: 较为简便的方法是用 整型(int)或浮点型(long.double 注意:该类型不一定能够准确存储数据) 来存放待转换的数值,可直接取余得到每一位数值 较为稳定的方法是用 字符数组储存待转换的数 ...
- JS 进制转换
十进制转换成其他进制 objectname.toString([radix]) objectname 必选项.要得到字符串表示的对象. radix 可选项.指定将数字值转换为字符串时的进制. 例如 ...
- php的进制转换
学习了php的进制转换,有很多的知识点,逻辑,也有最原始的笔算,但是我们还是习惯使用代码来实现进制的转换,进制的转换代码有如下:二进制(bin)八进制( oct)十进制( dec)十六进制( hex) ...
- C++ 中数串互转、进制转换的类
/******************************************************************** created: 2014/03/16 22:56 file ...
- 【String与基本类型之间的转换】以及【进制转换】
1. 基本数据类型---->字符串类型: 方法一:使用连接一个空字符串,例如 基本数据类型+“” : 方法二:静态方法 String.valueOf(),具体有: String.valueOf ...
随机推荐
- 【Web】CXF WebService 服务端和客户端 环境搭建及测试
cxf服务端 1.去官方下载对应的jar包:http://cxf.apache.org/ 2.maven配置相应jar包 3.修改web.xml,完成spring和cxf配置 <!-- Spri ...
- Python相关项目和技术
下面的项目是<Learn PYTHON the hard way>里面的,以后可能会补充: 1.Django,创建web程序的框架:https://www.djangoproject.co ...
- javascript:void(0)的作用示例
在做页面时,如果想做一个链接点击后不做任何事情,或者响应点击而完成其他事情,可以设置其属性 href = "#",但是,这样会有一个问题,就是当页面有滚动条时,点击后会返回到页面顶 ...
- 使用SourceTree Push 出现 POST git-receive-pack (chunked) 的解决方法
在使用SourceTree上传资料的时候,遇到 POST git-receive-pack (chunked) 从 stackoverflow 看到这样一则 This is a bug in Git; ...
- Codeforces 490B Queue【模拟】
题意还是很好理解的,根据题目给出描述条件然后求出这串QUEUE 我的做法就是用两个数组 before[] 和 after[] 表示 ai 前面的前面的人的学号 和 ai 后面的后面的人的学号 ex[] ...
- 原生弹窗拖拽代码demo+简单的抽奖
拖拽效果 效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- zookeeper perl 版本需求
[root@wx03 ~]# perl -v This is perl 5, version 22, subversion 1 (v5.22.1) built for x86_64-linux Cop ...
- 基于visual Studio2013解决C语言竞赛题之0602最大值函数
题目
- 基于visual Studio2013解决C语言竞赛题之0523魔方阵
题目
- qq邮箱是怎么做到同一个浏览器让多个不用用户同时打开的? --session的控制
待解:..... 借鉴网址:http://www.zhihu.com/question/20235500 欢迎来讨论.....