Official

E - Paper Cutting 2 Editorial by evima


If the smallest rectangle containing the two black squares does not change, the answer does not change, so we assume \(h_1 \leq h_2, w_1 \leq w_2\) without loss of generality. If we let \(A, B, C, D\) be the number of edges that can be cut above, below, to the left of, to the right of the rectangle, and let dp[\(A\)][\(B\)][\(C\)][\(D\)] be the expected number of cuts from such a situation, we can compute the answer in \(\mathrm{O}(L^4)\), but it is too slow.

Let us rephrase the problem into the following problem. We have:

  • \(A\) red balls with \(1, 2, \cdots, A\) written on them

  • \(B\) yellow balls with \(1, 2, \cdots, B\) written on them

  • \(C\) green balls with \(1, 2, \cdots, C\) written on them

  • \(D\) brown balls with \(1, 2, \cdots, D\) written on them

  • \(E = (h_2 - h_1) + (w_2 - w_1)\) red balls with \(1, 2, \cdots, E\) written on them

We will randomly rearrange these balls in a row. What is the expected value of the index (position) of the first black ball?

Actually, the answer to this does not equal the answer to the original problem, since the piece without the black squares will be thrown away. To handle it, after the rearranging, we will ignore a ball if another ball in the same color with a smaller number occurs earlier in the sequence.

Intuitively, we can choose balls in the sequence from front to back and throw away balls that should be ignored without changing the probabilities for relevant balls.

In the end, from the linearity of expectation, we can find the expected value in question by summing up the probability that each pair of (color, value) occurs earlier than black balls to avoid being ignored.

For example, the probability for the pair (red, \(i\)) is the probability that it appears the earliest among (red, \(1\)), (red, \(2\)), \(\ldots\), (red, \(i\)) and blacks, so it is \(\frac{1}{i+E}\).

Therefore, after precomputing the modulo inverse of each \(i\) and accumulated sums in \(\mathrm{O}(L)\) time, we can find the answer in \(\mathrm{O}(1)\) time.

posted:
last update: