TITLE: Permutations with restriction
QUESTION [22 upvotes]: We have $n$ types of objects, and the number of objects of type $i$ is $a_i$, $1\leq i\leq n$. 
What is the number of permutation of the $\sum_{i=1}^n a_i$ objects, if no two objects of the same type are next to each other?
A simple example: If we have the objects $\{a,a,a,b,b,c,c\}$, then we allow $abcabac$ but not $aaabbcc$.

REPLY [9 votes]: Gessel gives a formula for this (Section 6) as an application of his theory of generalized rook polynomials (see also http://arxiv.org/abs/1306.6232). Let $L^{(\alpha)}_n(x)$ be the generalized Laguerre polynomial $\sum_{k=0}^n (-1)^k \frac{1}{k!}{n+\alpha \choose n-k} x^k$ and $q_n(x) = (-1)^n L^{(-1)}_n(x)$, and let $\Phi$ be the linear functional on polynomials taking $x^n$ to $n!$. Then
$$
\Phi(q_{n_1}(x) \cdots q_{n_k}(x))
$$
is the number of words with $n_i$ $i$'s where no two letters are adjacent. To treat the $i$'s as distinct, replace $q_n$ by $n! q_n$.
For example,
$$q_2(x) = -x + \frac{1}{2}x^2\\
q_3(x) = x - x^2 + \frac{1}{6}x^3,
$$
so the number of permutations of $aaabbcc$ with no adjacent identical letters is
\begin{align*}\Phi(q_3(x)q_2(x)^2) &= \Phi(x^3 - 2x^4 + \frac{17}{12}x^5 - \frac{5}{12}x^6 + \frac{1}{24}x^7)\\
&= 3! - 2\cdot 4! + \frac{17}{12}\cdot 5! - \frac{5}{12}\cdot 6! + \frac{1}{24}\cdot 7!\\
&= 38.
\end{align*}
Not surprisingly, this comes from inclusion-exclusion. Let
$$B_p = \{(i,j) : n_1 + \cdots + n_{p-1} + 1 \leq i \neq j \leq n_1 + \cdots + n_p\},$$
and $B = B_1 \cup \cdots \cup B_p$. Say a permutation $w_1 \cdots w_n$ of $n = n_1 + \cdots + n_p$ matches $A$ if $j$ immediately follows $i$ in $w$ for each $(i,j) \in A$, and say $A$ is compatible if any permutations match it. We're after the number of permutations of $n$ which don't match any non-empty subset of $B$. By inclusion-exclusion, this is
$$
\sum_{\substack{A \subseteq B\\ A \text{ compatible}}} (-1)^{|A|} (n-|A|)! = \Phi(r_B(x)),
$$
where $\displaystyle r_B(x) = \sum_{\substack{A \subseteq B\\ A \text{ compatible}}} (-1)^{|A|} x^{n-|A|}$.
Define $r_{B_p}(x)$ similarly; then $r_B = r_{B_1} \cdots r_{B_k}$. You get a compatible $i$-subset of $B_p$ by choosing an ordering on $\{n_1 + \cdots + n_{p-1} + 1, \ldots, n_1 + \cdots + n_p\}$, then choosing $n_p-i-1$ commas to break the list into chunks (e.g. if the ordered set is $(2,3,1,5,4)$, you might break it into chunks $23|1|54$, meaning the conditions $3$ follows $2$ and $4$ follows $5$), and finally dividing by $(n_p-i)!$ since the order of the chunks doesn't matter.
Hence,
$$r_{B_p}(x) = \sum_{i=0}^{n_p} (-1)^i n_p! {n_p - 1 \choose n_p - i - 1} \frac{x^{n_p - i}}{(n_p-i)!},$$
which ends up as $n_p! q_{n_p}(x)$ after a bit of reindexing.