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 subtask can be solved by simply checking if there is at least one happy programmer for each possible assignment of tasks. There are
possible task assignments in total.
The second subtask can be solved using the inclusion-exclusion principle. If with
, where
, denotes the number of task assignments in which all programmers from set
are happy, then the number of good assignments is equal to

Let
. If
then obviously
. Otherwise, it holds

Binomial coefficients can be precomputed using the well-known relation
in time complexity
.
The total time complexity is
.
Finally, the third subtask can be solved using dynamic programming. Let
be the number of assignments of
tasks to
programmers among which there is at least one happy programmer. There are two possibilities – we can give the
programmer exactly
tasks (and make him happy), or we won't. In the first case, if
, the number of ways equals

and in the other it equals

The total time complexity is
.
Comments