Number of ways Nim-sum is X
Solution 1:
Here is an outline of what I would do:
-
If you had numbers 1 through 7, it would be much more convenient, right? You would do as you mentioned, and would only have to consider the undesirable cases where the first three piles add up to 0. This is also easier and there are not too many cases this way. Check it!
-
Now 8 and 9 are also allowed. If there is at least one of them, then there are at least two of them so that the leftmost digit adds up to zero. Distinguish cases (8,8), (8,9), (9,8), (9,9) as well as where all four are either 8 or 9. The latter cases are easy anyways, and the first cases should not be difficult either (2 piles remain and they should be in 1,...,7 and have some fixed nimsum).
-
Obviously add up the cases 1 and 2.
Can't guarantee this is the easiest way, but I think dealing with 8 and 9 is what makes this problem nontrivial!