Editorial for COCI '15 Contest 6 #4 Parovi
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.
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's denote the family of sets that have the partition located at index as , and the family of all possible initial sets as .
Then the solution is all sets from .
The inclusion-exclusion principle provides us with an efficient way of calculating the size of the solution set. It holds:
In order to calculate the cardinality of the required intersection, we need to count how many pairings such that the partitions are located at . We are not interested in partitions at other locations.
That number is equal to , where is the total number of pairs that don't "cross over" any partition location. The time complexity of this solution is .
Please note: A solution using dynamic programming also exists and is left as an exercise to the reader.
Comments