Problem description.
The idea to use a bitmask to represent the sets is from this page, as is the clever way to test rule 2.