CF 552C 进制转换

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


### 汇编语言中实现十六进制到二进制的转换 在汇编语言中,十六进制数可以直接映射为四位一组的二进制数。这是因为每一位十六进制数字可以表示从`0`到`F`(即`0`至`15`),这正好可以用四个二进制位来表达。因此,在实际编程过程中,通常不需要编写复杂的算法来进行这种转换;而是通过查表法或是简单的移位操作完成。 对于具体的编码实践而言,如果要显示一个十六进制数值对应的二进制形式给用户看,则可以通过循环读取每一个字节的数据并将其拆解成两个半字节分别处理的方式达成目标。下面给出一段示范性的8086汇编代码片段用于展示这一过程: ```assembly ; 假设DX寄存器里有一个我们要转成二进制字符串输出的16位无符号整型变量 MOV CX, 16 ; 设置计数器CX=16准备逐位处理 LEA SI,BUFFER ; BUFFER是一个缓冲区首地址指针 NEXTBIT: SHL DX, 1 ; 将DX左移一位使最高位置零或置一以便测试 RCL AL, 1 ; 把CF标志位(上一步产生的)加入AL最低位同时更新CF ADD AL,'0' ; 转换成ASCII码方便打印 MOV [SI],AL ; 存入缓存区等待后续统一输出 INC SI ; 缓冲区指针前进指向下一个待填充的位置 LOOP NEXTBIT ; 继续直到全部16个bit都被处理完毕 ``` 上述程序段展示了如何把保存于寄存器内的一个双字节数值按位展开成为一系列字符‘0’和‘1’组成的串[^1]。需要注意的是这段代码仅作为概念验证用途,并未考虑诸如错误检测等功能细节以及优化空间等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值