# Counting points in a box modulo a number

Consider the positive integers $\leq x$, then we know that there are $x/p + O(1)$ integers $\leq x$ that are $a \pmod{p}$ ($p$ prime).

Consider a similar problem, except this time, we are counting $(x, y) \in \mathbb{Z} \times \mathbb{Z}$ inside the box $|x| \leq B$ and $|y| \leq B$. I want to count the number of integers in this box with $x \equiv a \pmod{p}$ and $y \equiv b \pmod{p}$. Is the number of such pairs $4B^{2}/p^{2} + \text{error term}$. Is the error term $O(1)$ or $O(B)$? Can we have an error term of $O(1)$?

-

In fact, you don't need $p$ prime for this.
If we think about $p=3, a=1, b=1$ and let $B=3k$, the correct answer is $(2k-1)^2$ while our formula gives $4k^2$, with the error $4k \in O(B)$. Similarly if $B=3k+1$, the right answer is $(2k+1)^2=4k^2+4k+1$ while the formula gives $\frac {4(3k+1)^2}9 =\frac {36k^2+24k+1}9= 4k^2+ \frac {24}9k+\frac 19$ with error $\frac 43k \in O(B)$