F - Buckets 解説 by evima
When \(A = B\), the answer is clearly Yes. Below, we assume \(A \neq B\).
The operation can be rephrased as: choose distinct integers \(i,j\) and replace \(A_i,A_j\) with \(\max(A_i+A_j-M,0),\min(A_i+A_j,M)\), respectively.
Since \(A \neq B\), at least one operation is required, but performing even one operation makes one of \(A_i,A_j\) equal to either \(0\) or \(M\). Therefore, if \(B\) contains neither \(0\) nor \(M\), the answer is No.
Conversely, if \(B\) contains \(0\) or \(M\), then choosing some \(i\) with \(A_i = 0\) or \(M\) together with another \(j\) and performing the operation swaps \(A_i\) and \(A_j\). Thus, it suffices to check whether \(A\) and \(B\) can be made equal as multisets.
A necessary and sufficient condition for making \(A\) and \(B\) equal as multisets is: \(\sum A = \sum B\) and “the elements of \(A\) can be partitioned into \(N\) sets \(S_1,S_2,\dots,S_N\) (possibly empty) such that for each \(i\), the sum of elements in \(S_i\) is congruent to \(B_i\) modulo \(M\).” More precisely:
- There exists a sequence of positive integers \(x=(x_1,x_2,\dots,x_N)\) of length \(N\) satisfying the following conditions.
- For all \(i\), \(1 \le x_i \le N\).
- For all \(k\), let \(C_k = \sum_{x_i = k} A_i \bmod M\). Then \(B\) and \(C\) are equal modulo \(M\) (that is, replacing \(M\) with \(0\) in \(B\)).
We prove necessity. That \(\sum A = \sum B\) is necessary is obvious, so we prove the condition on \(x\). Initially, set \(x_i = i\) for each \(i\). Then, each time an operation chooses \(i,j\), replace all occurrences of \(x_j\) in \(x\) with \(x_i\). This yields an \(x\) satisfying the conditions.
We prove sufficiency. For each \(k\), gather all the water in buckets \(i\) satisfying \(x_i = k\) into a single bucket. For those \(i\) for which such an \(i\) exists, the gathered bucket contains the desired amount of water. For other \(i\), \(B_i\) is either \(0\) or \(M\), but since \(\sum A = \sum B\), the counts of \(0\) and \(M\) are also fine.
Below, we assume \(\sum A = \sum B\), and let \(X_i\) be the count of \(i\) in \(A\) and \(Y_i\) be the count of \(i\) in \(B\). First, since \(0\) and \(M\) can be identified, we may replace \(X_0,Y_0\) with \(X_0+X_M,Y_0+Y_M\) and then remove \(X_M,Y_M\). Here, because \(\sum A = \sum B\), we do not actually need to assign every element of \(A\) to some \(S\); the sum of leftover elements is congruent to \(0\) modulo \(M\). Thus, the problem becomes: can we use “\(X_i\) copies of \(i\) \((0 \le i < M)\)” to produce “\(Y_i\) copies of \(i\) \((0 \le i < M)\)” modulo \(M\) with leftovers allowed?
First, since \(Y_0\) can be satisfied by the empty set, we may set \(Y_0 = 0\). Also, when \(X_i, Y_i \ge 1\), we may use one copy of \(i\) to produce one copy of \(i\). If there were a solution that never uses \(\lbrace i \rbrace\) to produce \(i\), we can construct a solution that sometimes uses \(\lbrace i \rbrace\) to produce \(i\) as follows.
- If \(i\) is leftover: treat one of the leftover \(i\)’s as produced by \(\lbrace i \rbrace\).
- If \(i\) is not leftover: \(i\) is being used to produce something other than \(i\). Swap that usage of \(i\) with the set other than \(\lbrace i \rbrace\) used to produce \(i\).
By repeating this, we can reach a situation where \(\min(X_i,Y_i) = 0\). We say that \(i\) is in shortage when \(Y_i > 0\). We case-split based on which \(i\) are in shortage.
From here on, the phrase “\(i\) can be produced by the options \(S_1,S_2,\dots,S_k\)” enumerates multisets \(S\) satisfying the following conditions.
- \(\sum S \equiv i \pmod M\)
- \(S\) does not contain \(M\) or more copies of the same element.
- There is no subset \(T\) satisfying \(T \subsetneq S\), \(|T| \neq 0\), \(\sum T \equiv 0 \pmod M\) simultaneously.
\(M = 1\)
Possible.
\(M = 2\)
- When nothing is in shortage
- When $1$ is in shortage
Possible.
Impossible.
\(M = 3\)
- When nothing is in shortage
- When only $1$ is in shortage
- When only $2$ is in shortage
- When both $1$ and $2$ are in shortage
Possible.
We must produce $1$ using $\lbrace 2,2 \rbrace$, so the condition is $2Y_1 \le X_2$.
By multiplying all elements by $2$, this reduces to the case where only $1$ is in shortage. The condition is $2Y_2 \le X_1$.
Impossible.
\(M=4\)
- When nothing is in shortage
- When only $1$ is in shortage
- When only $2$ is in shortage
- When only $3$ is in shortage
- When $1$ and $2$ are in shortage
- When $1$ and $3$ are in shortage
- When $2$ and $3$ are in shortage
- When $1$, $2$, and $3$ are all in shortage
Possible.
There are two options: $\lbrace 2,3 \rbrace$ and $\lbrace 3,3,3 \rbrace$. We need $3$ for each copy of $1$, and after using one $3$ each, we need to add either $\lbrace 2 \rbrace$ or $\lbrace 3,3 \rbrace$, so the conditions are $Y_1 \le X_3$ and $Y_1 \le \frac{X_3-Y_1}{2} + Y_2$.
There are two options: $\lbrace 1,1 \rbrace$ and $\lbrace 3,3 \rbrace$. Since these are independent, the condition is $Y_2 \le \left \lfloor \frac{X_1}{2} \right \rfloor + \left \lfloor \frac{X_3}{2} \right \rfloor$.
By multiplying all elements by $3$, this reduces to the case where only $1$ is in shortage. The conditions are $Y_3 \le X_1$ and $Y_3 \le \frac{X_1-Y_3}{2} + Y_2$.
We must produce $1$ using $\lbrace 3,3,3 \rbrace$ and $2$ using $\lbrace 3,3 \rbrace$. Thus, the condition is $3Y_1 + 2Y_2 \le X_3$.
No combination of $2$'s can produce $1$ or $3$ modulo $4$, so it is impossible.
By multiplying all elements by $3$, this reduces to the case where $1$ and $2$ are in shortage. The condition is $3Y_3 + 2Y_2 \le X_1$.
Impossible.
Thus, all cases have been resolved. The time complexity is \(\mathrm{O}(Q)\) treating \(M\) as a constant.
投稿日時:
最終更新: