You need to calculate the number of ways to go from 5 to 7 in 5 0 0 0 0 0 7.
There is 1 way to get to the 5 at the beginning (you simply start there).
How many ways are there to get to the possibilities in the next column? The possibilities for the second column are 4, 5, and 6. There is 1 way to get to each of them: 5->4, 5->5, and 5->6, respectively.
The next 0 could be anything from 3 to 7. How many ways are there to get to the 3 at that point? Only 1: 5->4->3. But to get to 4 there are 2 ways: 5->4->4 or 5->5->4. To get to the 5 there are 3 ways: 5->4->5, 5->5->5, and 5->6->5. And so on.
That's what you want to calculate, but it so happens that to calculate the numbers for a particular column, all you need are the numbers from the previous column since the number of ways to get to a certain point is simply the sums of the number of ways to get to the points in the previous column that could have led to the current point.
So you only need two arrays, one for the the previous column and one for the current column.
Here's all the numbers filled in for 5 0 0 0 0 0 7:
5 0 0 0 0 0 7
a b c d e f g
3 . . 1 . . . .
4 . 1 2 6 . . .
5 1 1 3 7 19 . .
6 . 1 2 6 16 45 .
7 . . 1 3 10 30 90
8 . . . 1 4 15 .
9 . . . . 1 . . |
Two more examples:
3 0 0 0 0 0 0 0 0 0 0 3
a b c d e f g h i j k l
1 . . 1 3 9 25 69 189 518 1422 . .
2 . 1 2 6 16 44 120 329 904 2493 6898 .
3 1 1 3 7 19 51 140 386 1071 2983 8338 23376
4 . 1 2 6 16 45 126 356 1008 2862 8140 .
5 . . 1 3 10 30 90 266 783 2295 . .
6 . . . 1 4 15 50 161 504 . . .
7 . . . . 1 5 21 77 . . . .
8 . . . . . 1 6 . . . . . |
0 0 0 0 0 6
a b c d e f
1 1 . . . . .
2 1 3 . . . .
3 1 3 9 . . .
4 1 3 9 27 . .
5 1 3 9 27 81 .
6 1 3 9 27 81 243
7 1 3 9 27 81 .
8 1 3 9 27 . .
9 1 3 9 . . .
10 1 3 . . . .
11 1 . . . . .
|
Note that this last case (that the sequence starts with 0's) is simple since the answer is 3 to the power of the number of zeroes. It makes one wonder if there is a mathematical formula for the other cases. I don't know. I could only think of this dynamic programming solution.