公式

D - 同じ番号で接するカード / Cards Touching with the Same Number 解説 by admin 印刷ページ

Gemini 3.8 Flash (High)

Overview

For \(N\) cards, we choose whether to place each card face up or face down. We need to find the number of ways to choose the orientations such that there are exactly \(K\) positions where adjacent cards have the same number on their top faces, modulo \(998244353\).

Analysis

Naive Approach and Its Limitations

Since each card has \(2\) choices (face up or face down), there are \(2^N\) total combinations of card orientations. Because \(N \leq 2000\), a brute-force search trying all \(2^{2000}\) combinations will not finish within the time limit, resulting in TLE (Time Limit Exceeded).

Formulating Dynamic Programming (DP)

Consider deciding the orientation of the cards one by one from left to right. When deciding the orientation of the \(i\)-th card from the left, the only information needed to determine whether “the numbers on the top faces of adjacent cards are equal” is “which face was chosen for the immediately preceding \((i-1)\)-th card (and what number was on its top face)”. We do not need to keep track of the specific choices for any cards before that.

Therefore, we can apply dynamic programming (DP) with the following state information: - How many cards have been decided so far - How many matching positions on the top face have occurred so far (\(0 \le j \le K\)) - Which face was chosen for the immediately preceding card (front or back)

States where the number of matching positions exceeds \(K\) cannot satisfy the final condition, so we do not need to consider them. This significantly reduces the number of states to explore.

Algorithm

Define the DP table as follows:

  • \(dp[j][side]\): The number of ways to choose orientations such that the most recently chosen face is \(side\) (\(0\): front, \(1\): back) and there are \(j\) positions where adjacent cards have matching top faces.

We update the table as we process the cards one by one.

  1. Initialization: When placing the 1st card, there are no adjacent cards, so the number of matching positions is \(0\).

    • \(dp[0][0] = 1\) (1st card face up / front)
    • \(dp[0][1] = 1\) (1st card face down / back)
    • All other elements are \(0\)
  2. Transitions: For the \(i\)-th card (\(i = 2, 3, \dots, N\)), we consider all combinations of the previous card’s face \(prev\_side \in \{0, 1\}\) and the current card’s face \(curr\_side \in \{0, 1\}\) (\(2 \times 2 = 4\) combinations).

    • Let \(prev\_val\) be the value on the previous card’s top face, and \(curr\_val\) be the value on the current card’s top face.
    • If \(prev\_val == curr\_val\), the number of matching positions increases by \(1\), so \(match = 1\); otherwise, \(match = 0\).
    • For \(j\) satisfying \(j + match \leq K\), add the value to the new state: $\(next\_dp[j + match][curr\_side] \mathrel{+}= dp[j][prev\_side]\)$
  3. Answer: After deciding all \(N\) cards, the answer is the sum of the number of ways where the number of matching positions is exactly \(K\), which is \(dp[K][0] + dp[K][1]\).

Complexity

  • Time Complexity: \(O(NK)\) For \(N\) cards, the loop over the number of matches runs at most \(K\) times, with \(2 \times 2 = 4\) transitions for the orientations. In the worst case, this requires at most \(2000 \times 2000 \times 4 \approx 1.6 \times 10^7\) operations, which easily runs within the time limit (2 seconds).
  • Space Complexity: \(O(K)\) Since computing the \(i\)-th card only requires the results from the \((i-1)\)-th card, reusing the DP array (rolling array) keeps the memory consumption down to \(O(K)\).

Implementation Notes

  • When the same integer is written on the front and back: The problem statement notes that “choosing to place a card face up and choosing to place it face down are distinguished”, so no special branching is required even if \(A_i = B_i\). Calculating transitions independently for choosing front and back will yield the correct result.

  • Preventing Out-of-Bounds Access: Since the new number of matches will be \(j + match\), ensure that transitions are skipped if \(j + match > K\).

  • Using ModInt: The answer must be modulo \(998244353\). Using utilities like atcoder::modint998244353 from the AtCoder Library (ACL) prevents missing modulo operations or overflow during addition, keeping the code clean and simple.

    Source Code

#include <iostream>
#include <vector>
#include <atcoder/modint>

using namespace std;
using mint = atcoder::modint998244353;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k;
    if (!(cin >> n >> k)) return 0;

    vector<long long> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }

    // dp[j][side]: 一致する箇所が j 箇所で、直前のカードの面が side (0: 表, 1: 裏) である通り数
    vector<vector<mint>> dp(k + 1, vector<mint>(2, 0));
    dp[0][0] = 1;
    dp[0][1] = 1;

    for (int i = 1; i < n; ++i) {
        vector<vector<mint>> next_dp(k + 1, vector<mint>(2, 0));
        long long prev_vals[2] = {a[i - 1], b[i - 1]};
        long long curr_vals[2] = {a[i], b[i]};

        int max_j = min(i - 1, k);
        for (int j = 0; j <= max_j; ++j) {
            for (int prev_side = 0; prev_side < 2; ++prev_side) {
                if (dp[j][prev_side].val() == 0) continue;
                for (int curr_side = 0; curr_side < 2; ++curr_side) {
                    int match = (prev_vals[prev_side] == curr_vals[curr_side]) ? 1 : 0;
                    if (j + match <= k) {
                        next_dp[j + match][curr_side] += dp[j][prev_side];
                    }
                }
            }
        }
        dp = move(next_dp);
    }

    mint ans = dp[k][0] + dp[k][1];
    cout << ans.val() << "\n";

    return 0;
}

This editorial was generated by gemini-3.8-flash-high.

投稿日時:
最終更新: