7

I am trying to find the number of ways to arrange seven of the letter $D$s and eight $R$s, in such a way that no four consecutive letters are the same. In other words, there cannot be any $DDDD$ or $RRRR$. Thus, for example, $DRDDRRDDDRRDRRR$ would be a valid sequence while $DDDD...$ is not. I first found the number of ways to arrange all the ltters without restriction, $\binom{16}{9}$. I know that I am to subtract the number of invalid letter sequences, and here is where I am having difficulty. I first tried grouping together 4 $D$s, count the number of ways to rearrange one such block and the rest of the letters, do the same thing with grouping together 4 $R$s, and then adding the number of ways to arrange a block of four $D$s, a block of four $R$s, and the rest of the letters. The problem with this approach is that it risks multiple counting. For example, $DDDDRRRDDDRRRRR$ may be counted twice, when we consider the blocks $DDDDRRRDDD(RRRR)R$ or $DDDDRRRDDDR(RRRR)$, with $()$ considered as a single block.

I am wondering how I can efficiently get the answer.

  • 1
    Is it 8 or 9? The title says 9 but the question says 8. – Lucenaposition Aug 30 '24 at 00:33
  • This sounds like a job for the Principle of Inclusion and Exclusion! Your example, by the way, would be triple-counted -- once for the $4$ consecutive $D$s, and twice for the $5$ consecutive $R$s. – Robert Shore Aug 30 '24 at 00:44
  • @user2661923 I do not fully know what you're doing. I get the idea, but the devil's in the details. $\quad$ You (possibly?) have issues with the end (and start?) being possibly empty, so you might double count a solution for $x_4 = 0 $ with a solution for up to $x_3> 0$. Do you see how I worked around that? – Calvin Lin Aug 30 '24 at 03:26
  • @user2661923 FYI Nitpicking, you technically want a composition where order matters, and not a partition (where order doesn't matter). IE 1-3-3 leads to a different result as 3-1-3. $\quad$ Of course, you might already be intending that, but just haven't written the details explicitly. – Calvin Lin Aug 30 '24 at 03:30
  • This is a great question, thanks for sharing it! I enjoyed thinking about it even though I got nothing – Elmina Aug 30 '24 at 06:16

5 Answers5

2

Your example has 3296 ways.

I don't know a short "math way" but here's a simple Python script with a brute force solution for checking and a faster solution that you could do by hand (draw a $(N+1) \times (N+1)$ table where $N=\max(D,R)$ and fill in the values):

D = 7
R = 8
no = 4

from itertools import product print(sum( s.count('D') == D and 'D'no not in s and 'R'no not in s for s in map(''.join, product('DR', repeat=D+R)) ))

from functools import cache @cache def ways(x, y): if y == 0: return 1 if x < no else 0 return sum(ways(y, x-i) for i in range(1, min(no, x+1))) print(ways(D, R) + ways(R, D))

My ways(x, y) tells the number of ways to arrange x of one letter and y of another letter, with the arrangements starting with (at least) one of the x. (And without no consecutive equal letters.)

Output (Attempt This Online!):

3296
3296

Here's the table (the answer 3296 is the sum of 1439 and 1857, and for example 1439 gets computed as 124+385+930):

   1    1    1    1    0    0    0    0    0
   0    1    2    3    3    2    1    0    0
   0    1    3    6    9   10    9    6    3
   0    1    4   10   19   28   34   34   28
   0    0    3   12   31   59   90  115  124
   0    0    2   12   40   97  184  289  385
   0    0    1   10   44  133  308  582  930
   0    0    0    6   40  155  438  986 1857
   0    0    0    3   31  155  537 1439
1

The same generating function solution I gave for this other question works here too. Only thing that needs to be changed in the fractions in the denominator is like this:

$$ g(x,y) = \frac{1}{1-x-y + \frac{x^4}{1+x+x^2+x^3} + \frac{y^4}{1+y+y^2+y^3} } $$

I don't know if this simplifies nicely but here's a Sage-code to do the coefficient extraction (Btw, how does one get the coefficient from a PowerSeriesRing element? In the below code I just iterate through all and check if it's the wanted one.)

def f(m,n,r):
    R.<x,y> = PowerSeriesRing(QQ, 2, default_prec=m+n)
    g1X = sum(x^j for j in range(r))
    g1Y = sum(y^j for j in range(r))
    g = 1/(1-x-y+x^r/g1X+y^r/g1Y)
    gD = g.dict()
    for gKey in gD:
        if tuple(gKey)==(m,n):
            return gD[gKey]
    return 0

print (f(7,8,4))

Gives the answer $3296$.

ploosu2
  • 12,367
1

We consider a binary alphabet built from letters $\mathcal{V}=\{D,R\}$. Words which do not have any consecutive equal letters are called Smirnov words. A generating function for Smirnov words is given as \begin{align*} \left(1-\frac{Dz}{1+Dz}-\frac{Rz}{1+Rz}\right)^{-1}\tag{1} \end{align*} The coefficient $[z^n]$ of $z^n$ in the series (1) gives the number of binary words of length $n$ which do not have any consecutive letters. In the current problem we have to avoid runs of length $4$ of the letters $D$, resp. $R$. We respect this by substituting in (1) each occurrence of $Dz$ resp. $Rz$ by substrings of length $\leq 3$. \begin{align*} Dz&\to Dz+(Dz)^2+(Dz)^3\\ Rz&\to Rz+(Rz)^2+(Rz)^3 \end{align*}

We obtain \begin{align*} \color{blue}{A(z;D,R)} &=\left(1-\frac{Dz+(Dz)^2+(Dz)^3}{1+Dz+(Dz)^2+(Dz)^3} -\frac{Rz+(Rz)^2+(Rz)^3}{1+Rz+(Rz)^2+(Rz)^3}\right)^{-1}\\ &=\left(1-\frac{Dz\,\frac{1-(Dz)^3)}{1-Dz}}{\frac{1-(Dz)^4}{1-Dz}} -\frac{Rz\,\frac{1-(Rz)^3)}{1-Rz}}{\frac{1-(Rz)^4}{1-Rz}}\right)^{-1}\\ &\,\,\color{blue}{=\left(1-\frac{Dz\left(1-(Dz)^3\right)}{1-(Dz)^4} -\frac{Rz\left(1-(Rz)^3\right)}{1-(Rz)^4}\right)^{-1}} \end{align*}

With some help of Wolfram Alpha we obtain \begin{align*} {\color{blue}{[z^{15}]A(z;D,R)}}&=D^{12} R^3 + 65 D^{11} R^4 + 546 D^{10} R^5 + 1\,860 D^9 R^6 + 3\,296 D^8 R^7\\ &\qquad{+\color{blue}{3\,296 D^7 R^8 }}+ 1\,860 D^6 R^9 + 546 D^5 R^{10} + 65 D^4 R^{11} + D^3 R^{12} \end{align*} in accordance with the result given in some other answers.

Note: Smirnov words can be found for instance in example III.24 in Analytic Combinatorics by P. Flajolet and R. Sedgewick.

Markus Scheuer
  • 112,413
0

Addendum added to respond to a comment-request for a closed form formula for $~f(k,n).$


This is not an answer.
Instead this is merely a comment that describes an alternative approach.

Stars and Bars provides a (somewhat ugly) alternative approach. See this answer as a model of how to compute the enumeration factors discussed later in this response.

There are $8$ different ways of partitioning $7$, so that none of the terms are greater than $3$. These are

3-3-1, 3-2-2, 3-2-1-1, 3-1-1-1-1
2-2-2-1, 2-2-1-1-1, 2-1-1-1-1-1, 1-1-1-1-1-1-1-1.

For each such (mutually exclusive) partitioning, you would need to take the product of a scaling factor times a enumeration factor.

Then, you would sum the $8$ products together.

For example, the 3-2-1-1 has a scaling factor of $4 \times 3 = 12,$ since there are $4$ choices for the position of the $3,$ and then $3$ choices for the position of the $2.$

Further, consider the following tableau:

- DDD - DD - D - D -

There are $5$ islands created by this partitioning. Reading the islands left to right, let $x_1, \cdots, x_5$ denote the size of these islands. The islands represent the potential locations of the $9$ R's.

Let $k$ denote the number of these islands. So, in this situation, $~k = 5.$

Then, the enumeration factor will be the number of solutions to

  • $x_1 + \cdots + x_k = 9.$

  • $x_1, \cdots, x_k \in \Bbb{Z_{\geq 0}}$.

  • $x_1, \cdots, x_k \leq 3.~$

  • $x_2, \cdots, x_{k-1} \geq 1.$

The last bullet point above is used to preserve the integrity of the 3-2-1-1 structure. The corresponding enumeration factor is computed following the model, linked near the start of this response.

The last bullet point disrupts the symmetry that might otherwise be found in computing the enumeration factor, so the math does get messy.

This is why I am providing a long-winded comment, rather than a complete answer. It is because the math gets messy.


$\underline{\text{Addendum}}$

Per request, providing a closed form formula for $~f(k,n),~$ which corresponds to the enumeration of the following:

  • $x_1 + \cdots + x_k = n.$

  • $x_1, \cdots, x_k \in \Bbb{Z_{\geq 0}}.$

  • $x_1, \cdots, x_k \leq 3.~$

  • $x_2, \cdots, x_{k-1} \geq 1.$

To simplify the formula, I am going to adopt the convention that $~\displaystyle \binom{a}{b} = 0,~$ whenever $~a < b.$

The first step is to eliminate the last bullet point above, through the change of variables : $~y_i = x_i - 1 ~: ~i \in \{2,\cdots,k-1\}.$

This changes the enumeration problem to :

  • $x_1 + y_2 + \cdots + y_{k-1} + x_k = n - (k-2) = n + 2 - k.$

  • $x_1, y_2, \cdots, y_{k-1}, x_k \in \Bbb{Z_{\geq 0}}.$

  • $x_1, x_k \leq 3.~$

  • $y_2, \cdots, y_{k-1} \leq 2.$

The enumeration will be expressed as

$$\sum_{r=0}^k (-1)^r T_r.$$

Then,

$$T_0 = \binom{[n + 2 - k] + [k-1]}{k-1} = \binom{n+1}{k-1}.$$

Then, there are $~2~$ choices for the $~x_1~$ or $~x_k~$ variable being the sole violation, and $~k-2~$ choices for one of the remaining variables being in violation. So,

$$T_1 = \left[ ~2 \times \binom{n-3}{k-1} ~\right] + \left[ ~(k-2) \times \binom{n-2}{k-1} ~\right].$$

To compute $~T_r ~: ~r \in \{2,3,\cdots,k\},~$ you have to distinguish $~3~$ mutually exclusive cases, depending on how many of the variables $~\{x_1,x_k\}~$ are included in the $~r~$ variables in violation.

Let

  • $T_r(0)~$ denote that none of $~\{x_1,x_k\}~$ are included.

  • $T_r(1)~$ denote that one of $~\{x_1,x_k\}~$ is included.

  • $T_r(2)~$ denote that both of $~\{x_1,x_k\}~$ are included.

Then, you have that

$$T_r = T_r(0) + T_r(1) + T_r(2),$$

where,

$$T_r(0) = \binom{k-2}{r} \times \binom{2}{0} \times \binom{n+1 - 3r}{k-1}.$$

$$T_r(1) = \binom{k-2}{r-1} \times \binom{2}{1} \times \binom{n+1 - 3r - 1}{k-1}.$$

$$T_r(2) = \binom{k-2}{r-2} \times \binom{2}{2} \times \binom{n+1 - 3r - 2}{k-1}.$$

user2661923
  • 42,303
  • 3
  • 21
  • 46
  • I don't think my math is messy, in the sense that the ideshould be clear (with explicit calculations not done, which granted can be tedious) – Calvin Lin Aug 30 '24 at 03:48
  • 1/ The scaling factor is what brings your "sum over all partitions of size $d$" to my "compositions of size $d$" which is the $D_d$. $\quad$ 2/ Likewise, your enumeration factor is the number of compositions of R into size $d-1$, $d$ (twice) or $d+1$, namely $R_{d-1} + 2R_d + 2R_{d+1}$. (Why?) $\quad$ 3/ Multiply them together and you get my expression, which we sum over $d$. $\quad$ 4/ It is my opinion that my solution is much better organized and focuses on the crucial parts (combining unnecessary calculation) – Calvin Lin Aug 30 '24 at 03:54
  • @CalvinLin "... much better organized..." : I believe you. My approach is very messy. – user2661923 Aug 30 '24 at 04:01
  • yup i saw that, which is why I deleted my comment. It takes time to update. lol. $\quad$ Also, I'm terrible with editing comments after posting. – Calvin Lin Aug 30 '24 at 04:05
  • FWIW, whether it's counting my $R_r$ or your enumeration factor, for lager values, if I couldn't use a computer to calculate the generating function, then instead of manually counting, I would ultimately use PIE as expressed by Mike in the linked post. $\quad$ Your $0 \leq d_1 \leq 3$ makes the calculation less symmetric. Per Marc's post, when all the terms have identical restrictions, we have an nice-ish expression. You can work around it by conditioning on $x_1 = 0 $, $x_k = 0 $, which is how we get my $R_{d-1} , R_{d+1}$. – Calvin Lin Aug 30 '24 at 04:14
  • @CalvinLin Yes, I understand that I can routinely compute $~f(k)~$ and provide a closed form formula for $~f(k).~$ Then, the subsequent math is okay. It is simply that computing $~f(k)~$, where $~x_2,\cdots,x_{k-1} \geq 1,~$ will be badly stretching the intuition of the original poster. As far as P-I-E goes, I looked at that, and saw some major ugliness. Given that I can routinely compute a closed form formula for $~f(k),~$ it is hard to believe that raw P-I-E would be better. – user2661923 Aug 30 '24 at 04:19
  • Intersting, I would love to see that. For $f(k, n) $ ($k$ terms summing to $n$), we can write $f(k+1,, n) = \sum f(k, n-i)$ (or something like that), but I don't immediately see how we can hold $n$ constant and get a formula for it. Like what could yield my $D_i$ sequence (possibly convoluting with $(1, 2, 1)$ to turn into the enumeration factor). – Calvin Lin Aug 30 '24 at 04:43
  • @CalvinLin See the Addendum that I have just added to the end of my answer. – user2661923 Aug 30 '24 at 05:39
0

Apart from PIE (not as obvious as you might think) and generating functions (which likely is more machinery than OP wants), this is a nice brute force approach for small-ish numbers.

Show the following:

  • Consider a valid expression. Suppose that there are $d$ blocks of D's and $r$ blocks of $R$, then $ | d - r | \leq 1$.
    • Note. These are the number of "full, complete, non-empty" blocks. So $DDRRRDDRRDDRDRR$ has 4 blocks of D's and 4 blocks of R's.
  • Let there be $D_d$ ways for $d$ values $1 \leq d_i \leq 3$ such that $\sum d_i = 7$. Likewise, let there be $R_r$ ways for $r$ values $1 \leq r_i \leq 3$ such that $\sum r_i = 8$ (or 9, if that's what you want).
    • Determine these values explicitly. Apart from manually listing, you can use PIE here.
    • You an verify that $D_1, D_2, D_3, \ldots = 0, 0, 6, 16, 15, 6, 1, 0 \ldots $. Find $R_i$ yourself.
  • Then, the total number of ways is $\sum D_i R_{i+1} + 2D_i R_i + D_i R_{i-1}$.
Calvin Lin
  • 77,541
  • 1/ To be fair, I just used Generating functions to calculate $D_i$. 2/ I expected OP to just manually count, since the numbers are small-ish. 3/ I guess i should make it much clearer that these are the "full complete blocks"? You cannot intertwine D_D_D_D_D_D_D and RRR_RRR_RRR – Calvin Lin Aug 30 '24 at 03:23