A very nice explanation is given at Cut the knot.
You want the number for $p, q$ with $\gcd(p, q) = 1$ (otherwise it makes no sense). Consider the $q$ sequences:
$\begin{align}
&0 + 0, 0 + q, 0 + 2 q, \dotsc \\
&p + 0, p + q, p + 2 q, \dotsc \\
&\vdots \\
&(q - 1) p + 0, (q - 1) p + q, \dotsc
\end{align}$
They have no elements in common. Now take the series:
$\begin{align}
\sum_{k \ge 0} z^{r p + k q}
&= z^{r p} \sum_{k \ge 0} z^{k q} \\
&= \frac{z^{r p}}{1 - z^q}
\end{align}$
Add them all up:
$\begin{align}
\sum_{0 \le r \le q - 1} \frac{z^{r p}}{1 - z^q}
&= \frac{1}{1 - z^q} \sum_{0 \le r \le q - 1} z^{r p} \\
&= \frac{1}{1 - z^q} \frac{1 - z^{q p}}{1 - z^p} \\
&= \frac{1 - z^{p q}}{(1 - z^p) (1 - z^q)}
\end{align}$
The coefficients of this are all 0 (the number isn't representable) or 1 (the number is representable). We get the series with 1 for non-representable ones by:
$\begin{align}
\frac{1}{1 - z} - \frac{1 - z^{p q}}{(1 - z^p) (1 - z^q)}
&= \frac{(1 - z^p) (1 - z^q) - (1 - z) (1 - z^{p q})}
{(1 - z) (1 - z^p) (1 - z^q)}
\end{align}$
This is a polynomial (it is easy to see that large enough numbers are all representable). Its degree is the last non-representable number, and that is just the degree of the numerator less the degree of the denominator:
$\begin{align}
(1 + p q) - (p + q + 1)
= p q - p - q
\end{align}$
As the coefficients are all 1, we can also get the number of non-representable ones as:
$\begin{align}
\lim_{z \to 1} \frac{(1 - z^p) (1 - z^q) - (1 - z) (1 - z^{p q})}
{(1 - z) (1 - z^p) (1 - z^q)}
\end{align}$
Applying l'Hôpital thrice gives:
$\begin{align}
\frac{−3 p q (p q − p − q + 1)}{- 6 p q}
= \frac{p q - p - q + 1}{2}
\end{align}$