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 ...
随机推荐
-
Spring Boot项目中使用jdbctemplate 操作MYSQL数据库
不废话,先来代码 pom文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
-
搜集资料&;安装环境
开始入坑DSP,芯片是TI的TMS320C6678,硬件平台为TI官方的TMS320C6678L开发板. 官方给的资料有很多,各种用户手册.软件支持包,眼花缭乱.先尝试把开发环境搭起来,跑个Hello ...
-
分享自己配置的HttpURLConnection请求数据工具类
>>该工具类传入string类型url返回string类型获取结果import java.io.BufferedReader;import java.io.InputStream;impo ...
-
Atom 备份神器 —— Sync Settings
当 Atom 的扩展越装越多,配置也越来越偏离了系统默认设置时,一旦电脑重装后需要复原开发环境,这将是一件比较头疼的事,但今天发现一个扩展,可以解决这一问题,那就是「Sync Settings」,根据 ...
-
Zepto的天坑汇总
前言 最近在做移动端开发,用的是zepto,发现他跟jquery比起来称之为天坑不足为过,但是由于项目本身原因,以及移动端速度要求的情况下,也只能继续用下去. 所以在这里做一下汇总 对img标签空sr ...
-
Java String类中的intern()方法
今天在看一本书的时候注意到一个String的intern()方法,平常没用过,只是见过这个方法,也没去仔细看过这个方法.所以今天看了一下.个人觉得给String类中加入这个方法可能是为了提升一点点性能 ...
-
hdu 2489 最小生成树状态压缩枚举
思路: 直接状态压缩暴力枚举 #include<iostream> #include<algorithm> #include<cstdio> #include< ...
-
UVA 11178 Morley&#39;s Theorem(旋转+直线交点)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18543 [思路] 旋转+直线交点 第一个计算几何题,照着书上代码打 ...
-
python局域网alive ip侦听
python局域网alive ip侦听 作者:vpoet mails:vpoet_sir@163.com 注:写着玩,欢迎copy # -*- coding: cp936 -*- # coding = ...
-
LODOP、C-Lodop简短排查语句
https使用,故障:1.是https网站吗,https扩展版C-Lodop如何使用 参考http://www.c-lodop.com/faq/pp32.html2.双击桌面上的c-lodop快捷方式 ...