Given integers $n$ and $k$, find the number of quadratic congruences of the form $$ ax^2 + bx + c \equiv 0 \pmod{n} $$ having exactly $k$ solutions in $\mathbb{Z}_n$, where $a, b, c \in \mathbb{Z}_n$.
I could only find a formula for the case $n$ is a prime number but failed to derive one for the composite case.
Here is my attempt for the case $n = p$, where $p$ is a prime number.
The case $p = 2$ is pretty trivial. For $k = 0$, there are 2 congruences, 4 congruences when $k = 1$ and 2 congruences having 2 solutions. The following will consider the case $p$ is an odd prime.
For $a \equiv 0 \pmod{p}$, the congruence becomes $bx + c \equiv 0 \pmod{p}$. If $b \equiv 0 \pmod{p}$ then the congruence will have 0 solutions if $c \not \equiv 0 \pmod{p}$ and $p$ solutions otherwise. If $b \not \equiv 0 \pmod{p}$ then $\gcd(b, p) = 1$, which implies a unique solution $x \equiv -b^{-1}c \pmod{p}$.
This case contributes one congruence for $k = p$ and $p(p - 1)$ congruences for $k = 1$.
Now, for $a \not \equiv 0 \pmod{p}$, the congruence becomes $(2ax + b)^2 \equiv b^2 - 4ac \pmod{p}$. Let $\Delta \equiv b^2 - 4ac \pmod{p}$, then there will be no solution if $\Delta$ is a quadratic non-residue of $p$. Otherwise, if $\Delta \equiv 0 \pmod{p}$ then $2ax + b \equiv 0 \pmod{p}$, which has an unique solution $x \equiv -(2a)^{-1}b \pmod{p}$ since $\gcd(a, p) = 1$. As $b^2 \equiv 4ac \pmod{p}$, we can determine an unique $c$ for every $a$ and $b$ because $c \equiv (4a)^{-1}b^2 \pmod{p}$, thus, this case contributes $p(p - 1)$ congruences for $k = 1$. The final case is $\Delta \not \equiv 0 \pmod{p}$, i.e. non-zero quadratic residue of $p$, which yields 2 solutions $x \equiv (2a)^{-1}\left(b \pm \sqrt{\Delta}\right) \pmod{p}$. Now, $c \equiv (4a)^{-1}\left(b^2 - \Delta\right) \pmod{p}$, there are $\dfrac{p - 1}{2}$ choices for $\Delta$, $p - 1$ choices for $a$ and $p$ choices for $b$, which implies a total of $\dfrac{p(p - 1)^2}{2}$ congruences.
Combining the above cases, we obtain \begin{array}{cl} \text{1 solution}: & 2p(p - 1)\\ \text{2 solutions}: & \dfrac{p(p - 1)^2}{2}\\ \text{p solutions}: & 1\\ \text{0 solution}: & p^3 - 1 - 2p(p - 1) - \dfrac{p(p - 1)^2}{2}\\ \text{Others}: & 0 \end{array}
Then, I attempted to solve for composite $n$ using a similar approach but failed to proceed since $b$ and $a$ are not always coprime to $n$. I also tried CRT but it was wrong.
I also tried brute-forcing the problem to see if there are any notable patterns but failed to detect anything besides those $k$'s that have at least one congruence divides $n$ (aside from $k = 2$).
How can I proceed and obtain similar results for composite $n$?
Thank you in advance.