Given that we have $x$ number of $A$'s and $y$ number of $B$'s. How can I find the number of ways of arranging them in a line, such that no more than $k$ pieces of the same colour are adjacent?
Constraints: x,y<=1000
I first thought it could be ${x+y \choose x}-{x+y-k \choose x}$. Based on total combinations - combinations having a group with greater than $k$ pieces of the same color together. Then for the latter part, we can combine $k+1$ pieces of the same color (say $A$) as a single piece, so that the total number of pieces reduces to $(x+y-(k+1)+1) $ and the number of pieces of color A reduces to $x-(k+1)+1$. Now each combination of this scheme will be invalid. Total combinations like these will be ${x+y-k \choose x}$ But we can also inject some color $B$ pieces into the combined $k+1$ pieces of color $A$ and still get invalid combination. How to add these? Is there some simpler way of solving the problem?
A recursive solution will also work. The question appeared on a programming contest.