G - 3^N Minesweeper Editorial by en_translator
Consider the following six conditions of an integer \(T\):
- Condition \(1\): \(T\) is \(0\) or \(1\).
- Condition \(2\): \(T\) is \(0\), \(1\), or \(2\).
- Condition \(3\): \(T\) is \(1\) or \(2\).
- Condition \(4\) : \(T = 0\).
- Condition \(5\) : \(T = 1\).
- Condition \(6\) : \(T = 2\).
Also, let us denote by \(f(X_1,X_2, \ldots, X_N)\) the number of bombs at the positions whose \(i\)-th digit in ternary notation satisfies condition \(X_i\).
Then the input and output are interpreted as follows: given \(f(X)\) for \(X\) such that \(X_i \in \lbrace 1,2,3 \rbrace\) for each \(i\), you are asked to find \(f(X)\) for \(X\) such that \(X_i \in \lbrace 4,5,6 \rbrace\) for each \(X_i \in \lbrace 4,5,6 \rbrace\).
Here,
\(
\begin{aligned}
f(X_1,X_2, \ldots, X_{i-1}, 4, X_{i+1}, \ldots, X_N) &= f(X_1,X_2, \ldots, X_{i-1}, 2, X_{i+1}, \ldots, X_N) - f(X_1,X_2, \ldots, X_{i-1}, 3, X_{i+1}, \ldots, X_N), \\
f(X_1,X_2, \ldots, X_{i-1}, 5, X_{i+1}, \ldots, X_N) &= f(X_1,X_2, \ldots, X_{i-1}, 1, X_{i+1}, \ldots, X_N) + f(X_1,X_2, \ldots, X_{i-1}, 3, X_{i+1}, \ldots, X_N) -f(X_1,X_2, \ldots, X_{i-1}, 2, X_{i+1}, \ldots, X_N), \\
f(X_1,X_2, \ldots, X_{i-1}, 6, X_{i+1}, \ldots, X_N) &= f(X_1,X_2, \ldots, X_{i-1}, 2, X_{i+1}, \ldots, X_N) - f(X_1,X_2, \ldots, X_{i-1}, 1, X_{i+1}, \ldots, X_N);
\end{aligned}
\)
so it is sufficient to, for each \(i = 1,2, \ldots, N\), find \(f(X)\) for all \(X\) such that \(X_j \in \lbrace 4,5,6 \rbrace\) if \(j \leq i\) and \(X_j \in \lbrace 1,2,3 \rbrace\) if \(j > i\).
This operation can be considered as an application of \(N\)-dimensional cumulative sums or fast Zeta transformation.
posted:
last update: