Official

D - 同じ番号で接するカード / Cards Touching with the Same Number Editorial by admin

imported

Overview

We choose which side of each card faces up so that there are exactly \(K\) places where adjacent cards have equal numbers. We can count the number of choices that satisfy this condition using dynamic programming, adding cards one by one from left to right.

Analysis

Since each card has \(2\) choices, enumerating all combinations requires checking \(2^N\) possibilities. This approach will not run in time for \(N \leq 2000\).

Suppose we have determined the face-up sides for the first few cards from the left. When adding the next card, the only new position to check is the single adjacent pair between this card and the immediately preceding one. The choices made for cards further to the left will not affect future transitions. Therefore, it is sufficient to keep track of the number of equal adjacent pairs so far and which side of the last card is facing up.

For the first \(i\) cards from the left, let \(\mathrm{dp}[j][s]\) be the number of choices such that the number of equal adjacent pairs is \(j\) and the side of the last card is \(s\). We denote the front side as \(0\) and the back side as \(1\), and let \(V_{i,s}\) be the number on side \(s\) of the \(i\)-th card.

When we next add card \(i+1\) with side \(t\) facing up, the number of equal adjacent pairs increases by \(1\) if \(V_{i,s}=V_{i+1,t}\), and remains unchanged otherwise. We can simply add \(\mathrm{dp}[j][s]\) to the corresponding next state. Since the number of equal pairs never decreases, there is no need to keep track of states where it exceeds \(K\).

For the first card, there are no adjacent pairs, and there is \(1\) choice for each of the front and back sides. Any subsequent sequence of choices can be uniquely decomposed into “the choices up to the previous card” and “the side of the new card”, and the above transitions consider each such combination exactly once. Therefore, the states at each step represent the number of choices as defined, and the final answer is the sum of the \(2\) states where the number of equal pairs is \(K\).

Even if the numbers on the front and back of the same card are equal, we still keep the states and transitions distinct based on the side. This ensures that the choices between the front and back sides, which are considered distinct in the problem, are counted correctly.

Algorithm

  1. Initialize \(\mathrm{dp}[0][0]=\mathrm{dp}[0][1]=1\), and set all other entries to \(0\).
  2. Process the cards from the \(2\)nd card onward in order from left to right. Initialize the array for the next step with all \(0\)s.
  3. Iterate over the number of equal adjacent pairs \(j\) so far, the previous side \(s\), and the added side \(t\). Compare the numbers on both sides to find the new number of equal pairs, and if it is at most \(K\), add to the corresponding state of the next step. Perform all additions modulo \(998244353\).
  4. Replace the current array with the array for the next step.
  5. After processing all cards, output \(\mathrm{dp}[K][0]+\mathrm{dp}[K][1]\) modulo \(998244353\).

Complexity

  • The time complexity is \(O(N(K+1))\). For each card, we examine at most \(K+1\) possibilities for the number of equal pairs and \(4\) combinations of sides. It is written as \(K+1\) because it includes \(K=0\).

  • The space complexity is \(O(N+K)\). It uses \(O(N)\) space for the input and \(O(K)\) space for the states of the current and next steps.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    const int mod = 998244353;
    int n, k;
    cin >> n >> k;
    vector<array<int, 2>> a(n);
    for (auto &card : a) cin >> card[0] >> card[1];
    vector<array<int, 2>> dp(k + 1), next_dp(k + 1);
    dp[0] = {1, 1};
    for (int i = 1; i < n; ++i) {
        fill(next_dp.begin(), next_dp.end(), array<int, 2>{0, 0});
        for (int count = 0; count <= min(k, i - 1); ++count) {
            for (int prev = 0; prev < 2; ++prev) {
                for (int side = 0; side < 2; ++side) {
                    int nc = count + (a[i - 1][prev] == a[i][side]);
                    if (nc > k) continue;
                    int &value = next_dp[nc][side];
                    value += dp[count][prev];
                    if (value >= mod) value -= mod;
                }
            }
        }
        dp.swap(next_dp);
    }
    cout << (dp[k][0] + dp[k][1]) % mod << '\n';
}

This editorial was generated by imported.