Four Stones
Contest: Codeforces Round 606 (Div. 1, based on Technocup 2020 Elimination Round 4)
Difficulty rating: 3500
Tags: constructive algorithms

There are four stones on an infinite line in integer coordinates $$$a_1, a_2, a_3, a_4$$$. The goal is to have the stones in coordinates $$$b_1, b_2, b_3, b_4$$$. The order of the stones does not matter, that is, a stone from any position $$$a_i$$$ can end up in at any position $$$b_j$$$, provided there is a required number of stones in each position (that is, if a coordinate $$$x$$$ appears $$$k$$$ times among numbers $$$b_1, \ldots, b_4$$$, there should be exactly $$$k$$$ stones at $$$x$$$ in the end).

We are allowed to move stones with the following operation: choose two stones at distinct positions $$$x$$$ and $$$y$$$ with at least one stone each, and move one stone from $$$x$$$ to $$$2y - x$$$. In other words, the operation moves a stone to a symmetric position relative to some other stone. At any moment it is allowed to have any number of stones at the same position.

Find any sequence of operations that achieves the goal, or determine that it is impossible. The sequence does not have to be shortest, but it may contain at most $$$1000$$$ operations.

Input format:
The first line contains four integers $$$a_1, \ldots, a_4$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — initial coordinates of the stones. There may be multiple stones sharing the same coordinate.

The second line contains four integers $$$b_1, \ldots, b_4$$$ ($$$-10^9 \leq b_i \leq 10^9$$$) — target coordinates of the stones. There may be multiple targets sharing the same coordinate.

Output format:
If there is no sequence of operations that achieves the goal, print a single integer $$$-1$$$. Otherwise, on the first line print a single integer $$$k$$$ ($$$0 \leq k \leq 1000$$$) — the number of operations in your sequence. On the next $$$k$$$ lines, describe the operations. The $$$i$$$-th of these lines should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$x_i \neq y_i$$$) — coordinates of the moved stone and the center of symmetry stone for the $$$i$$$-th operation.

For each operation $$$i$$$, there should at least one stone in each of the coordinates $$$x_i$$$ and $$$y_i$$$, and the resulting coordinate $$$2y_i - x_i$$$ must not exceed $$$10^{18}$$$ by absolute value.

If there are multiple suitable sequences, print any of them. It is guaranteed that if there is a suitable sequence of operations, then there is also a suitable sequence that satisfies all the additional requirement.

Constraints:
Time limit: 1.0 s
Memory limit: 512.0 MB

Example input:
0 1 2 3
3 5 6 8

Example output:
3
1 3
2 5
0 3

Example input:
0 0 0 0
1 1 1 1

Example output:
-1

