Official

C - No Streak Editorial by evima


For ease of explanation, we will restate the problem in terms of strings, as follows:

How many strings \(S\) consisting of characters A, B, and X satisfy the following conditions?

  • The string \(S\) contains exactly \(a\) occurrences of A, \(b\) occurrences of B, and \(x\) occurrences of X.
  • Neither AA nor BB appears as a substring.
  • For every prefix \(T\) of \(S\), the number of A in \(T\) is at least the number of B in \(T\).

We count this in a manner analogous to Catalan number arguments. Recall that one way to derive the usual Catalan numbers is to count paths that never go below the diagonal, using the idea of flipping A and B after the crossing point to derive that the answer is (the number of ways to reach \((a, b)\)) - (the number of ways to reach \((b-1, a+1)\)) = \(\binom{a+b}{a}\) minus \(\binom{a+b}{b-1}\). We apply this method here.

In this problem, the extra condition is that neither AA nor BB can appear. We again use the approach of counting strings where A and B are flipped after crossing the diagonal and subtracting them from the total.

The condition “neither AA nor BB appears” is mostly unaffected even if A and B are flipping after crossing the diagonal. However, we must care that immediately after the diagonal crossing, BB and BX are allowed but BA is not allowed.

Thus, roughly speaking, the answer can be found as follows:

  1. From (the number of ways to reach \((a, b, x)\)),
  2. subtract (the number of ways to reach \((b-1, a+1, x)\) with BB immediately after the diagonal crossing) and
  3. (the number of ways to reach \((b-1, a+1, x)\) with BX immediately after the diagonal crossing).

Let \(f(a, b, x)\) denote the number of ways to arrange \(a\) As, \(b\) Bs, and \(x\) Xs so that neither AA nor BB appears as a substring. Then, the above three numbers can be represented as follows:

  1. \(f(a,b,x)\) ways
  2. We can correspond such sequences with the ones where BB are replaced with B, so \(f(b-1,a,x)\) ways
  3. We have two cases based on whether the character following the BX is B or (A|X) (this character always exists):
    • BXB: We can correspond such sequences with the ones where BXB are replaced with B, so \(f(b-1,a,x-1)\) ways
    • BX(A|X): We can correspond such sequences with the ones where BX are replaced with B, so \(f(b-1,a+1,x-1)\) ways

Thus, if we can compute \(f(a,b,x)\) in linear time, we can solve the entire problem efficiently.

To compute \(f(a,b,x)\), proceed as follows. Suppose \(a>0\) by handling \(a=0\) as a corner case. Let \(k\) be the number of times two As appear consecutively when only looking at A and X in the string \((0 \leq k \leq a-1)\). Then:

  • There are \(\binom{a-1}{k}\) ways to choose the positions for adjacent As.
  • There are \((a-1) - k + 2 = a - k + 1\) gaps where Xs may be inserted, and for \(a-k-1\) of them we must insert at least one X. Thus, there are \(\binom{x - (a - k - 1) + (a-k)}{(a-k)} = \binom{x + 1}{a - k}\) ways to put Xs.
  • There are \(a + x + 1\) gaps where Bs may be inserted, and for \(k\) of them we must insert Bs. Thus, there are \(\binom{a+x+1-k}{b-k}\).

\(f(a, b, x)\) is the product of these, so we can compute it in linear time.

Therefore, the problem can be solved in \(\mathrm{O}(N)\) time or similar, which is sufficiently fast.

posted:
last update: