F - Triple Transformation Editorial by evima
Let \(f(x,y,z)\) denote the triple obtained by applying the operation once to \((x,y,z)\). Let \(f ^ {(K)}(x,y,z)\) denote the triple obtained by applying the operation \(K\) times.
The case \(x=y=z=0\) is trivial, so we assume \(x+y+z>0\) below.
[1] Common properties of the four types of transformations
The difficulty of this problem lies in the fact that which of the four types of transformations is applied depends on the relative sizes of \(x,y,z\). However, in fact all four types share a common property, and discovering this property leads to the solution.
A straightforward property is that \(S := x+y+z\) is invariant under any transformation. That is,
\[ f(x,y,z)=(x',y',z') \quad \implies \quad x+y+z=x'+y'+z'. \]
Next, focus on the first component. After replacing \((x,y,z)\), the first component becomes one of
\[ x-y-z,\quad 2x,\quad y+z-x. \]
Considering these modulo \(S\), we can find a common property among all three variants. Indeed,
\[ \begin{aligned} x-y-z&\equiv (x-y-z)+(x+y+z)\equiv 2x\pmod{S},\\ y+z-x&\equiv (y+z-x)-(x+y+z)\equiv -2x\pmod{S} \end{aligned} \]
so viewed modulo \(S\), the new value of \(x\) is either \(\pm 2x\). For the triple as a whole, it can be verified that the new value of \((x,y,z)\) modulo \(S\) is either
\[ (2x,2y,2z)\quad \text{or}\quad (-2x,-2y,-2z). \]
[2] Computing \(f^{(K)}(x,y,z)\) (excluding exceptional patterns)
Let
\[ a =(2^Kx)\bmod S,\quad b =(2^Ky)\bmod S,\quad c =(2^Kz)\bmod S. \]
Cases such as \(a=0\), \(b=0\), \(c=0\) are treated as exceptions and handled later. Here we assume \(\boldsymbol{a\neq 0, b\neq 0, c\neq 0}\).
From \(1\leq a,b,c < S\) and \(a+b+c\equiv 0\pmod{S}\), we get
\[ a+b+c\in \lbrace S, 2S\rbrace. \]
By the property described in [1] and from \(0\leq x,y,z\leq S\), \( f^{(K)}(x,y,z)\) is either
\[ (a,b,c)\qquad \text{or}\qquad (S-a,S-b,S-c). \]
Combined with \(x+y+z=S\),
- if \(a+b+c=S\), then \(f^{(K)}(x,y,z)=(a,b,c)\),
- if \(a+b+c=2S\), then \(f^{(K)}(x,y,z)=(S-a,S-b,S-c)\).
[3] Solution
Let us handle the exceptions such as \(2^Kx\equiv 0\pmod{S}\) and complete the solution.
First, observe the following:
- For a triple \((x,y,z)\) where \(x,y,z\) are all multiples of \(2\), we can reduce to the problem of repeating the operation on \((x/2,y/2,z/2)\) by dividing all values by \(2\). That is, we can reduce to a problem where \(S=x+y+z\) is halved.
- If \(S=x+y+z\) is even, applying the operation once reduces to the case where \(x,y,z\) are all even. Combined with the above, we can reduce to a problem where \(S=x+y+z\) is halved.
Therefore, by processing roughly the first \(\lfloor \log_2 S\rfloor\) operations in advance, we can reduce to a problem where \(S=x+y+z\) is odd.
When \(S=x+y+z\) is odd, \(2^Kx\equiv 0\pmod{S}\) holds only when \(x\equiv 0\). Therefore:
- If none of \(x,y,z\) is \(0\): The exceptions described in [2] do not occur.
- If exactly one of \(x,y,z\) is \(0\). For example, let \(x=0,y\neq 0,z\neq 0\). In this case, it can be verified that the operation always maps \((0,y,z)\) to \((0,2y\bmod S,2z\bmod S)\).
- If exactly two of \(x,y,z\) are \(0\). For example, let \((x,y,z)=(0,0,S)\). It can be verified that the operation maps \((0,0,S)\) to itself.
Computing the answer in case 3 is easy.
In case 2, setting
\[ a =(2^Kx)\bmod S,\quad b =(2^Ky)\bmod S,\quad c =(2^Kz)\bmod S \]
gives \(a=0\) and \(b+c=S\), and \(f^{(K)}(x,y,z)=(a,b,c)\) holds, so case 2 can be handled by the same rule as case 1.
In this problem, the task is to find \(K\) such that \(f^{(K)}(A_1,A_2,A_3)=(B_1,B_2,B_3)\). After the preprocessing described above, this problem can be rephrased as finding \(K\) simultaneously satisfying the three equations:
\[ \begin{aligned} 2^KA_1&\equiv b_1\pmod{S},\\ 2^KA_2&\equiv b_2\pmod{S},\\ 2^KA_3&\equiv b_3\pmod{S} \end{aligned} \]
(done by solving for \((b_1,b_2,b_3)=(B_1,B_2,B_3)\) or \((S-B_1,S-B_2,S-B_3)\), etc.). This is a variant of the well-known discrete logarithm problem.
[4] Solving the discrete logarithm problem in this problem
The BSGS (Baby Step Giant Step) method is applicable to this problem as well (for those unfamiliar, see also ABC270G and ARC024D).
Note that we have reduced to the case where \(S\) is odd, so \(2\) has a multiplicative inverse modulo \(S\).
Take an appropriate \(L\) and write \(K=iL+j\).
The system
\[ \begin{aligned} 2^KA_1&\equiv b_1\pmod{S},\\ 2^KA_2&\equiv b_2\pmod{S},\\ 2^KA_3&\equiv b_3\pmod{S} \end{aligned} \]
is equivalent to
\[ \begin{aligned} 2^{j}A_1&\equiv 2^{-Li}b_1\pmod{S},\\ 2^{j}A_2&\equiv 2^{-Li}b_2\pmod{S},\\ 2^{j}A_3&\equiv 2^{-Li}b_3\pmod{S}. \end{aligned} \]
First, for \(j=0,1,\ldots,L\), precompute
\[ (2^{j}A_1\bmod S,2^{j}A_2\bmod S,2^{j}A_3\bmod S). \]
Then, for \(i=0,1,\ldots,L\), compute
\[ (2^{-Li}b_1\bmod S,2^{-Li}b_2\bmod S,2^{-Li}b_3\bmod S) \]
while searching for a pair \((i,j)\). By setting \(L=\sqrt{K_{\max}}\) where \(K_{\max}\) is an upper bound on \(K\), the minimum solution \(K\) can be found in \(O(\sqrt{K_{\max}}\log K_{\max})\) or similar time. Also, since \(2^k\bmod S\) takes at most \(S\) distinct values, \(K_{\max}\) can be taken to be \(S\).
The above solution uses the fact that \(2\) has a multiplicative inverse, but it is also possible to solve this problem under weaker assumptions (for example, even when \(S\) is even). Such approaches can be found at Discrete Logarithm for Monoid Actions (Japanese).
posted:
last update: