Permutations of colored balls with restrictions Consider $N$ balls, each in one of $K$ possible colors. We denote by $n_k$ the number of balls colored in the $k$-th color ($\sum_{k=1}^K n_k =N$). Is there a known formula for the total number of permutations of the $N$ balls subject to the restriction that concatenated copies of shorter sequences should be discounted? For example, if we have $3$ $b$lue-colored balls and $3$ $r$ed-colored balls then one should not count the permutations $brbrbr$ and $rbrbrb$.

Yes you can. Using Möbius inversion, you can show that the number of sequences of length $N$, consisting of $n_k$ copies of color $k$ for each $k\in \{1,\dots,K\}$, is
$$
\sum_{d\mid \gcd(n_1,\dots,n_K)} \mu(d)\frac{(N/d)!}{(n_1/d)!(n_2/d)!\cdots (n_K/d)!},
$$
where $\mu$ is the Möbius $\mu$ function. The sum ranges over positive integers $d$ such that $d$ is a common divisor of $n_1,\dots,n_K$.
This is essentially the principle of inclusion exclusion. You add in all of the sequences with the $d=1$ term, then for $d=2$, you subtract away sequences which are a double of a smaller sequence. Similarly, for $d=3$, you subtract triple repeats of smaller sequences. But for $d=6$, since $\mu(6)=+1$, you add back in the six-tuple repeats, because these were doubly subtracted in the previous two steps. The $\mu()$ function magically makes everything cancel out to count the number of sequences which are not repeats of a smaller one.
