公式

D - A xor B plus C 解説 by noya2


Introduction

This editorial is quite long.

The overall flow is structured as follows:

  1. Identify periodicity by considering modulo \(2^d\) operations.
  2. Focus on carry operations that occur at the \(d\)-th bit during binary addition, and use this to calculate the \(d\)-th bit of \(X_N\).
  3. Confirm that the number of carry operations in one period is sufficiently small under the given constraints.
  4. Present a solution and verify that it works efficiently for practical cases.
  5. Optimize the solution further to ensure it comfortably meets the time limit.

A sufficiently fast solution for the test cases provided in the problem is already achieved in step 4. Step 5 serves as a bonus to guarantee computational efficiency.

Problem Structure

To compute \(X_N\), directly iterating the recurrence relation is infeasible due to time constraints. Therefore, we need to find some periodicity. The following fact is key:

The recurrence relation \(X_{i+2} = (X_i \oplus X_{i+1}) + C\) makes sense modulo \(2^d\) for any \(d=0,1,\dots\).

This means that for the sequence \((X \bmod 2^d) = ((X \bmod 2^d)_1, (X \bmod 2^d)_2, \dots)\) where \((X \bmod 2^d)_i := (X_i \bmod 2^d)\), the sequence follows a similar recurrence relation: $\( (X \bmod 2^d)_{i+2} = (((X \bmod 2^d)_i \oplus (X \bmod 2^d)_{i+1}) + C) \bmod 2^d. \)\( In other words, we can calculate the sequence by keeping only the lowest \)d\( bits of the values in \)X$.

Interestingly, it can be shown that the sequence \(X\) defined by general \(A, B, C\) does not exhibit periodicity. However, the sequence \((X \bmod 2^d)\) always has a periodic structure, as the set of possible values is finite. Specifically, the transition from \(((X \bmod 2^d)_i, (X \bmod 2^d)_{i+1})\) to \(((X \bmod 2^d)_{i+1}, (X \bmod 2^d)_{i+2})\) has at most \((2^d)^2 = 2^{2d}\) distinct states, implying a maximum period of \(2^{2d}\).

Moreover, while the original recurrence relation for \(X\) is not reversible, the relation for \((X \bmod 2^d)\) is. That is, \((X \bmod 2^d)_i\) can be derived from \((X \bmod 2^d)_{i+1}\) and \((X \bmod 2^d)_{i+2}\). This reversibility allows us to treat the sequence indices as general integers, not just positive integers.

The following sections analyze the periodicity of \((X \bmod 2^d)\) to compute \(X_N \bmod 2^d\) efficiently. Additionally, we estimate the upper bound of \(X_N\), ensuring that selecting a sufficiently large \(d\) gives us the exact value of \(X_N\).

Periodicity of \((X \bmod 2^d)\)

Let us introduce some notation. It is helpful to think of all non-negative integers as represented in binary form.

For a non-negative integer \(v\) and \(d\), let \(v^d\) denote the \(d\)-th bit of \(v\) in binary form: $\( v^d := (\lfloor v / 2^d \rfloor \bmod 2). \)\( In C++ code, this would be `(v >> d & 1)`. Here, \)2^k\( denotes \)2\( raised to the power of \)k$.

For a sequence \(V\) and a non-negative integer \(d\), define the sequence \(V^d\) as \(V^d_i = (V_i)^d\).

The key to understanding periodicity lies in analyzing “carry operations.” During binary addition, carries at certain bit positions affect higher bits. These carry operations create dependencies between bits, complicating the problem.

To focus on a specific bit’s carry operations, define the sequence \(Y^d\) to represent whether a carry occurs at the \(d\)-th bit when computing \(X\): $\( Y^d_{i+2} = \begin{cases} 1 & \text{if } ((X_i \oplus X_{i+1}) \bmod 2^{d+1}) + (C \bmod 2^{d+1}) \geq 2^{d+1}, \\ 0 & \text{otherwise.} \end{cases} \)$

Meanwhile, \(X^d\), which focuses on the \(d\)-th bit of \(X\), can be updated with a simplified recurrence relation. Since focusing on a single bit makes \(+\) and \(\oplus\) equivalent, the recurrence becomes: $\( X^d_{i+2} = X^d_i \oplus X^d_{i+1} \oplus Y^{d-1}_{i+2} \oplus C^d \quad \cdots (*) \)\( Here, \)Y^{-1} = (0, 0, \dots)$ is defined for convenience.

The periodicity of \((X \bmod 2^d)\) implies the periodicity of \(X^d\) and \(Y^d\). Although \(Y^d\) depends on \(X\), we can redefine \(Y^d\) in terms of \(X^*\) and \(Y^*\) alone: $\( Y^{d+1}_{i+2} = \begin{cases} 1 & \text{if } (X^{d+1}_i \oplus X^{d+1}_{i+1}) + Y^d_{i+2} + C^{d+1} \geq 2, \\ 0 & \text{otherwise.} \end{cases} \quad \cdots (\star) \)$

If no carry occurs at the \(0\)-th bit, the \(0\)-th bit and the higher bits can be treated as independent problems. Without loss of generality, assume that a carry occurs at the \(0\)-th bit. This corresponds to \(Y^0 \neq (0, 0, \dots)\), which is equivalent to \(((A^0 = 0 \lor B^0 = 0) \land C^0 = 1)\).

Supplement It is possible that no carry occurs at any bit. This happens when the bitwise representation of $C$ is a subset of $A$ and $B$. In this case, $X = (A, B, (A \oplus B) + C, A, B, \dots)$ has a trivial periodicity of $3$ (not necessarily minimal). Thus, $X_N$ can be computed easily.

We will now prove the following Lemma 1:

Lemma 1
For any non-negative integer \(d\), the sequence \((X \bmod 2^{d+1})\) has a (not necessarily minimal) period of \(3 \times 2^d\).

This can be proved using mathematical induction. The base case \(d=0\) is straightforward to verify. Assume that Lemma 1 holds for some non-negative integer \(k\), i.e., for \(d=k\).

To analyze further, we introduce the following values \(Z^d_0, Z^d_1, Z^d_2\):

\[ Z^d_{j} = \bigoplus_{i=0}^{2^d-1} Y^d_{3i+j} \]

This represents the XOR of all carry bits in one period of \(Y^d\) when indices are grouped modulo \(3\). For any integer \(j\), \(Z^d_j\) should be interpreted as \(Z^d_{j \bmod 3}\).

From the structure of the 1-bit recurrence \((*)\), it can be shown that \(X^d_i\) can be expressed as the XOR of \(Y^{d-1}_*\), \(A^d\), \(B^d\), and \(C^d\). Furthermore, from the periodicity assumption for \(d=k\), we have:

\[ (X_i \bmod 2^{k+1}) = (X_{i + 3 \times 2^k} \bmod 2^{k+1}), \]

\[ X^{k+1}_i \oplus Z^k_{i} \oplus Z^k_{i-1} = X^{k+1}_{i + 3 \times 2^k}. \]

This means that the lower \(k\) bits of \(X_i\) and \(X_{i + 3 \times 2^k}\) are identical due to periodicity, but the \((k+1)\)-th bit differs by exactly \(Z^k_{i} \oplus Z^k_{i-1}\). This implies that \(Y^k\) also has a (not necessarily minimal) period of \(3 \times 2^k\), and during the \(3 \times 2^{k+1}\) applications of the recurrence, \(Y^k_*\) contributes in the first half of the cycle and cancels out in the second half. Applying the argument to \(i \to i + 3 \times 2^k\), we establish Lemma 1 for \(d=k+1\).

Next, we prove the following Lemma 2:

