I would suggest using a generating polynomial to do this. The idea is to represent the contribution of one die towards the total by the power series (1 + x + ... + x^n) where n is the number of faces on the die, then multiplying together one series per die and calculating the coefficient of the power of the sum you're looking for - e.g. the coefficient of x^41 in your example. You can reduce the degree of combinatorial explosion by discarding terms where the power of X exceeds the target, or cannot reach it based on the remaining terms in the product.
user3490
- 437
- 1
- 3
- 8