Editorial for COCI '11 Contest 2 #4 KompičI
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 binary digits - if that digit is present, and if it isn't.
There are at most 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 .
Comments