Here is the argument of combinatorics.
Let diff[i, c] be the number of ways to paint i messages in accordance with the rules of the statement of the problem so that the last fence is colored c .
We have:
diff[i, c] = diff[i - 1, c'] + diff[i - 2, c''], c' != c OR c'' != c
For each c which we draw i , the previous one can either end with the symbol c' != c (in this case, we donβt care what the second previous one is), or the second previous one can end with the symbol c'' != c (in this case we all equal to the previous one) or both.
There are k - 1 possibilities for c' != c and k - 1 for c'' != c Thus, we can remove c from the repetition and simply write:
diff[i] = (k - 1) * (diff[i - 1] + diff[i - 2])
What do you have.
source share