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


### 回答1: 将16进制图片转换为jpg图片的过程可以使用编程语言来实现。首先,我们需要将16进制字符串转换为字节流。在Python中,可以使用`bytes.fromhex()`函数将16进制字符串转换为字节流。然后,我们将字节流写入到文件中,文件的扩展名为jpg。最后,我们可以使用图片查看器来打开生成的jpg图片。 以下是一个示例Python代码实现: ```python # 导入所需模块 import binascii # 16进制字符串 hex_str = "FFD8FFE000104A46494600010100000100010000FFDB0043000C08080C07080F0E0D100F0E1A134D1E1706001A1A1A271D2B262A262B303634464066484C4B40434C6E5C373B496A796F4A26592E474143404EAC080283ADECBD60FD0F50D608F5C81E3078FBFD52BA9F9CF3D383A4F79C82F0348481F1224B163A5555475E818CBD9671A295062EB195535929D4E05E06B0FF6784E7EAEBCCE100983EDFCDDD0F2B542B4321427A5790483D8BCA155186313A4D34B9D5C016952E6AB7CD2B0CE6D6C9E791A175E4E134248AD0715089CCFF5E3AE4A24DAB6327977D22706BC48DD6D75542976AC242485905246617EEAFDBB76ACEFE1A628B7CDDF9C2A8249983454658591EFB0D0705A131AB5A1853C0FF1C64CCACFA0C18E3A30AF2409A5202B6B22216B73AD4B36F6A51AA105B590FAD064A81C13A905D043AA94083AAEB570487B8081F65604654659467D265498F9EE420AE6342FAC65CCEBE5843BB070A3AB550A06306D2526157385304E54CB6BE74B917699FB6C9EC0627D7571B95050D8C40F404172E6C7C77032E39D251D1EA43360ADAE852F1E845AC1AD3EDB4E5FAAD529AAAA1910882FB34F6523F7C459C929B715D7C6B8E18D54C9FF01DFA5A2A79B5022F8AA8F4A209854A51A89C204D63AC2E43C78700136AA181F41F271AFC055732121B3D5928F656264181788AF2051C7072C66598B4E55DFD4DF0E30A491C2D84E1400AAEEA644D8292A3F5E12C71AEEE02DE5BC30001FFD9" # 将16进制字符串转换为字节流 byte_stream = binascii.unhexlify(hex_str) # 将字节流写入文件,扩展名为jpg with open("image.jpg", "wb") as f: f.write(byte_stream) print("图片转换完成!") ``` 执行以上代码后,它会将16进制字符串转换为jpg图片,并保存为名为"image.jpg"的文件。我们可以在文件浏览器中找到并打开这个图片文件。 ### 回答2: 将16进制图片转换为jpg格式的图片需要进行以下步骤: 1. 将16进制数据转换为二进制数据:由于计算机中存储的数据是以二进制形式存储的,所以需要将16进制数据转换为二进制形式。 2. 按照图片的格式进行解码:16进制图片可能有不同的格式,比如BMP、PNG等,需要根据实际情况进行解码,将其还原为原始的图像数据。 3. 将解码后的图像数据保存为jpg格式的文件:使用适当的图像处理工具或编程语言,将解码后的图像数据保存为jpg格式的文件。 4. 调整图像的大小和质量(可选):根据需要,可以对图像进行调整,包括大小和质量等方面的调整。 5. 最后保存jpg格式的图片:将调整后的图像保存为jpg格式的图片文件。 总结起来,将16进制图片转换为jpg格式的图片需要先将16进制数据转换为二进制数据,再进行解码和保存为jpg格式的文件。通过这些步骤,就能成功地将16进制图片转换为jpg格式的图片。 ### 回答3: 将16进制图片转换为jpg图片需要经过以下几个步骤。 首先,需要将16进制图片转换为二进制数据。在计算机中,图片的存储是以二进制形式进行的。而16进制则是一种表示二进制的方法,每个16进制数对应四位二进制数。所以可以将16进制图片中的每个16进制数转换为对应的四位二进制数,然后将所有的二进制数连在一起,就得到了图片的二进制数据。 接下来,需要了解16进制图片的长宽和颜色信息。通常,16进制图片的颜色信息是以RGB(红绿蓝)格式存储的。每个像素点都有对应的RGB数值,表示该点的颜色。另外,还需要知道图片的长宽信息,以确定像素的排列方式。 然后,根据二进制数据和颜色信息,可以构造一张新的jpg图片。jpg是一种常见的图片格式,支持高压缩率和无损压缩。可以使用图形处理软件或者编程语言来实现这一步骤。根据图片的长宽信息,将二进制数据按照像素点的排列方式填充到对应的位置,并根据颜色信息为每个像素点赋予相应的颜色值。 最后,保存新生成的jpg图片。可以选择将图片保存到计算机的磁盘中,或者直接输出到屏幕上。这样就完成了将16进制图片转换为jpg图片的过程。 总结起来,将16进制图片转换为jpg图片需要将16进制转换为二进制数据,获取颜色和长宽信息,构造新的jpg图片,最后保存生成的jpg图片。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值