TITLE: Combinatorics using averages
QUESTION [6 upvotes]: How many solutions exist for $$x+2y+4z=100$$ in non-negative integers?

The author, Martin Erickson, in his book, Aha! Solutions, published by MAA, gives the following brief solution :

There are $26$ choices for $z$, namely, all integers from $0$ to $25$. Among these choices, the average value of $4z$ is $50$. So, on average, $x+2y=50$. In this equation, there are $26$ choices for $y$, namely, all integers from $0$ to $25$. The value of $x$ is determined by the value of $y$. Hence, altogether there are $26^2=676$ solutions to the original equation.

Can somebody explain to me why this mindblowing solution works using averages?

REPLY [5 votes]: Consider the cartesian product $S=\{0,1,\ldots, 50\}\times\{0,1,\ldots, 25\}$, which represents choosing $y$ to be at most $50$, and $z$ to be at most $25$. We call elements of $S$ solutions, though they may not actually give solutions to the equation. We say $(y,z)\in P$ is valid if $2y+4z\leq 100$, since we can then choose $x$ to be $100-2y-4z$, and invalid otherwise. I'll show a way to match invalid solutions with valid ones. If $(y,z)$ is an invalid solution, then $(50-y, 25-z)$ is a valid solution. This is because $\color{red}{2y+4z}+\color{blue}{2(50-y)+4(25-z)}=200$, so if one of the two colored terms is greater than $100$, the other is less than $100$. Two invalid solutions are never matched this way, and if two valid solutions are matched this way, it's because $2y+4z=100$. There are $26$ solutions for which this can occur.
Hence, there are $51\cdot 26$ solutions in $S$. Of these, $26$ of these solutions are valid and are matched with another valid solution. All other $51\cdot 26-26$ solutions can be grouped into pairs, consisting of one valid solution, and one invalid one. Hence, the number of valid solutions is $\frac{51\cdot 26-26}{2}+26=26\cdot 26$.