公式

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

claude5.5opus-high

Overview

In this problem, we need to count the number of ways to choose the face-up side of each card such that the number of adjacent pairs of cards with equal face-up values is exactly \(K\). This can be solved using dynamic programming (DP) with the state: “how many cards have been placed”, “how many equal adjacent pairs there are”, and “the orientation of the last card”.

Analysis

Drawbacks of the Naive Approach

Since each card has 2 orientations, there are \(2^N\) possible choices in total. For \(N \le 2000\), a brute-force search will easily exceed the time limit (TLE).

Key Observation

Whether the boundary between card \(i\) and card \(i+1\) forms an “equal adjacent pair” depends only on the orientations of card \(i\) and card \(i+1\). The orientations of the cards placed before them do not matter.

Therefore, when determining the orientations from left to right, we only need to keep track of the following two pieces of information:

  • How many equal adjacent pairs have occurred so far
  • Which orientation the previous (last) card was placed in

With this, we can count the number of ways using DP.

Example

Suppose there are 3 cards: \((1,2), (2,3), (2,1)\).

  • If we place the 1st card face-down (top side 2) and the 2nd card face-up (top side 2), they match at the first boundary.
  • If we place the 3rd card face-up (top side 2), they also match at the second boundary, resulting in a total of 2 matching positions.

In this way, whether each boundary matches can be determined solely by the orientations of the two adjacent cards.

Algorithm

We define \(dp[i][k][s]\) as follows:

The number of ways to choose the orientations of the first \(i\) cards from the left such that there are exactly \(k\) equal adjacent pairs, and the \(i\)-th card has orientation \(s\) (\(0\): face-up, \(1\): face-down).

Initialization

The 1st card can be either face-up or face-down, and there are no boundaries yet. Therefore, \(dp[1][0][0] = dp[1][0][1] = 1\).

Transitions

We try all pairs of orientation \(s\) for card \(i\) and orientation \(t\) for card \(i+1\). Let \(c_i(s)\) denote the number on card \(i\) in orientation \(s\) (\(c_i(0)=A_i\), \(c_i(1)=B_i\)).

\[ dp[i+1][k + [c_i(s) = c_{i+1}(t)]][t] \mathrel{+}= dp[i][k][s] \]

Here, \([\cdot]\) is the Iverson bracket notation, which evaluates to \(1\) if the condition is true and \(0\) if it is false.

Answer

\[ dp[N][K][0] + dp[N][K][1] \pmod{998244353} \]

Note that even if a card has the same number on both sides, the two orientations are treated as distinct choices. Since the above DP distinguishes the orientation \(s\), it naturally satisfies this rule.

Complexity

  • Time Complexity: \(O(N^2)\)
    • For each \(i\), \(k\) takes at most \(i\) values, and there are 4 combinations of \((s,t)\), totaling \(O(4 \cdot N^2/2)\) operations. For \(N=2000\), this takes roughly \(8 \times 10^6\) operations, which is sufficiently fast.
  • Space Complexity: \(O(N)\)
    • We only need to maintain the array dp for the previous step and nd for the next step, so \(O(N)\) space is sufficient.

Implementation Details

  • Array reuse: Whenever moving from step \(i\) to step \(i+1\), initialize the required range of nd (\(k = 0 \ldots i+1\)) to \(0\) before performing the transitions. Finally, copy nd back into dp.

  • Loop bounds: Up to the \(i\)-th card, there are \(i-1\) boundaries, so the number of equal adjacent pairs \(k\) is at most \(i-1\). Restricting the loop to this range avoids redundant operations.

  • Magnitude of values: Since \(A_i, B_i\) are at most \(10^9\), they fit in a 32-bit int. However, to be safe, the code reads them as long long and only uses them for equality comparisons.

  • Modulo operations: Take the modulo \(998244353\) after every addition to prevent integer overflow.

  • Case when \(N=1\): The loop does not execute at all, and for \(K=0\), the answer evaluates to \(2\). This is the correct result.

    Source Code

#include <bits/stdc++.h>
using namespace std;
const long long MOD=998244353;
int main(){
    int n,K;
    scanf("%d %d",&n,&K);
    vector<array<long long,2>> v(n);
    for(int i=0;i<n;i++){scanf("%lld %lld",&v[i][0],&v[i][1]);}
    vector<array<long long,2>> dp(n+1,{0,0}), nd(n+1,{0,0});
    dp[0][0]=1;dp[0][1]=1;
    for(int i=0;i+1<n;i++){
        for(int k=0;k<=i+1;k++){nd[k][0]=0;nd[k][1]=0;}
        for(int k=0;k<=i;k++){
            for(int s=0;s<2;s++){
                long long c=dp[k][s];
                if(!c)continue;
                for(int t=0;t<2;t++){
                    int nk=k+(v[i][s]==v[i+1][t]?1:0);
                    nd[nk][t]=(nd[nk][t]+c)%MOD;
                }
            }
        }
        for(int k=0;k<=i+1;k++){dp[k]=nd[k];}
    }
    long long ans=(dp[K][0]+dp[K][1])%MOD;
    printf("%lld\n",ans);
}

This editorial was generated by claude5.5opus-high.

投稿日時:
最終更新: