TITLE: How many sequences of numbers $\{a_1...a_5\}$ where $a_i \in \{1...25\}$ satisfy $a_{i+1} \leq a_i + 2$
QUESTION [5 upvotes]: Here's how it looks:

1 1 1 1 1 
  1 1 1 1 2 
  1 1 1 1 3 
  1 1 1 2 1 
  1 1 1 2 2 
  1 1 1 2 3 
  1 1 1 2 4 
  1 1 1 3 1 
  ......... 
  25 25 25 25 24 
  25 25 25 25 25 

Counting sequences using a simple script gives an answer of 386958
I already know that if I had just $a_{i+1} \leq a_i$, it would be ${25+5-1 \choose 5}=118755$, but I don't know what to do with that extra "+2" in $a_{i+1} \leq a_i + 2$
What is the mathematical solution here? Do I have to use a generating function for this?

REPLY [2 votes]: This answer is probably much more work than just counting by using a script, but here goes. . . 
We'll use inclusion/exclusion.  The total number of $5$-tuples is $25^5$, and we have to exclude those which lie in the union of the four sets
$$S_k=\{\,{\bf a}\mid a_{k+1}>a_k+2\,\}\ .$$

To count $S_1$ we need to arrange $25$ dots in the following way, where $\bullet$ represents a compulsory dot and $\circ$ represents an optional dot:
$$\qquad\qquad\cdots\circ a_1\bullet\bullet\circ\cdots a_2\circ\cdots
  \qquad\qquad(1)$$
This is just the standard problem of counting the solutions to $x_1+x_2+x_3=25$ with $x_2\ge2$, and the number of possibilities is $C(23,2)$.  We then have to choose arbitrary values for $a_3,a_4,a_5$, so $|S_1|=C(23,2)25^3$.  The same holds for $S_2,S_3,S_4$.
Arrangements in $S_1\cap S_2$ look like this:
$$\qquad\qquad\cdots\circ a_1\bullet\bullet\circ\cdots a_2\bullet\bullet\circ\cdots a_3
  \circ\cdots\qquad\qquad(2)$$
and we get $|S_1\cap S_2|=C(21,3)25^2$.  There are two more terms like this. . . 
. . . but $S_1\cap S_3$ is different.  It will look like two independent copies of $(1)$ together with one "free choice", so $|S_1\cap S_3|=C(23,2)^225$.

Continuing in a similar way, the total number is
$$\eqalign{25^5-4C(23,2)25^3
  &{}+(3C(21,3)25^2+3C(23,2)^225)\cr
  &{}-(2C(19,4)25+2C(21,3)C(23,2))+C(17,5)\ ,\cr}$$
and guess what?? . . . if you evaluate it you get $386958$.