D - 同じ番号で接するカード / Cards Touching with the Same Number Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
We process the cards from left to right and count the number of valid choices using dynamic programming, keeping track of the “number of adjacent pairs with equal top-face integers” and the “orientation of the last card.”
Analysis
We Cannot Try All Possible Choices
Since each card has \(2\) choices (front or back), there are \(2^N\) configurations in total. Checking all of them will not finish in time for \(N \leq 2000\).
The Number of Newly Formed Matches Depends Only on the Immediately Preceding Card
Consider deciding the orientation of the \((i+1)\)-th card after deciding the orientations of the first \(i\) cards from the left.
The only place where a new match can potentially be formed is between the \(i\)-th card and the \((i+1)\)-th card. The number of matches before that does not change.
For example, if the top face of the \(i\)-th card is \(3\), and the \((i+1)\)-th card has \(3\) on its front and \(5\) on its back:
- If the \((i+1)\)-th card is placed front-up, the number of matches increases by \(1\).
- If the \((i+1)\)-th card is placed back-up, the number of matches does not increase.
Therefore, we do not need to remember the orientations of all previous cards. To add the next card, we only need the following two pieces of information:
- The number of matches so far
- The orientation of the last card
Since the number of matches alone is insufficient to determine whether a match occurs with the next card, we also include the orientation of the last card in the state.
Furthermore, because the number of matches never decreases, any state exceeding \(K\) matches does not need to be considered going forward.
Algorithm
1. DP State
Let the integer on the top face of card \(i\) be represented according to its orientation \(s\) as follows:
- \(V_i(0)=A_i\): front side up
- \(V_i(1)=B_i\): back side up
After processing the first \(i\) cards from the left, we define:
\(dp[j][s] =\) the number of ways to choose orientations such that the number of matches is exactly \(j\) and the orientation of the \(i\)-th card is \(s\).
Here, \(j\) is an integer between \(0\) and \(K\) inclusive, and \(s \in \{0, 1\}\). All values are computed modulo \(998244353\).
2. Initial State
With only the \(1\)st card, there are no adjacent cards, so the number of matches is \(0\).
Since there is \(1\) way to place it front-up and \(1\) way to place it back-up, we set:
\(dp[0][0]=1,\quad dp[0][1]=1\)
All other entries are initialized to \(0\).
Even if \(A_1=B_1\), choosing to place the front side up and choosing to place the back side up are distinguished, so this initialization remains unchanged.
3. Transitions
Assuming the first \(i\) cards from the left have been processed, we add the \((i+1)\)-th card.
Let \(s\) be the orientation of the \(i\)-th card and \(t\) be the orientation of the \((i+1)\)-th card. The number of newly added matches, \(add\), is:
- \(add=1\) if \(V_i(s)=V_{i+1}(t)\)
- \(add=0\) otherwise
Therefore, for states satisfying \(j+add \leq K\), we update:
\(ndp[j+add][t] \mathrel{+}= dp[j][s]\)
There are \(4\) possible pairs for \((s, t)\): \((0,0),(0,1),(1,0),(1,1)\). After updating for all pairs, we swap \(dp\) and \(ndp\).
In this transition, we extend each existing choice of orientations by adding the front or back of the next card. Since the original choice and the added orientation can be uniquely determined from the resulting choice, we can count without omissions or duplicates.
4. Answer
After processing all cards, as long as the number of matches is \(K\), the orientation of the last card can be either, so the answer is:
\((dp[K][0]+dp[K][1]) \bmod 998244353\)
Complexity
- Time Complexity: \(O(N(K+1))\)
- Space Complexity: \(O(K+1)\)
For each card, we check \(4\) pairs of orientations and at most \(K+1\) values for the number of matches.
The DP only needs to store the previous step and the updated step, and storing the card integers for just the previous and current cards is sufficient. Note that the complexity is expressed using \(K+1\) to account for the case where \(K=0\).
Implementation Notes
Do not combine front and back even if they have the same integer
Even if \(A_i=B_i\), choosing the front vs. back orientation are distinct choices. In the code, by treating front and back as separate states, we count correctly.Initialize the destination array every time
Initialize all elements ofndpto \(0\) before updating. Directly updating in-place within the same array would lead to erroneous transitions (as if reusing the newly added card), so we keepdpandndpseparate.Only iterate over reachable ranges
If \(i\) cards have been processed, the number of matches is at most \(i-1\). Furthermore, to avoid exceeding \(K\) after the update, the code sets:
limit = min(i - 1, K - add)
Iflimitis negative, the corresponding transition is not executed.Take modulo after each addition
Since each value is always kept strictly less than \(MOD\), if the value is \(\geq MOD\) after addition, subtracting \(MOD\) once is sufficient.Handles \(N=1\) naturally
In this case, \(K=0\) by the constraints. Without performing any transitions, the \(2\) ways (front and back) become the answer.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
constexpr int MOD = 998244353;
int N, K;
cin >> N >> K;
array<int, 2> prev;
cin >> prev[0] >> prev[1];
vector<array<int, 2>> dp(K + 1, {0, 0});
vector<array<int, 2>> ndp(K + 1, {0, 0});
dp[0] = {1, 1};
for (int i = 1; i < N; ++i) {
array<int, 2> cur;
cin >> cur[0] >> cur[1];
fill(ndp.begin(), ndp.end(), array<int, 2>{0, 0});
for (int s = 0; s < 2; ++s) {
for (int t = 0; t < 2; ++t) {
int add = (prev[s] == cur[t]);
int limit = min(i - 1, K - add);
for (int j = 0; j <= limit; ++j) {
int &v = ndp[j + add][t];
v += dp[j][s];
if (v >= MOD) v -= MOD;
}
}
}
dp.swap(ndp);
prev = cur;
}
int ans = dp[K][0] + dp[K][1];
if (ans >= MOD) ans -= MOD;
cout << ans << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: