TITLE: Generating function for the number of ways of writing an integer as a sum of distinct integers from a finite set
QUESTION [7 upvotes]: Let $A$ be a finite set of integers.  The generating function for the number of ways of writing a given integer $n$ as the sum of $k$ elements from $A$ not necessarily distinct is given by:
$$\left(\sum_{a \in A}{x^a}\right)^k=\sum_n{r(n,k)x^n}$$
Is there a generating function for the number of ways of writing an integer $n$ as a sum of $k$ distinct elements of $A$?

REPLY [9 votes]: Using the Polya Enumeration Theorem (PET) the closed form is given by
$$[z^n] Z(P_k)\left(\sum_{a\in A} z^a \right)$$
where  $Z(P_k) = Z(A_k)-Z(S_k)$  is the  difference between  the cycle
index of  the alternating group and  the cycle index  of the symmetric
group. This cycle index is known in species theory as the set operator
$\mathfrak{P}_{=k}$ (unlabeled) and the species equation here is
$$\mathfrak{P}_{=k}\left(\sum_{a\in A} \mathcal{Z}^a\right).$$

Recall  the recurrence by Lovasz  for the cycle  index $Z(P_k)$ of
the set operator $\mathfrak{P}_{=k}$ on $k$ slots, which is
$$Z(P_k) = \frac{1}{k} \sum_{l=1}^k (-1)^{l-1} a_l Z(P_{k-l})
\quad\text{where}\quad
Z(P_0) = 1.$$
This  recurrence  lets us  calculate  the  cycle  index $Z(P_n)$  very
easily.  For example when $n=3$ the cycle index is
$$Z(P_3) = 
1/6\,{a_{{1}}}^{3}-1/2\,a_{{2}}a_{{1}}+1/3\,a_{{3}}.$$
These cycle indices  are also given by the  exponential formula, which
says that
$$Z(P_k) = [w^k]
\exp\left(a_1 w - a_2 \frac{w^2}{2} + a_3 \frac{w^3}{3}
- a_4 \frac{w^4}{4} + \cdots\right).$$
For example  suppose $A$ consists of  powers of two.  By inspection we
should get from
$$\sum_{k\ge 0} [w^k]
\exp\left(\sum_{l\ge 1} (-1)^{l-1} a_l \frac{w^l}{l}\right)$$
evaluated at
$$a_l = \sum_{q\ge 0} z^{l2^q}
\quad\text{the value}\quad
\frac{1}{1-z}.$$
And indeed we get for the sum term
$$\sum_{l\ge 1} (-1)^{l-1} \frac{w^l}{l} \sum_{q\ge 0} z^{l2^q}
= \sum_{q\ge 0} \sum_{l\ge 1} (-1)^{l-1} z^{l2^q} \frac{w^l}{l}
= \sum_{q\ge 0} \log (1+wz^{2^q}).$$
We obtain
$$\sum_{k\ge 0} [w^k] \prod_{q\ge 0} (1+wz^{2^q})
= \left. \prod_{q\ge 0} (1+wz^{2^q}) \right|_{w=1}
\\ = \prod_{q\ge 0} (1+z^{2^q})
= \frac{1}{1-z}.$$
The  reader  is  invited  to  verify  that  the  conversion  from  the
exponential formula to the  product representation of the set operator
does in fact always carry through  independent of the choice of $A$ so
that we get
$$[w^k] \prod_{a\in A} (1+wz^a).$$
Here is some  Maple code to explore these  cycle indices.  We would
always prefer the recurrence in a practical setting.

pet_cycleind_set :=
proc(k)
option remember;

    if k=0 then return 1; fi;

    expand(1/k*add((-1)^(l-1)*a[l]*
                   pet_cycleind_set(k-l), l=1..k));
end;

pet_cycleind_set2 :=
proc(k)
option remember;
local gf;

    gf := exp(add((-1)^(l+1)*a[l]*w^l/l, l=1..k));

    coeftayl(gf, w=0, k);
end;

Remark. We can derive the recurrence from the exponential formula.
Introducing
$$G(w) = 
\exp\left(\sum_{l\ge 1} (-1)^{l-1} a_l \frac{w^l}{l}\right)$$
we differentiate to obtain
$$G'(w) = G(w) 
\left(\sum_{l\ge 1} (-1)^{l-1} a_l w^{l-1}\right)
= G(w) \left( \sum_{l\ge 0} (-1)^{l} a_{l+1} w^{l}\right).$$
Extracting coefficients we get
$$[w^k] G'(w) = (k+1) [w^{k+1}] G(w)
= \sum_{q=0}^k (-1)^q a_{q+1} [w^{k-q}] G(w)
\\ = \sum_{q=1}^{k+1} (-1)^{q-1} a_q [w^{k+1-q}] G(w).$$
This is our recurrence precisely.
Remark, II. We  may ask why the exponential formula  is the OGF of
the  set  operator   $\mathfrak{P}_{=k}$  and  the  multiset  operator
$\mathfrak{M}_{=k}.$ This  is obtained  from the labeled  species of
permutations being factored into  disjoint cycles. Marking cycle sizes
with the variable $\mathcal{A}_q$ we obtain the species
$$\mathfrak{P}(\mathcal{A}_1 \mathfrak{C}_{=1}(\mathcal{W})
+ \mathcal{A}_2 \mathfrak{C}_{=2}(\mathcal{W})
+ \mathcal{A}_3 \mathfrak{C}_{=3}(\mathcal{W})
+ \mathcal{A}_4 \mathfrak{C}_{=4}(\mathcal{W})
+ \cdots).$$
The exponential formula then follows.  (This is an EGF which means the
coefficients include  an inverse factorial, which produces  an OGF for
the cycle indices as these are averaged over all $k!$ permutations.)