Lemma 2
For \(d \geq 1\), \(Z^d\) is constant and satisfies \(\lbrace Z^d_0, Z^d_1, Z^d_2 \rbrace = \lbrace 0, 0, 1 \rbrace\) (where \(\lbrace \ast \rbrace\) denotes a multiset).

Consider the recurrence \((\star)\). The conditions for \(Y^{k+1}_{i+2}\) and \(Y^{k+1}_{i+2 + 3 \times 2^k}\) to be \(1\) are as follows:

\[ Y^{k+1}_{i+2} = 1 \iff (X^{k+1}_i \oplus X^{k+1}_{i+1}) + Y^k_{i+2} + C^{k+1} \geq 2, \]

\[ Y^{k+1}_{i+2 + 3 \times 2^k} = 1 \iff (X^{k+1}_{i + 3 \times 2^k} \oplus X^{k+1}_{i+1 + 3 \times 2^k}) + Y^k_{i+2 + 3 \times 2^k} + C^{k+1} \geq 2, \]

\[ \iff (X^{k+1}_i \oplus Z^k_{i} \oplus Z^k_{i-1} \oplus X^{k+1}_{i+1} \oplus Z^k_{i+1} \oplus Z^k_{i}) + Y^k_{i+2} + C^{k+1} \geq 2, \]

\[ \iff (X^{k+1}_i \oplus X^{k+1}_{i+1} \oplus Z^k_{i+1} \oplus Z^k_{i+2}) + Y^k_{i+2} + C^{k+1} \geq 2. \]

For instance, if \(Z^k_{i+1} = Z^k_{i+2}\), then the two conditions are equivalent, and the parity of carry counts (determined by \(Z^{k+1}_{i+2}\)) becomes \(0\). Conversely, if \(Z^k_{i+1} \neq Z^k_{i+2}\), the two conditions are equivalent only if \(Y^k_{i+2} + C^{k+1} = 0\) or \(2\). Otherwise, exactly one of the two conditions is true. Combining these, \(Z^{k+1}_0, Z^{k+1}_1, Z^{k+1}_2\) are given by:

\[ Z^{k+1}_i = \begin{cases} 0 & \text{if } Z^k_i = Z^k_{i-1}, \\ Z^k_i & \text{if } Z^k_i \neq Z^k_{i-1} \land C^{k+1} = 0, \\ (2^k - Z^k_i) \bmod 2 & \text{if } Z^k_i \neq Z^k_{i-1} \land C^{k+1} = 1. \end{cases} \]

Since \((X \bmod 2^{0+1}) = X^0 = (\dots, 0, 0, 1, 0, 0, 1, \dots)\) and \(Y^0 = (\dots, 1, 1, 0, 1, 1, 0, \dots)\), we have \(\lbrace Z^0_0, Z^0_1, Z^0_2 \rbrace = \lbrace 0, 1, 1 \rbrace\). Applying the recurrence for \(k=0, 1, \dots\) confirms the conclusion.

Discussion on Minimal Periodicity Although not directly relevant to solving the problem, it can be shown that the sequence $X$ does not exhibit periodicity in general. To demonstrate this, we can show that the period identified in Lemma 1 is minimal. This can be formalized as Lemma 3: > **Lemma 3** > For any non-negative integer $d$, the sequence $(X \bmod 2^{d+1})$ has a **minimal** period of $3 \times 2^d$. Consider the following fact: > For any non-negative integer $d$, $\lnot (Z^d_0 = Z^d_1 = Z^d_2)$. Using mathematical induction, the base case $d=0$ is easy to verify. Assume Lemma 3 holds for $d=k$ and prove it for $d=k+1$. The minimal period of $(X \bmod 2^{k+2})$ is a multiple of the minimal period of $(X \bmod 2^{k+1})$, which is $3 \times 2^k$. However, it is also a divisor of $3 \times 2^{k+1}$. Since there exists an $i$ such that $Z^k_{i} \oplus Z^k_{i-1} = 1$, we have $X^k_i \neq X^k_{i+3 \times 2^k}$, and thus $3 \times 2^k$ is not a period. Therefore, the minimal period of $(X \bmod 2^{k+2})$ is $3 \times 2^{k+1}$. This completes the proof of Lemma 3. From Lemma 3, it immediately follows that $X$ does not have a maximum value. That is, the elements of $X$ can grow arbitrarily large.

