51nod 1449 weight weighing balance/in-system

Source: Internet
Author: User
Test instructions
Give you w,n, ask you in w^0,w^1,w^2 ... all kinds of one, ask you can use these weights and the weight of things put on the balance of the balance to make balances;
Ideas:
This is easy to associate with the binary:
If you put M on the side, in fact, the weights are purely additive, can be expressed as such a binary, each bit is 0 or 1
So if both sides are going to put it.
So how am I going to use M and what I already have, to construct a binary number that's only 01
Why do you say that? Because the right side of the left is equivalent AH;

Then from low to high processing, to ensure the use of weights <=1;

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>pii;
const double eps=1e-5;
Const double Pi=acos ( -1.0);
const int mod=1e9+7;
const int inf=0x3f3f3f3f;

int w,n;
int bit[35];
int num;

void init ()
{
    num=0;
    while (n)
    {
        bit[num++]=n%w;
        n/=w;
    }
}

int main ()
{
    scanf ("%d%d", &w,&n);
    Init ();

    for (int i=0;i<num;i++)
    {
        if (bit[i]>=w)
        {
            bit[i]-=w;
            bit[i+1]++;
        }
        if (bit[i]<=1)   //0 is used, 1 is not
            continue;
        else if (bit[i]==w-1)    //and M side of +w^ (i-1) makes the other side as long as +w^i on the line;
            bit[i+1]++;
        Else
        {
            puts ("NO");
            return 0;
        }
    }
    Puts ("YES");
    return 0;
}







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.