Editorial:
First, when is the task impossible? If all $$$a_i$$$ have the same remainder $$$d$$$ modulo an integer $$$g$$$, then it will always be true regardless of our moves. The largest $$$g$$$ we can take is equal to $$$\mathrm{GCD}(a_2 - a_1, a_3 - a_1, a_4 - a_1)$$$. Remark: case when all $$$a_i$$$ are equal is special. If the GCD's or the remainders do not match for $$$a_i$$$ and $$$b_i$$$, then there is no answer. For convenience, let us apply the transformation $$$x \to (x - d) / g$$$ to all coordinates, and assume that $$$g = 1$$$. We can observe further that parity of each coordinate is preserved under any operation, thus the number of even and odd numbers among $$$a_i$$$ and $$$b_i$$$ should also match.
Otherwise, we will divide the task into two subproblems:
1. Given four stones $$$a_1, \ldots, a_4$$$, move them into a segment $$$[x, x + 1]$$$ for an arbitrary even $$$x$$$.
2. Given four stones in a segment $$$[x, x + 1]$$$, shift them into a segment $$$[y, y + 1]$$$.
Suppose we can gather $$$a_i$$$ and $$$b_i$$$ into segments $$$[x, x + 1]$$$ and $$$[y, y + 1]$$$. Then we can solve the problem as follows:
• gather $$$a_i$$$ into $$$[x, x + 1]$$$;
• shift the stones from $$$[x, x + 1]$$$ to $$$[y, y + 1]$$$;
• undo gathering $$$b_i$$$ into $$$[y, y + 1]$$$ (all moves are reversible).
First subproblem. Throughout, let $$$\Delta$$$ denote the maximum distance between any two stones. Our goal is to make $$$\Delta = 1$$$. We will achieve this by repeatedly decreasing $$$\Delta$$$ by at least a quarter, then we will be done in $$$O(\log \Delta)$$$ steps.
Suppose $$$a_1 \leq a_2 \leq a_3 \leq a_4$$$, and one of the stones $$$a_i$$$ is in the range $$$[a_1 + \Delta / 4, a_1 + 3 \Delta / 4]$$$. Consider two halves $$$[a_1, a_i]$$$ and $$$[a_i, a_4]$$$, and mirror all stones in the shorter half with respect to $$$a_i$$$. Then, $$$a_i$$$ becomes either the leftmost or the rightmost stone, and the new maximum distance $$$\Delta'$$$ is at most $$$3 \Delta / 4$$$, thus reaching our goal.
What if no stones are in this range? Denote $$$d_i = \min(a_i - a_1, a_4 - a_i)$$$ — the distance from $$$a_i$$$ to the closest extreme (leftmost or rightmost) stone. We suppose that $$$d_2, d_3 < \Delta / 4$$$, otherwise we can reduce $$$\Delta$$$ as shown above. Further at least one of $$$d_2, d_3$$$ is non-zero, since otherwise we would have $$$\mathrm{GCD}(a_2 - a_1, a_3 - a_1, a_4 - a_1) = 1 = \Delta$$$, and the goal would be reached.
Observe that performing moves $$$(a_i, a_j)$$$, $$$(a_i, a_k)$$$ changes $$$a_i$$$ to $$$a_i + 2(a_k - a_j)$$$. With this we are able to change $$$d_i$$$ to $$$d_i + 2 d_j$$$. Repeatedly do this with $$$d_i \leq d_j$$$ (that is, we are adding twice the largest distance to the smallest). In $$$O(\log \Delta)$$$ steps we will have $$$\max(d_2, d_3) \geq \Delta / 4$$$, allowing us to decrease $$$\Delta$$$ as shown above.
Finally, we have all $$$a_i \in [x, x + 1]$$$. To make things easier, if $$$x$$$ is odd, move all $$$a_i = x$$$ to $$$x + 2$$$.
Second subproblem. We now have all stones in a range $$$[x, x + 1]$$$, and we want to shift them by $$$2d$$$ (we will assume that $$$2d \geq 0$$$). Observe that any arrangement of stones can be shifted by $$$2\Delta$$$ by mirroring all stones with respect to the rightmost stone twice.
Consider an operation we will call Expand: when $$$a_1 \leq a_2 \leq a_3 \leq a_4$$$, make moves $$$(a_3, a_1)$$$ and $$$(a_2, a_4)$$$. We can see that if we apply Expand $$$k$$$ times, the largest distance $$$\Delta$$$ will grow exponentially in $$$k$$$. We can then shift the stones by $$$2d$$$ as follows:
1. Apply Expand until $$$\Delta > d$$$.
2. While $$$\Delta \leq d$$$, shift all stones by $$$2\Delta$$$ as shown above, and decrease $$$d$$$ by $$$\Delta$$$.
3. If $$$\Delta = 1$$$, exit.
4. Perform Expand$$${}^{-1}$$$ — the inverse operation to Expand, and return to step $$$2$$$.
In the end, we will have $$$\Delta = 1$$$ and $$$d = 0$$$. Further, since $$$\Delta$$$ grows exponentially in the number of Expand's, for each value of $$$\Delta$$$ we will be making $$$O(1)$$$ shifts, thus the total number of operations for this method $$$O(\log d)$$$.