Number theory-Funny scales (Spoj-scale)

Source: Internet
Author: User

Funny Scales problem ' s Link

----------------------------------------------------------------------------

Mean:

Given two numbers n and X, there is a balance, initially Zopan X, you need to select some numbers from the following set to two disks, making two disks equal (note: Each number can only be taken once).

Analyse:

That

Turn X into 3 binary:

But the topic says that each 3^i must be 1, so we need to change the coefficients of each of the equations represented by X to 1, which is the key.

Method:

    • In the process of making X into 3, each item is set to {0,1,2}, and when it encounters 2 o'clock, 2 is represented as 3-1, that is: move forward, this position bit-1.

As a result, the problem of repetition of coefficients is well dealt with, and when the final statistic is answered, the set can be divided according to the symbols of the coefficients.

Time complexity:o (N)

 

View Code

#include <cstdio>
Const int MAXL = -;

intN, X;
int A[MAXL], LenA;
intB[MAXL], LenB;
int Dig[MAXL];

int Main()
{

scanf("%d%d", &N, &X);

for(int I = 0;X != 0;I++)
{
Dig[I] += X % 3;
if(Dig[I] > 1)
{
Dig[I + 1]++;
Dig[I] = Dig[I] - 3;
}
X /= 3;
}

for(int I = MAXL - 1;I >= 0;I--)
if(Dig[I] != 0)
if(Dig[I] == -1)
A[LenA++] = I + 1;
Else if(Dig[I] == +1)
B[LenB++] = I + 1;

if(A[0] >N | | B[0] >N
printf("-1\ n");
Else
{
for(int I = LenA - 1;I >= 0;I--)
printf(I ? "%d" : "%d", A[I]);
printf("\ n");
for(int I = LenB - 1;I >= 0;I--)
printf(I ? "%d" : "%d",B[I]);
printf("\ n");
}
return 0;
}

Number theory-Funny scales (Spoj-scale)

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.