公式

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

claude5.5opus-high

Overview

This problem asks us to count the number of ways to choose the face (front or back) for each card such that there are exactly \(K\) adjacent pairs of cards with equal numbers on their top faces. We can solve this with dynamic programming (DP) where the state tracks the “orientation of the previous card” and the “number of matching adjacent pairs so far”.

Analysis

Naive Approach Each card has 2 possible orientations, giving \(2^N\) combinations in total. For \(N=2000\), a brute-force search is far too slow to pass within the time limit.

Key Observations - Whether the top faces of card \(i\) and card \(i+1\) are equal depends solely on the orientations of these two cards. - Therefore, when deciding the orientations from left to right, we only need to keep track of two pieces of information: - The orientation of the previous card (front or back) - The number of matches counted so far - The orientations of earlier cards do not affect any future decisions.

Thus, the number of states is bounded by roughly \(2 \times (K+1)\), allowing us to count efficiently using DP.

Concrete Example Consider 2 cards with \((A,B) = (1,2), (2,1)\). There are 4 possible pairs of top faces:

1st card 2nd card Match
Front \(1\) Front \(2\) No
Front \(1\) Back \(1\) Yes
Back \(2\) Front \(2\) Yes
Back \(2\) Back \(1\) No

Therefore, when \(K=1\), the answer is \(2\).

Algorithm

Let \(\mathrm{dp}_s[j]\) be defined as “the number of ways to choose orientations up to the current card such that the last card has orientation \(s\) (\(0\) = front, \(1\) = back) and there are exactly \(j\) matches.”

  • Initialization: The first card can be placed either front or back, with 0 matches, so we set \(\mathrm{dp}_0[0]=\mathrm{dp}_1[0]=1\).
  • Transitions: Transition from card \(i-1\) with orientation \(s\) to card \(i\) with orientation \(t\).
    • If the values on the top faces \(v_{i-1}(s)\) and \(v_i(t)\) are equal, the match count increases by 1: \(\mathrm{dp}'_t[j+1] \mathrel{+}= \mathrm{dp}_s[j]\)
    • If they are not equal, the match count remains unchanged: \(\mathrm{dp}'_t[j] \mathrel{+}= \mathrm{dp}_s[j]\)
    • There are \(2 \times 2 = 4\) pairs of \((s, t)\), and we apply the update above for each pair.
  • Answer: The sum \((\mathrm{dp}_0[K]+\mathrm{dp}_1[K])\) modulo \(998244353\).

States where the match count exceeds \(K\) do not contribute to the answer, as the match count never decreases. Hence, an array length of \(K+1\) is sufficient.

Note that even if a card has the same number on both sides, orientations \(0\) and \(1\) are treated as distinct states. This naturally satisfies the requirement in the problem statement that front and back are distinguished.

Complexity

  • Time Complexity: \(O(NK)\)
  • Space Complexity: \(O(K)\) (since we only need to store the DP array from the previous step)

Implementation Notes

  • Using shifted arrays: A transition that increases the match count by 1 can be represented by shifting the array right by one position and adding [0]+dp[:-1]. Since the last element is truncated, states exceeding \(K\) matches are discarded automatically.

  • List-level operations: For each of the 4 transitions, determine whether to use the “shifted version” or the “original version”, and add them element-wise using zip. This runs fast even in Python because the lists are processed in bulk.

  • Take modulo properly: Remember to apply modulo \(998244353\) after every addition.

    Source Code

import sys
def main():
    data=sys.stdin.buffer.read().split()
    n=int(data[0]);K=int(data[1])
    A=[0]*n;B=[0]*n
    for i in range(n):
        A[i]=int(data[2+2*i]);B[i]=int(data[3+2*i])
    M=998244353
    L=K+1
    dp0=[0]*L;dp1=[0]*L
    dp0[0]=1;dp1[0]=1
    for i in range(1,n):
        pa=A[i-1];pb=B[i-1];ca=A[i];cb=B[i]
        sh0=[0]+dp0[:-1]
        sh1=[0]+dp1[:-1]
        a00=sh0 if pa==ca else dp0
        a10=sh1 if pb==ca else dp1
        a01=sh0 if pa==cb else dp0
        a11=sh1 if pb==cb else dp1
        n0=[(x+y)%M for x,y in zip(a00,a10)]
        n1=[(x+y)%M for x,y in zip(a01,a11)]
        dp0=n0;dp1=n1
    print((dp0[K]+dp1[K])%M)
main()

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