Wednesday 7 October 2020

Polynomial solution to coin problem

Consider the following math problem.

 Given an unlimited supply of coins of given denominations, find the total number of distinct ways to get a desired change.

 Consider the easier problem. Consider an vector of sets of numbers (the sets finite or countable, the vector is finite). What are the number of ways to select one item from each set in order to generate a given sum $n$?

Lets define a generating function as follows:

If $v$ is a vector of sets, and $a_i$ is the number of ways to select an element from each set to sum to $i$, then is:
$$G_v(x) = a_ix^i$$.

The sum you get by selecting an element is the value of that specific element. So if the needed sum is in the set, there is one way to create the sum, otherwise the is none. By consequence:

If $v$ contains a single set $s$, then $G_v(x) = \sum_i \delta_ix^i$. where $\delta_i$ is 1 if the set contains the element, and zero if it doesn't.

Consider now two vectors, $a$ and $b$. 

Set $G_a(x) = a_ix^i$ and $G_b(x) = b_ix^i$ Say we want to create a sum of $n$. Call $k$ the sum of elements we get from the original vector $a$. Then the sum of elements we get from original vector $b$ is $n-k$. The number of ways to select a sum $k$ from $a$ is $a_k$, and the number of possibilities to select $n-k$ from $b$ is $b_{n-k}$. So the number of ways to create $n$ with this scheme is $a_kb_{n-k}$. But this counts for every $k$, and all these ways are distinct. So the total numbers of ways is $\sum_k a_kb_{n-k}$. So $G_c(x) = \sum_n\sum_k a_kb_{n-k}x^n$, but this is exactly $G_a(x)G_b(x)$.

By consequance, if $a$ and $b$ are vectors, and $c$ is the concatenation of the two, then $$G_c(x) = G_a(x)G_b(x)$$, or more extreme, each generating function can be constructed by multiplying the generating functions of the single sets in the vector.


Our coin problem is similar to this problem, where is set corresponds to a single coins, and the content of the set are the multiples of the values of the coins. This is easy to see, as selecting any number of coins with value $v$, is equivalent to selecting from the set $\{0,v,2v,3v,...\}$.

Th generating function is obviously equal to $$1+x^v+x^{2v}+x^{3v}+...$$ which is the taylor expansion of $\frac{1}{1-x^v}$.

If all coins are known, the generating function has to be

$$\prod_{v \in C} \frac{1}{1-x^v}$$

with $C$ the set of coins, and $c$ is it's common multiple.

Consider the following field: 

$$\mathbb{Z}[\omega]/(\omega^c-1)$$

and define $\omega_v := \omega^{c/v}$

Then $\omega_v^v = 1$

 So $(x^v-1) = (x-\omega_v)^v$

Note that putting $\omega = e^{2\pi i/c}$ gives an injection from this field to the complex numbers. 

So our generating function is:

$$ \frac{(-1)^n}{\prod_{v \in C}(x-\omega_v)^v}$$.

This can be expanded with partial fractions:

According to the residue method of partial fractions over the complex numbers our generating function is:

$$G(x) = \sum_{v \in V} \sum_{j=0}^{v-1} \frac{a_{vj}}{ (x-\omega_v)^{v-j}} $$

With $$a_{vj} = \frac{1}{j!} \left. \frac{d^j}{dx^j} \frac{(-1)^n (x-\omega_v)^v}{\prod_{v' \in C}(x-\omega_{v'})^{v'}} \right|_{x=\omega_v}$$


Our simplified:

With $$a_{vj} = \frac{1}{j!} \left. \frac{d^j}{dx^j} \frac{(-1)^n}{\prod_{v' \in C \setminus \{v\}}(x-\omega_{v'})^{v'}} \right|_{x=\omega_v}$$

 Even tough our field is not continous, the derivatives over rational functions are always wel defined. 

To select the correct coefficient, we can define our sum $s$, and apply $$\left.\frac{d^s}{dx^s} G(x) \right|_{x=0}$$

So the number of possible ways to create the desired change is:

 $$ \left|\left.\frac{d^s}{dy^s} \left[\sum_{v \in C} \sum_{j=0}^{v-1} \frac{1}{ (y-\omega_v)^{v-j}} \left(\frac{1}{j!} \left. \frac{d^j}{dx^j} \frac{1}{\prod_{v' \in C \setminus \{v\}}(x-\omega_{v'})^{v'}} \right|_{x=\omega_v}\right) \right]\right|_{y=0}\right|$$

over the field  $$\mathbb{Z}[\omega]/(\omega^c-1)$$

With $C$ the set of coins, $n$ the size of $V$,  $s$ the sum the coins should have and $\omega_v := \omega^{c/v}$ is the $v$-th root of unity in the field.