Editorial for COCI '11 Contest 2 #4 KompičI


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 thing to notice is that for each of the input values, only the set of its digits is of importance. We are not interested in the order in which digits appear or the repetition of digits. Therefore, each value can be represented with a sequence of 10 binary digits - 1 if that digit is present, and 0 if it isn't.

There are at most 2^{10} = 1024 different sequences. For each sequence, we can easily calculate how many input values yield exactly that sequence, and store these results into some array.

For each pair of sequences, it's easy to tell if they share some digit - they do if there is a position at which both sequences have ones. If they don't share a digit, there are no pals here. If they do, then we can form a pair of pals by choosing any value that yields the first sequence, and any value that yields the second sequence. The total number of such pairs is:

number_of_values[sequence1] * number_of_values[sequence2]

Finally, we must count the number of pals that have the same sequence:

number_of_values[sequence] * (number_of_values[sequence] - 1) / 2

We must go through every possible pair of sequences, so complexity is \mathcal O(1024^2).


Comments

There are no comments at the moment.