Editorial for COCI '11 Contest 1 #3 X3


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.

The first useful observation is that individual binary digits of the friendship value are mutually independent, so they can be considered separately. If the ith digit of a result is equal to 1, a value of 2i is added to the friendship value, otherwise 0 is added.

A digit of the result is equal to 1 only if the corresponding digits in extraterrestrial names differ. Since we are computing the total friendship value, we can count the number of appearances of 2i (digit 1 in position i) for each i.

Let us denote by Ki the number of extraterrestrials who have the digit 1 in position i in binary name notation. Then we add

(Ki×(NKi))×2i

to the sum of friendships, since that is exactly the number of pairs with differing digits in position i, multiplied by the weight of that digit position.


Comments

There are no comments at the moment.