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)