Editorial for COCI '19 Contest 5 #5 Zapina


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 NN possible task assignments in total.

The second subtask can be solved using the inclusion-exclusion principle. If with a(S), where S{1,2,,N}, denotes the number of task assignments in which all programmers from set S are happy, then the number of good assignments is equal to

S{1,2,,N}(1)|S|+1a(S)

Let S={s1,s2,,sk}. If s1+s2++sk>N then obviously a(S)=0. Otherwise, it holds

a(S)=(Ns1)(Ns1s2)(N(s1+s2++sk1)sk)(Nk)N(s1+s2++sk)

Binomial coefficients can be precomputed using the well-known relation (nk)=(n1k1)+(n1k) in time complexity O(N2).

The total time complexity is O(N2N).

Finally, the third subtask can be solved using dynamic programming. Let dp(n,k) be the number of assignments of k tasks to n programmers among which there is at least one happy programmer. There are two possibilities – we can give the nth programmer exactly n tasks (and make him happy), or we won't. In the first case, if kn, the number of ways equals

(kn)(n1)kn

and in the other it equals

0ik,in(ki)dp(n1,ki)

The total time complexity is O(N3).


Comments

There are no comments at the moment.