Editorial for WC '18 Finals S3 - Screen Time


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.

We'll define a type-i scene as one excluding actor i (1 \le i \le 3). Let T_i be the total number of type-i scenes, and C_i be the number of type-i scenes included in the chosen subset. A subset is valid if and only if the following hold:

  • 0 \le C_i \le T_i, for each i,
  • C_1 < C_2 (equivalent to S_1 > S_2), and
  • C_1 < C_3 (equivalent to S_1 > S_3).

The number of different subsets with a given set of C values is equal to the product \prod_{i=1}^3 \binom{T_i}{C_i}. Before going further, let's consider how we can efficiently evaluate choose values in general (modulo a prime P such as 1\,000\,000\,007). We'll first let \text{ModInv}(v, p) be the modular inverse of v for prime modulus p, which is equal to v^{p-2} \bmod p.

\text{ModInv}(v, p) may be evaluated in \mathcal O(\log p) time using either exponentiation by squaring or the extended Euclidean algorithm. Then, we have:

\displaystyle \binom n k \bmod P = \frac{n!}{k!(n-k)!} \bmod P = \left[(n! \bmod P) \times \text{ModInv}(k!, P) \times \text{ModInv}((n-k)!, P)\right] \bmod P

If we precompute x! \bmod P and \text{ModInv}(x!, P) for each x between 0 and N, inclusive, then we'll be able to evaluate any necessary choose value in constant time. Now, let Ways[i][j] = \left[\sum_{k=j}^{T_i} \binom{T_i}{k} \right] \bmod P – in other words, the number of size-j-or-greater subsets of type-i scenes.

Note that Ways[i][j] = \left[Ways[i][j+1]+\binom{T_i}{j}\right] \bmod P, meaning that we can precompute Ways[i][j] for all possible pairs (i, j) in \mathcal O(N) by iterating downwards through the j values.

Let's consider fixing the number of type-1 scenes in the chosen subset. For a given C_1, there are Ways[2][C_1+1] subsets of type-2 scenes such that C_2 > C_1 is satisfied, and similarly Ways[3][C_1+1] subsets of type-3 scenes. Therefore, the total number of valid subsets which can be chosen with a given C_1 is \left[\binom{T_1}{C_1} \times Ways[2][C_1+1] \times Ways[3][C_1+1]\right] \bmod P, which can be added up over all \mathcal O(N) possible choices of C_1 to yield the final answer.


Comments

There are no comments at the moment.