D - 回転スタンプ / Rotating Stamp Editorial by admin Editorial for printing
Claude Opus 5 (High)Overview
This problem asks us to maximize the number of cells covered by ink in both presses (dark cells)—once “without rotation” and once with a “90-degree clockwise rotation”—after inverting at most \(K\) cells of the stamp. Formulating the condition for a cell to become dark reveals that the problem is independent for each cycle under a 90-degree rotation of the grid. We can solve the problem by combining the choices for each cycle using knapsack DP.
Analysis
1. Formulating the Condition for a Cell to be “Dark”
Let \(S_{i,j} \in \{0,1\}\) represent the stamp after modification.
- 1st press (no rotation): Ink is placed on cell \((r,c)\) of the paper when \(S_{r,c} = 1\).
- 2nd press (\(90^\circ\) clockwise): Cell \((i,j)\) of the stamp overlaps with cell \((j,\; N-i+1)\) of the paper. The cell covering cell \((r,c)\) of the paper satisfies \(j=r,\; N-i+1=c\), which means \(i=N-c+1,\ j=r\). In other words, this occurs when \(S_{N-c+1,\;r}=1\).
Therefore,
\[ (\text{number of dark cells}) = \sum_{r,c} \bigl[\, S_{r,c}=1 \ \text{and}\ S_{g(r,c)}=1 \,\bigr],\qquad g(r,c) := (N-c+1,\; r) \]
Here, \(g\) is the \(90^\circ\) rotation of the grid itself, which is a permutation of all cells.
2. Decomposition into Cycles Separates the Problem
Applying \(g\) four times returns to the original state (\(g^4 = \mathrm{id}\)), so the \(N^2\) cells are decomposed into:
- Cycles of length \(4\) (e.g., \((1,1)\to(N,1)\to(N,N)\to(1,N)\to(1,1)\))
- The center cell \(\left(\frac{N+1}{2},\frac{N+1}{2}\right)\) (a cycle of length \(1\)), which exists only when \(N\) is odd
Let a cycle be \(x_0, x_1=g(x_0), \dots, x_{L-1}\), and let its values be \(b_t = S_{x_t}\). Then the sum above can be written as:
\[ \sum_{\text{cycle}} \ \sum_{t=0}^{L-1} \bigl[\, b_t = 1 \ \text{and}\ b_{t+1 \bmod L} = 1 \,\bigr] \]
In other words, the core of this problem is that cells in different cycles do not affect each other. Furthermore, since the inversion cost is simply the sum of the number of inversions in each cycle, both the cost and the value can be separated cycle by cycle.
3. Comparison with the Naive Approach
Exhaustively searching which cells to invert would require \(2^{N^2}\) operations (which is \(2^{6400}\) for \(N=80\)), making it completely impossible. However, based on the observation above, by testing all \(2^4 = 16\) possibilities within each cycle (at most \(4\) cells) and precomputing the “maximum value achievable when paying a cost of \(d\)”, the problem reduces to a knapsack problem where each cycle is treated as an item.
Concrete Example (\(N=2\), \(K=1\), \(A=\begin{pmatrix}1&0\\0&1\end{pmatrix}\))
Since \(N=2\), all \(4\) cells form a single cycle \((1,1)\to(2,1)\to(2,2)\to(1,2)\), with values \((1,0,1,0)\). Since there are no adjacent pairs of \(1\)s, there are currently \(0\) dark cells. Inverting one cell to get \((1,1,1,0)\) makes both \((b_0,b_1)\) and \((b_1,b_2)\) equal to \(1\), yielding an answer of \(2\).
Algorithm
- Trace the cells (using \(f(r,c) = (N-1-c,\; r)\) in \(0\)-indexed coordinates) to enumerate all cycles formed by \(g\).
- For each cycle (of length \(L \le 4\)), iterate through all \(2^L\) bitmasks to compute:
- Cost \(= \text{number of cells differing from } A\)
- Value \(= \sum_t [\,b_t \wedge b_{(t+1)\bmod L}\,]\)
and construct a table best[cost] = (maximum value achievable with that cost) (of length \(L+1\)).
The values for a cycle of length \(4\) can be easily classified: - Four \(1\)s \(\to\) value \(4\) - Three \(1\)s \(\to\) value \(2\) - Two adjacent \(1\)s \(\to\) value \(1\) - Otherwise (two diagonal \(1\)s, or at most one \(1\)) \(\to\) value \(0\)
For length \(1\) (the center cell), since \(b\wedge b = b\), setting it to \(1\) gives a value of \(1\).
3. Perform knapsack DP by processing the cycles in order, letting dp[k] = maximum value when the total cost used is exactly k.
4. The answer is \(\max_{0 \le k \le K} \mathrm{dp}[k]\) (take the maximum over all \(k \le K\), since the condition is “at most \(K\)”).
Complexity
The number of cycles is approximately \(N^2/4\), and the DP transitions for each cycle take \(O(K \cdot (L+1)) = O(K)\).
- Time Complexity: \(O(N^2 K)\) (roughly \(4\times 10^7\) operations for \(N=80,\ K=6400\), which is sufficiently fast)
- The brute-force search within each cycle takes \(O(N^2 \cdot 2^4)\), which is negligible
- Space Complexity: \(O(N^2 + K)\)
Implementation Notes
Be careful not to make mistakes in the rotation mapping. In \(0\)-indexed coordinates, the cell corresponding to paper cell \((r,c)\) is \((N-1-c,\; r)\). Mechanically convert from the \(1\)-indexed \((N-c+1, r)\). Even if the direction (\(g\) or \(g^{-1}\)) is reversed, the cycle decomposition and the “number of adjacent pairs of \(1\)s on the cycle” remain the same, so the answer will not change; however, keep it consistent to avoid confusion.
For cycle enumeration, manage visited cells with a
visarray and repeatedly apply \(g\) starting from unvisited cells until the cycle closes.best[cost]is not monotonic with respect tocost(for example, cost \(2\) may achieve a higher value than cost \(1\)). However, this is not an issue as long as the DP state tracks “cost is exactly \(k\)” and we take the maximum over all \(k \le K\) at the end (alternatively, you can makebestmonotonic by taking prefix/suffix maximums).Initialize unreachable states with a sufficiently small value (such as
-1e18) and skip them during transitions.It is reassuring to verify that the implementation works when \(K=0\) (only the cost-\(0\) choice is taken in each cycle), and that when \(N=1\), the answer is \(A_{1,1}\) (or \(1\) if \(K\ge 1\)).
Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, K;
if(!(cin >> N >> K)) return 0;
vector<vector<int>> A(N, vector<int>(N));
for(int i=0;i<N;i++) for(int j=0;j<N;j++) cin >> A[i][j];
// f(r,c) = (N-1-c, r) (0-indexed)
vector<vector<char>> vis(N, vector<char>(N, 0));
const long long NEG = -1e18;
vector<long long> dp(K+1, NEG);
dp[0] = 0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(vis[i][j]) continue;
vector<pair<int,int>> cyc;
int r=i, c=j;
while(!vis[r][c]){
vis[r][c]=1;
cyc.push_back({r,c});
int nr = N-1-c, nc = r;
r = nr; c = nc;
}
int len = (int)cyc.size();
// enumerate assignments
vector<int> best(len+1, -1);
for(int mask=0; mask<(1<<len); mask++){
int cost=0, val=0;
for(int t=0;t<len;t++){
int b = (mask>>t)&1;
if(b != A[cyc[t].first][cyc[t].second]) cost++;
}
for(int t=0;t<len;t++){
int b = (mask>>t)&1;
int b2 = (mask>>((t+1)%len))&1;
if(b && b2) val++;
}
if(cost<=len && val > best[cost]) best[cost]=val;
}
// make best monotone-usable via dp
vector<long long> ndp(K+1, NEG);
for(int c0=0;c0<=K;c0++){
if(dp[c0]==NEG) continue;
for(int cst=0; cst<=len; cst++){
if(best[cst]<0) continue;
int nc2 = c0+cst;
if(nc2 > K) continue;
long long v = dp[c0] + best[cst];
if(v > ndp[nc2]) ndp[nc2]=v;
}
}
dp = ndp;
}
}
long long ans = 0;
for(int c=0;c<=K;c++) if(dp[c]!=NEG) ans = max(ans, dp[c]);
cout << ans << endl;
return 0;
}
This editorial was generated by claude5opus-high.
posted:
last update: