The intimidated chickens problem
(Saad Mneimneh and Alexey Nikolaev)

The 2017 Raytheon Mathcounts National Competition contained the following question: In a barn, 100 chicks sit peacefully in a cricle. Suddenly, each chick randomly pecks the chick immediately to its left or right. What is the expected number of unpecked chickens? (the answer is 100/4=25) This question inspired the intimidated chickens problem:

Consider $n$ chickens sitting in a row. Each chicken is oriented to look either to the left or to the right. We say that chicken $i$ is intimidated iff chickens $i-1$ and $i+1$ are both looking at it. A good orientation of the chickens is one in which no chicken is intimidated (the second chicken below is intimidated).




This interesting formulation is related to a famous sequence known as the quarter-squares: $a_n=\lfloor n/2\rfloor\lceil n/2\rceil=\lfloor n^2/4\rfloor$, for $n\geq 0$: $$0, 0, 1, 2, 4, 6, 9, 12, 16, 20, 25, 30, 36, 42, 49, 56, 64, 72, 81, 90, 100, 110, 121, 132, 144, \ldots$$ It turns out that the number of good orientations is $g_n=a_{n+2}, n\geq 0$. To derive this result, think of a binary word $b_1b_2\ldots b_n$, where $0$ represents left and $1$ represents right. As such, we must have $b_i\leq b_{i+2}$. So each of the sequences $b_1,b_3,\ldots$ and $b_2,b_4,\ldots$ must be non-decreasing, i.e. must switch from $0$ to $1$ at most once. Therefore, there are $\lceil n/2\rceil+1=\lceil (n+2)/2\rceil$ and $\lfloor n/2\rfloor+1=\lfloor (n+2)/2\rfloor$ possible sequences, respectively. By the product rule, this gives $a_{n+2}$.

Another interesting way to derive this result is to first establish the recurrence $g_n=2g_{n-1}-2g_{n-3}+g_{n-4}$, by emphasizing the start of the binary pattern: $$g_n=\underline{0}g_{n-1}+[\underline{1}g_{n-1} -\underline{100}g_{n-3}-(\underline{110}g_{n-3}-\underline{1100}g_{n-4})]$$ The characteristic equation of the above recurrence is $x^4-2x^3+2x-1=0$, which is the same as $(x-1)^3(x+1)=0$ with solutions $p=q=r=1$ and $s=-1$, resulting in $g_n=c_1(1)^n+c_2n(1)^n+c_3n^2(1)^n+c_4(-1)^n$. Given $g_0=1$, $g_1=2$, $g_2=4$, and $g_3=6$, we get $g_n=n+[7+2n^2+(-1)^n]/8$, which can be shown to be $a_{n+2}$ (e.g. by induction).

For large $n$, if each chicken is randomly oriented to the left or to the right with equal probability, then the probability of a good orientation is approximately given by the following cool expression (this is exact when $n$ is even, and overshoots by $1/2^{n+2}$ when $n$ is odd): $$\frac{(n+2)^2}{2^{n+2}}$$ The circular version of this problem is not very interesting, with the number of good orientations $c_0=1$, and for $n>0$, $c_n=2$ if $n$ is odd (all chickens face the same direction), and $c_n=4$ if $n$ is even (chickens with the same parity, i.e. either even numbered or odd numbered, face the same direction). However, the expected number of intimidated chickens does not change dramatically, $n/4$ in the circular case compared to $(n-2)/4$ in the linear case. It can be shown that $c_n=g_n-4g_{n-3}+3g_{n-4}$, so $a_n-4a_{n-3}+3a_{n-4}$ is either $2$ or $4$, which can be verified in the above sequence.

Here is another related problem: Given the set of integers $\{1,2,\ldots,n\}$, select $k$ integers in such a way that no two of them have a difference of exactly $2$. Let $A_{n,k}$ be the number of ways we can do that. It is not hard to establish the following recurrence (based on how we select in the subset $\{1,2\}$): $$A_{n,k}=A_{n-1,k}+A_{n-3,k-1}+A_{n-4,k-2}$$ which can be used to compute $A_{n,k}$ for several values of $n$ and $k$. We can then observe that $A_{2n,n}$ is also equal to $a_{n+2}$. Therefore, $$A_{2n,n}=g_n=\sum_{k\geq 0}A_{n-2,k}2^{n-2k}(-1)^k=2^n\sum_{k\geq 0}A_{n-2,k}\Big(-\frac{1}{4}\Big)^k=a_{n+2}$$ where $\lfloor n/2\rfloor$ can be used as a practical upprer bound for $k$ ($n-2k\geq 0$). The second equality is given by the principle of inclusion and exclusion, where we subtract from $2^n$ (when $k=0$) the number of orientations with at least one intimidated chicken: $2^{n-2k}$ counts the number of orientations that intimidate a given fixed set of $k$ chickens, and $A_{n-2,k}$ counts the number of such sets (two intimidated chickens cannot be at a distance of exactly $2$, and the first and last chickens cannot be intimidated).

In fact, we can solve for $A_{n,k}$ and show that $A_{2n,n}=a_{n+2}$. A valid selection of $k$ integers in $\{1,2,\ldots,n\}$ defines runs of consecutive integers. Observe that every run must contain either $1$ or $2$ integers. If the total number of runs is $r$, then $k-r$ of these must have size $2$, and so there are $C_{k-r}^r$ ways of choosing those runs. The remaining $n-k$ integers (which were not selelected) form $r+1$ gaps between runs, the lengths of which can be described by the following equation: $$l_0+l_1+\ldots+l_{r-1}+l_r=n-k$$ where $l_1,\ldots,l_{r-1}\geq 2$, and $l_0,l_r\geq 0$. The number of non-negative integer solutions for this equation is $C_r^{n-k-r+2}$, given by a standard counting method as long as $0\leq r\leq n-k+2$. Therefore, $$A_{n,k}=\sum_{0\leq r\leq n-k+2} C_{k-r}^rC_r^{n-k-r+2}$$ Observe that the practical lower and upper bounds for $r$ in $A_{n,k}$ are $\lceil k/2\rceil$ ($r\geq k-r$) and $\min\{k,\lfloor (n-k)/2\rfloor +1\}$ ($k-r\geq 0$ and $n-k-r+2\geq r$), respectively. Therefore, to compute $A_{2n,n}=\sum_r C_{n-r}^rC_r^{n-r+2}$, we may use: $$A_{2n,n}=\sum_{r=\lceil n/2\rceil}^{\lfloor n/2\rfloor+1} C_{n-r}^rC_r^{n-r+2}$$ When $n$ is odd, the sum contains one term. When $n$ is even, it contains two terms. By expanding the binomial coefficients in every term, $A_{2n,n}$ simplifies to $(n+1)(n+3)/4$ when $n$ is odd, and $(n+2)^2/4$ when $n$ is even, which is $\lfloor (n+2)^2/4\rfloor=a_{n+2}$ for every $n$.

Given the solution for $A_{n,k}$, we can now write: $$g_n=2^n\sum_{k=0}^{\lfloor n/2\rfloor} \sum_{r=\lceil k/2\rceil}^{\min\{k,\lfloor (n-k)/2\rfloor\}} C_{k-r}^rC_r^{n-k-r}\Big(-\frac{1}{4}\Big)^k$$ The bounds can be relaxed; for instance, when $r$ is bounded, we can make $k\geq 0$, and similarly, when $k$ is bounded, we can make $r\geq 0$ (all values of $r$ are safe since anomalies arise when $n-k-r\leq -1$ and $0\leq k-r\leq r$, which would make $k>n/2$). The following figure illustrates three areas for $(k,r)$: green is needed, red must be avoided, and white makes no difference (green and red include their boundaries).



An interesting observation is that $\sum_{k\geq 0}A_{n,k}$ satisfies a recurrence similar to that of $A_{n,k}$. In particular, it is not hard to see that $\sum_{k\geq 0}A_{n,k}=\sum_{k\geq 0}A_{n-1,k}+\sum_{k\geq 0}A_{n-3,k}+\sum_{k\geq 0}A_{n-4,k}$. Another known sequence with this recurrence is $\alpha_n$ for $n\geq 0$: $$1, 1, 1, 2, 4, 6, 9, 15, 25, 40, 64, 104, \ldots$$ which makes $\sum_{k\geq 0}A_{n,k}=\alpha_{n+2}$ related to Fibonacci numbers: $\alpha_n-\alpha_{n-4}=\alpha_{n-1}+\alpha_{n-3}=F_n$, and when $n$ is even, $\alpha_{n}=F^2_{(n+2)/2}$, and when $n$ is odd, $\alpha_{n}=F_{(n+1)/2}\cdot F_{(n+3)/2}$. Therefore, $$\sum_{k\geq 0}A_{n-2,k}=\alpha_{n}=\sum_{k=0}^{\lfloor n/2\rfloor }\sum_{r=\lceil k/2\rceil}^ {\min\{k,\lfloor(n-k)/2\rfloor\}}C_{k-r}^rC_r^{n-k+r}=\left\{ \begin{array}{ll} F^2_{(n+2)/2} & n \textrm{ is even}\\ F_{(n+1)/2}\cdot F_{(n+3)/2} & n \textrm{ is odd} \end{array} \right. $$ where the bounds in the summations can also be relaxed as described above. The number $\alpha_{n}$ counts the subsets of $\{1,2,\ldots,n\}$ in which all the chickens can be intimidated simultaneously. This is beautiful when compared to $g_n$: $$g_n=2^n\sum_{k=0}^{\lfloor n/2\rfloor} \sum_{r=\lceil k/2\rceil}^{\min\{k,\lfloor (n-k)/2\rfloor\}} C_{k-r}^rC_r^{n-k-r}\Big(-\frac{1}{4}\Big)^k= \left\{\begin{array}{ll} [(n+2)/2]^2 & n \textrm{ is even}\\ (n+1)/2\cdot (n+3)/2 & n \textrm{ is odd} \end{array} \right. $$ If we now define $B_{n,k}$ as the number of orientations in which exactly $k$ out of $n$ chickens are intimidated, then $B_{n,0}=g_n$, $\sum_{k\geq 0}B_{n,k}=2^n$, and $\sum_{k>\lfloor n/2\rfloor}B_{n,k}=0$. But what is $B_{n,k}$ for any given $n$ and $k$? $B_{n,1}$ ($0,0,0,2,6,16,32,60,100,160,240,\ldots$) is related to the number of non-palindromic reversible strings with $4$ black and $n-5$ white beads. $B_{n,2}$ ($0,0,0,0,1,4,16,44,110,236,472,\ldots$) is related to the number of bracelets with $1$ blue, $6$ identical red, and $n$ identical black beads. $B_{n,2}$ and $B_{n,4}$ ($0,0,0,0,0,0,0,0,1,6,36,146,511,1512,\ldots$) are also related to parrafin numbers (page 1925) $l(9,n)$ and $l(13,n)$, respectively.