Solution to the Problem

Since \(N\) is as large as \(10^{18}\), directly computing \(X^d\) and \(Y^d\) is infeasible. Using \((\ast)\) and \((\star)\), \(X^d\) can be derived from \(A, B, C, Y^{d-1}\). Furthermore, combining \((\ast)\) and \((\star)\) allows us to compute \(Y^{d+1}\) from \(A, B, C, Y^d\). Hence, the focus shifts to computing \(Y^d\). Utilizing periodicity, we only store values of \(Y^d_i\) for \(i \in [0, 3 \times 2^d)\). Below, \(Y^d\) will be treated as an array of length \(3 \times 2^d\).

It turns out that the number of \(1\)s in \(Y^d\) does not increase drastically as \(d\) increases. Let us confirm this. Following the definition of \(Z\), define \(W^d_0, W^d_1, W^d_2\) as follows. The sum \(W^d_0 + W^d_1 + W^d_2\) exactly matches the number of \(1\)s in \(Y^d\).

\[ W^d_j = \sum_{i=0}^{2^d-1} Y^d_{3i+j} \]

Assume \(A, B, C < 2^m\). In this problem, \(m=20\). Suppose we compute \(Y^{m-1}\) using a naive approach. For \(d \geq m\), since \(A^d = B^d = C^d = 0\), the recurrence relation simplifies. For \(d \geq m\), the recurrence relations \((\ast)\) and \((\star)\) can be combined and rewritten in terms of \(Y\):

\[ Y^d_i = \begin{cases} 1 & \text{if } \displaystyle \bigoplus_{\substack{0 \leq j < i \\ j \equiv i, i-1}} Y^{d-1}_j + Y^{d-1}_i \geq 2, \\ 0 & \text{otherwise}. \end{cases} \]

Here, the subscript \(\equiv\) takes modulo \(3\). Simplifying further:

\[ Y^d_i = \begin{cases} \displaystyle \bigoplus_{\substack{0 \leq j < i \\ j \equiv i, i-1}} Y^{d-1}_j & \text{if } Y^{d-1}_i = 1, \\ 0 & \text{if } Y^{d-1}_i = 0. \end{cases} \quad \cdots (\diamond) \]

Let us establish a method to compute the array \(Y^d\) of length \(3 \times 2^d\) from \(Y^{d-1}\) of length \(3 \times 2^{d-1}\). For \(i \in [0, 3 \times 2^{d-1})\), the following hold:

  • If \(Y^{d-1}_i = 0\), then \(Y^d_i = Y^d_{i + 3 \times 2^{d-1}} = 0\).
  • If \(Y^{d-1}_i = 1\), let \(f(i) := \displaystyle \bigoplus_{\substack{0 \leq j < i \\ j \equiv i, i-1}} Y^{d-1}_j\), then \(Y^d_i = f(i)\) and \(Y^d_{i + 3 \times 2^{d-1}} = f(i) \oplus Z^{d-1}_i \oplus Z^{d-1}_{i-1}\).

From Lemma 2, there is exactly one \(j \in \{0, 1, 2\}\) such that \(Z^{d-1}_j \oplus Z^{d-1}_{j-1} = 0\), which is constant for all \(d\). Denote this \(j\) as \(j'\). Thus, the second bullet point can be rewritten as:

  • If \(Y^{d-1}_i = 1\), then:
    • If \(i \equiv j'\), \(Y^d_i = Y^d_{i + 3 \times 2^{d-1}} = f(i)\),
    • If \(i \not\equiv j'\), \(Y^d_i = f(i)\), \(Y^d_{i + 3 \times 2^{d-1}} = f(i) \oplus 1\).

Now, we are ready to evaluate \(W^d_0, W^d_1, W^d_2\). For \(j \neq j'\), \(W^d_j = W^{d-1}_j\). However, \(W^d_{j'}\) cannot be directly evaluated. If \(f(i) = 1\) for all \(i \equiv j'\), then \(W^d_{j'} = 2W^{d-1}_{j'}\). Conversely, if \(f(i) = 0\) for all \(i \equiv j'\), then \(W^d_{j'} = 0\). For an upper bound, the following inequality holds:

\[ W^d_{j'} \leq W^{d-1}_{j'} + W^{d-1}_{j'-1}. \]

Since \(f(i)\) is in the form of cumulative XOR, if \(W^{d-1}_{j'-1} = 0\), \(Y^d_i\) alternates between \(0\) and \(1\) for \(Y^{d-1}_i = 1\). In this case, \(W^d_{j'} = \lfloor W^{d-1}_{j'} / 2 \rfloor \times 2 \leq W^{d-1}_{j'}\). For general \(W^{d-1}_{j'-1}\), the alternation breaks at most \(W^{d-1}_{j'-1}\) times, resulting in at most \(W^{d-1}_{j'-1}\) extra \(1\)s.

Finally, we obtain an upper bound for \(W\). Clearly, \(W^{m-1}_j \leq 2^{m-1}\). Using the inequality for \(d = m-1, m-2, \dots, d\):

\[ W^d_{j'} \leq (d - m + 2)2^{m-1}, \]

\[ W^d_{j'+1}, W^d_{j'+2} \leq 2^{m-1}. \]

This analysis shows that storing the indices \(i\) where \(Y^d_i = 1\) (denoted as \(I_d\)) is much more efficient than storing \(Y^d\) directly. Computing \(I_d\) from \(I_{d-1}\) can be done in \(O(|I_{d-1}|)\) time. Additionally, since \(X^d_N\) computation only requires values of \(i \leq N\), we can discard elements of \(I_d\) larger than \(N\).

Summarizing, the solution works as follows:

  1. Compute \(X \bmod 2^m\) and \(Y^{m-1}\) naively. Use this to compute \(I_{m-1}\).
  2. For \(d = m, m+1, \dots\) until \(I_d\) becomes empty:
    • Compute \(X^d_N\) from \(I_{d-1}\).
    • Compute \(I_d\) from \(I_{d-1}\).
    • Discard elements of \(I_d\) larger than \(N\).

Time Complexity Estimation

Let \(d'\) be the largest \(d\) such that \(3 \times 2^{d-1} \leq N\). For \(d > d'\), \(I_d \subset I_{d-1}\), and \(I_d\) eventually becomes empty, causing the algorithm to terminate. Although \(|I_d|\) can only be roughly estimated as \(\max(A, B, C) \log N\), practical experiments confirm that \(|I_d|\) decreases exponentially. The algorithm handles the given test cases efficiently and achieves AC.

Optimization

For \(d > d'\), each element \(x \in I_{d+1}\) must be removed from \(I\) at some \(d > d'\). Define \(d(x)\) as the smallest \(d > d'\) such that \(x \not\in I_d\). For the smallest \(x \in I_d\), \(d(x) = d' + 2\).

If we compute \(d(x)\) for all \(x \in I_{d'+1}\), we can determine the \(d'+1\)-th bit and beyond of \(X_N\) in \(O(|I_{d'+1}|)\) time.

Using \((\diamond)\), the smallest \(d\) where \(\displaystyle \bigoplus_{\substack{0 \leq j < i \\ j \equiv i, i-1}} Y^{d-1}_j = 0\) can be determined recursively using \(d(j)\). With simulation tools like std::set, this reduces the time complexity to \(O(\max(A, B, C) \log N \log(\max(A, B, C) \log N))\).

投稿日時:
最終更新: