Simple Combinatorics in finite rings Let $g = [g_{1} g_{2} \dots g_{r}] \in \Bbb Z_{q}^{*r}$ be a given vector with each $g_{i} \in \Bbb Z_{q}^{*}$ where $\Bbb Z_{q}^{*}$ is $\Bbb Z_{q} \backslash \{0\}$ and $q > 6$ is odd. How many vectors $a = [a_{1} a_{2} \dots a_{r}] \in \Bbb Z_{q}^{r}$ with each $a_{i} \in \Bbb Z_{q}^{}$  are such that:
$(1)$ $a^{T}g = 0$?
$(2)$ $a^{T}g = 1$?
 A: If there are $t$ zeroes in $g$ then each one of these gives rise to $q$ possibilities for that corresponding entry in $a$. If, for instance, $q$ is even and $g_i$ is $\frac{q}{2}$ then there can be $\frac{q}{2}$ possibilities (the even ones) to make $a_i g_i = 0$, too, and counting these gets quite tricky...
If $q$ is prime (or at least one of the $g_i$ is relatively prime to $q$) then the remaining $r-t$ elements in $a$ need to satisfy one particular equation, and so we just need (for (1)) $a_i = (-g_i)^{-1} \left( \sum_{j\not = i} a_j g_j \right )$, giving us $q^{r-1}$ choices for $a$. For (2)/(3) it will just be that sum $\pm 1$.
Note that in the case when $r=t$ then all possible $q^r$ vectors for $a$ are either solutions or non-solutions depending on whether the sum is or is not zero...
When all $g_i$ are zero-divisors in ${\Bbb Z}_q$ you will need to do some careful considerations of greatest common divisors.
A: First of all, by the Chinese Remainder Theorem, it suffices to consider prime powers $q$. For composite $q$, the number of solutions will be the product of the solution numbers for the respective prime power parts of $q$.
So assume $q = p^m$, $p$ prime.
For elements $a\in\mathbb{Z}/q\mathbb{Z}$, we define the $p$-adic valuation $v_p(a)$ as the maximum exponent $e$ such that $p^e \mid a$. In the special case $a=0$, we set $v_p(a) = m$.
Now set $N = \min\{v_p(g_i) \mid i\in\{1,\ldots,r\}\}$.
For Question 1, the answer is $q^{r-1}\cdot p^N$.
This can be proven by induction over $m$, for example.
Apparently, Question 2 doesn't have a solution if $N \neq 0$.
For $N = 0$, the number of solutions is $q^{r-1}$.
(Let $i$ be a index with $v_p(g_i) = 0$. Then $g_i$ is a unit. Now for each choice of the $r-1$ entries $a_j$ with $j\neq i$, there is a unique $a_i$ s.t. $a^T g = 1$.)
