TITLE: How many ways to add to 32?
QUESTION [9 upvotes]: I have been presented with a rather complex combination problem.
Using only the numbers 2, 4, 6 and 8, how many possible ways can you add up to 32 if the number 4 may only be used no more than once in any solution?
Also including solutions that do not use the number 4, I can find 29 unique solutions when the order doesn't matter.
Solution 1:     8   8   8   8
Solution 2:     8   8   8   4   2   2
Solution 3:     8   8   8   2   2   2   2
Solution 4:     8   8   6   6   4
Solution 5:     8   8   6   6   2   2
Solution 6:     8   8   6   4   2   2   2
Solution 7:     8   8   6   2   2   2   2   2
Solution 8:     8   8   4   2   2   2   2   2   2
Solution 9:     8   8   2   2   2   2   2   2   2   2
Solution 10:    8   6   6   6   6
Solution 11:    8   6   6   6   4   2
Solution 12:    8   6   6   6   2   2   2
Solution 13:    8   6   6   4   2   2   2   2
Solution 14:    8   6   6   2   2   2   2   2   2
Solution 15:    8   6   4   2   2   2   2   2   2   2
Solution 16:    8   6   2   2   2   2   2   2   2   2   2
Solution 17:    8   4   2   2   2   2   2   2   2   2   2   2
Solution 18:    8   2   2   2   2   2   2   2   2   2   2   2   2
Solution 19:    6   6   6   6   6   2
Solution 20:    6   6   6   6   4   2   2
Solution 21:    6   6   6   6   2   2   2   2
Solution 22:    6   6   6   4   2   2   2   2   2
Solution 23:    6   6   6   2   2   2   2   2   2   2
Solution 24:    6   6   4   2   2   2   2   2   2   2   2
Solution 25:    6   6   2   2   2   2   2   2   2   2   2   2
Solution 26:    6   4   2   2   2   2   2   2   2   2   2   2   2
Solution 27:    6   2   2   2   2   2   2   2   2   2   2   2   2   2
Solution 28:    4   2   2   2   2   2   2   2   2   2   2   2   2   2   2
Solution 29:    2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2

That in itself was rather challenging, but there are additional restrictions:
A. Order DOES matter. That is to say, something like [8,8,6,6,4] is indeed considered a different solution than [8,8,6,4,6]. 
B. Any solution that does use the number 4, must BEGIN with the number 4.
If I treat any elements that have the same value as other elements within a given solution as unique, I can take n! where n is the number of elements within each solution. Naturally, for those solutions that contain the number 4, I use (n-1)! instead. That provides me with a whopping 21,104,423,119,848 solutions.
Taking repeating elements as non unique, using n!/(n1! x n2! x n3! x ... x nk!) for all solutions that don't contain the number 4, and the same but using (n-1)! in the numerator for solutions that do contain the number 4, I find a greatly reduced 1,577 solutions.
Now here's the part I'm struggling with.
C. There are two unique ways to include the value 2 in a solution.
D. There are four unique ways to include the value 8 in a solution.
E. There are five unique ways to include the value 6 in a solution.
That is to say there are different "versions" of each value.
F. No versions of 6 or 8 may be repeated within any single solution
For clarity, assume we have [8,8]. There are 4 "versions" of 8 that may be used, but not repeated within a solution. [8a,8a] for instance is not valid. This could be [8a,8b] or [8a,8c] or [8a,8d] or [8b,8a] or [8b,8c] etc.
Assume you had [2,2]. Because you CAN repeat "versions" of 2, you could use [2a,2b] or [2b,2a] or [2a,2a] or [2b,2b].
There you have it.
Following all of the restrictions, how many different ways can you add up to 32?

REPLY [3 votes]: The count is $7424856$.
The natural approach, since order matters, is to use an exponential generating function (we'll use a variable $t$).  Since each symbol has a value, and we want the total value to be $32$, we use a second variable $x$ to keep track of value:
if a symbol has value $v$ and can be used at most $r$ times, we introduce a factor
$$\left(1+x^v t + x^{2v}\frac{t^2}{2!} + \cdots + x^{rv}\frac{t^r}{r!}\right)$$
corresponding to that symbol.  (If $r=\infty$ this factor is $\exp(x^v t)$.)
Let $f(x,t)$ be the product of these factors over all symbols.  (In your problem there will be multiple factors for a given value $v$, since we have multiple symbols with the same value.)  Given a sum $s$, and a length $l$, the number of strings of length $l$ and total value $s$ is the coefficient of $x^s t^l/l!$ in $f(x,t)$.
For your example, we'll ignore the $4$ for now, as you suggest.  We have
$$f(x,t)=e^{2 t x^2} \left(1+t x^6\right)^5 \left(1+t x^8\right)^4;$$
we get our answer by computing the coefficient of $x^{32}$ (which is a polynomial in $t$), and the coefficient of $x^{28}$ (to account for strings with a $4$ in front), adding these two together, and replacing $t^k$ by $k!$.
We find $[x^{28}]f(x,t)$ and $[x^{32}]f(x,t)$ are
$$=\frac{8 t^{14}}{42567525}+\frac{8 t^{12}}{31185}+\frac{16 t^{11}}{14175}+\frac{4
   t^{10}}{63}+\frac{32 t^9}{63}+\frac{16 t^8}{5}+\frac{80 t^7}{3}+50 t^6+88
   t^5+60 t^4$$
and
$$\frac{2 t^{16}}{638512875}+\frac{8 t^{14}}{1216215}+\frac{16
   t^{13}}{467775}+\frac{8 t^{12}}{2835}+\frac{16 t^{11}}{567}+\frac{92
   t^{10}}{315}+\frac{32 t^9}{9}+\frac{34 t^8}{3}+56 t^7+122 t^6+60 t^5+t^4$$
respectively.  Adding these together, and substituting $k!$ for $t^k$ gives the answer $7424856$.
Remark. Instead of ignoring the $4$, we can incorporate it into $f(x,t)$ with the factor $(1+x^4)$; the power of $t$ no longer measures the length of the string, but just the length of the permutable part of the string (where order matters).  The result is the same.
Example. It's instructive to verify the calculation by looking at a smaller case, replacing $32$ by $8$ (and let's say there are no $4$'s.)  We have
$$[x^8]f(x,t)=\frac{2 t^4}{3}+10 t^2+4 t,$$
so the argument above implies there should be $4$ strings of length $1$, $10(2!)=20$ of length $2$, and $2/3(4!)=16$ of length $4$.  These are correct: the $4$ strings of length $1$ are the $4$ "versions" of the $8$ symbol; the $20$ strings of length $2$ are permutations of a $6$ and a $2$, in various versions, and the $16$ strings of length $4$ look like $2222$, where there are $2$ possible versions of each $2$.