G - Golf 2 解説 by U_no_who


Consider two types of operations, which add vectors respectively:

  • Type 1: \((\pm A,\pm B)\);
  • Type 2: \((\pm B, \pm A)\);

Suppose the two types of operations are performed \(k_1\) and \(k_2\) times, respectively.

We can consider the signs separately. Assume the net displacement from the first type of operation is \((+x_1 \times A,+ y_1 \times B)\), with the constraints:

\[|x_1|,|y_1| \le k_1 \\ x_1 \equiv y_1 \equiv k_1 \pmod 2\]

Both are obvious, and the second type of operation follows similarly.

We require

\[\begin{cases} x_1A+y_1B=X \\ y_2A+y_1B=Y \end{cases}\]

At the same time, we want to minimize \(k_1+k_2\), which is equivalent to minimizing \(\max(|x_1|,|y_1|)+\max(|x_2|,|y_2|)\).

Note that we are solving a Diophantine system. Assume \(\gcd(A,B)=1\).

Using the extended Euclidean algorithm, we can find a particular solution for \(x_1,y_1,x_2,y_2\), and then express the general solution as

\[\begin{cases} x_1(p)=x_{1,0}+pB \\ x_2(p)=x_{2,0}-pA \\ y_1(q)=y_{1,0}-qA \\ y_2(q)=y_{2,0}+qB \end{cases}\]

Observe that these are linear functions. We want to minimize

\[\max(|x_1|,|y_1|)+\max(|x_2|,|y_2|)\]

This is a minimization problem involving absolute value linear functions. Consider the lines where the critical points \((p,q)\) occur:

\[\begin{cases} x_1=y_1 \\ x_1+y_1=0 \\ x_2=y_2 \\ x_2+y_2=0 \end{cases}\]

Take pairwise intersections to obtain critical points, each corresponding to a pair \((p,q)\).

Note that these critical points may not necessarily satisfy constraints like \(x_1 \equiv y_1 \pmod 2\), but we have empirical experience that searching over a \(7 \times 7\) grid around each critical point suffices.

The time complexity is \(\mathcal O(T (\log (A,B)+C))\), where \(C = \binom{4}{2} \times 7^2 = 490\).

投稿日時:
最終更新: