Here's an example
For $n = 4$ and $k = 1$, there are 8 possible arrangements:
$a\ a\ a\ a$
$a\ a\ a\ b$
$a\ a\ b\ a$
$a\ b\ a\ a$
$b\ a\ a\ a$
$b\ a\ b\ a$
$b\ a\ a\ b$
$b\ a\ b\ a$
with $b$ being the element that can't have more than $k$ consecutive appearances.
I'm not really well-versed in combinatorics, so $2^n$ being the total number of arrangements is the only piece of info I came up with.
Anyway, this is actually a programming challenge from one of those competitive programming sites, but I wanna get a grasp of the math behind the idea before thinking about implementation methods, so I figured I'd ask things here. Also, the input range for both $n$ and $k$ is $[1, 10^6]$ so a dynamic programming approach is the way to go.
The dynamic programming states are :
$(i,j)$= number of valid strings of length $i$ ending with character $j$. Assume $j$ to be $0$ for $a$ and $1$ for $b$.
Now, consider a compressed version of the string , it will be like $\{ (a,x),(b,x'),(a,z),(b,z') \}$ and so on, or $\{ (b,x),(a,x'),(b,z),(a,z') \}$ and so on.
For example, the set of pairs : $ \{ (a,1),(b,2),(a,1) \} $ would indicate the string :
$ abba $
Now let's call $q_{i}$ the second member of the $i^{th}$ pair in the set. You want the sum of all $q_{i}$ to be $N$, where $N$ is the length of the string , each $q_{i}$ is positive, and each $q_{i}$ belonging to a $'b'$ is $ \le K $.
So, the dyanmic programming transisions would be :
$ d[i][0]= \sum_{j=0}^{i-1} d[j][1] $,
$ d[i][1]= \sum_{j=i-k}^{i-1} d[j][0] $
with the base case being $d[0][0]=d[0][1]=1$
The answer will be sum of $ d[N][0]$ and $d[N][1]$. Try and think of how you can use prefix sums to optimize these transitions to work in $O(N)$
I personally feel this is a standard problem, and having many queries for different $K$ with fixed $N$ would make the problem much more interesting and harder.