进制转换

C. Vanya and Scales
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

Examples
inputCopy
3 7
outputCopy
YES
inputCopy
100 99
outputCopy
YES
inputCopy
100 50
outputCopy
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.

THINK:
不管砝码是放在哪一个盘子里,最后都是用进制数表示这个数,即把这个数进行进制转换;比如3 7,最后就是把7转换成3进制;在这个题中的意思是如果是w-1,那就是说明这个砝码要用w-1个,(像7转换成3进制后是21,这里的21的意思是3^0的系数是1,3^1的系数是2),但是每个砝码只能用1次,所以这里的3^1的系数就要进一位,进一位的意思是在右边盘子里再加一个3^1的砝码,这样进完位之后,a[i+1]++,本位就要变成0(这一步比较关键,因为他影响到下一位,必须进行操作)由于右边的盘子里相同的砝码也只能用一次,所以只能是w-1的情况进一,w-2就不行,由于加一的影响会导致下一位可能是w,所以w的时候也要加一,最后所有的数只能是1或者0,如果有其他的数就说明没有办法表示;这样说来这个题其实就是进制转换的模拟了;模拟的过程是如果当前的数是0,就把相应的砝码放到7那个盘子里,反之如果是1就放在另一个盘子里,就构造出来了等式;
操作的话先要从后面开始,所以要先把这些数逆转到一个数组里面

#include<stdio.h>
#include<stdlib.h>
int a[20],b[20];
int main()
{
    long long int n;long long int m;
    scanf("%lld%lld",&n,&m);
    int t=0;
    while(m!=0)
    {
        a[++t]=m%n;
        m=m/n;
    }
    for(int i=t;i>=1;i--)
    {
        b[i]=a[i];
    }
    int f=0;
    for(int i=1;i<=t;i++)
    {
        if(b[i]==n-1)
        {
            b[i+1]++;
            b[i]=0;
            f=1;
        }
        else if(b[i]==n)
        {
            b[i+1]++;
            b[i]=0;
            f=1;
        }
        else if(b[i]<=1){f=1;continue;}
        else {f=0;break;}
    }
    if(f==0)printf("NO\n");
    else printf("YES\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值