Editorial for COCI '16 Contest 7 #4 Poklon


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Let S be the total mass of the weights after balancing the scale. Then the total mass of the weights on the beams of the large scale is equal to S/2, and the sum of the weights on the beams of these scales is equal to S/4, and so on. Generally, a beam of a scale at depth r holds a load of mass S/2^r.

Since some beams (at depth r) already have a weight of mass m, additionally the following inequality must hold: S/2^r \ge m. Finally, we have S = \max\{m \times 2^r\} for each weight of mass m, that is at depth r.

We are left with efficiently comparing the numbers of the form a \times 2^b, where a and b are small enough to fit in a 32-bit data type. Since these numbers in binary notation have at most 32 ones (multiplication with 2^b corresponds to left shift b times), it is sufficient to just linearly iterate over the ones in the binary notation of these numbers.


Comments

There are no comments at the moment.