Official

C - LU / RD Marking Editorial by evima


Hints: https://atcoder.jp/contests/arc166/editorial/7374


[1] Problem decomposition

We divide the edges in the grid into several components so that two edges that can be operated on at the same time belong to the same component, as shown in the following figure:

For each component, the set of edges that can be marked is determined independently, so the answer to this problem can be calculated as the product of the answers for each component.


[2] Solution for each component

For each component, we need to solve the following problem:

There are \(n\) edges in a row. You can perform an operation to mark two consecutive edges that have not yet been marked. Find the number of possible sets of edges that are eventually marked.

Let \(a_n\) denote the answer to this problem. When \(n\geq 2\),

  • if you do not mark the leftmost edge, you can make \(a_{n-1}\) sets;
  • if you mark the leftmost edge, you can make \(a_{n-2}\) sets.

By considering these two cases, we obtain the recurrence relation \(a_n = a_{n-1}+a_{n-2}\), and \(\{a_n\}\) can be calculated in \(O(1)\) time per term using this recurrence relation (this is a famous sequence called the Fibonacci sequence).


[3] Solution for the original problem

If you properly arrange the calculation formula, the answer to this problem can be expressed in the form:

\[(a_2a_4\cdots a_{2n})^2a_{2n+1}^m.\]

Considering the recurrence relation in [2], the answer can be calculated in \(\Theta(H+W)\) time per test case, but this is likely to result in TLE under the constraints of this problem.

However, improving the computational complexity of this part is easy. You just need to precompute the values of \(a_2a_4\cdots a_{2n}\) and \(a_{2n+1}\) for each \(n\), independently of individual test cases.

You can solve it in \(\Theta(\max(H_i, W_i))\) time for precomputation and \(\Theta(\log(|H-W|))\) time per test case, which is sufficiently fast.

posted:
last update: