Find ordered pairs (x, y), such that |x-y| is divisible by 3

combinatorial-proofscombinatorics

The question is to find the total number of ordered integer pairs $(x, y)$ such that the absolute value of their difference is divisible by 3, subject to constraints $0 \leq x \leq N$, $\ 0 \leq y \leq M$ and $x+y \neq 0$, where $N$ and $M$ are positive integers.

For example: if $N = 2, M = 3$, then the ordered pairs which satisfy the condition are $(1,1), (2, 2)$, and $(0, 3)$. Therefore the answer would be 3.

Now, I can manually find answers for smaller values of $N$ and $M$. Is there a way to get a simplified expression that generalizes the answer for any large $N, M$?

Best Answer

$x+y=0 \iff x=0;y=0$. It'll be easy to count them all including $(0,0)$ and then subtracting one.

For any integer $W$ lets define $Q_w$ and $R_W$ as the unique two integers so that $W = 3Q_2 + R_W$ and $0 \le R_W < 3$. (In other words $Q_W$ is the whole number quotient of dividing $W$ by $3$ and $R_W$ is the remainder.)

Now for any $k \le M$ we will have $k = 3Q_k + R_k$ and so for any $v = 3j + R_k$ we will have $|k -v| = |(3Q_k + R_k) -(3j +R_k)| = |3Q_k -3j| = 3\times |Q_k -j|$ (and it's easy see that if $R_v\ne R_k$ then $|k-v| = |(3Q_k +R_k)-(3Q_v +R_v)| =|3(Q_k-Q_v) +(R_k-R_v)|=3\times |Q_k-Q_v| \pm (R_k-R_v)$ is not divisible by $3$)

So for each $k \le M$ there was $R_k, 3+R_k, 6 + R_k,......, 3H+R_k$ where $3H+R_k$ is the largest number of that form $\le N$. Now that number is either $3Q_N + R_k$ if $3Q_N + R_k \le N = 3Q_N + R_n$ (that is if $R_k \le R_n$), of that number is $3(Q_N-1) + R_k$ if $3Q_N + R_k > N$ (that is if $R_k > R_n$)

So there are 2 cases:

If $R_M \le R_N$ then for every $w \le M$ (there are $M+1$ such numbers) then for ever $3k + R_w$ with $k \le Q_N$ (there are $Q_N + 1$ such numbers) we will have their differences divisible by $3$.

Thus there are $(M + 1)(Q_N + 1) = (M+1)(\lfloor \frac N 3\rfloor + 1) = M\lfloor \frac N 3\rfloor + M + \lfloor \frac N 3\rfloor + 1$. And if we remove $(0,0)$ there are $M\lfloor \frac N 3\rfloor + M + \lfloor \frac N 3\rfloor$

And case 2 is if $R_M > R_N$ but that means $R_N < R_M$ and we do the same thing but in terms of every $w \le R_N$.

We get there are $(N+1)(Q_M +1) -1 = N\lfloor \frac M 3\rfloor + N + \lfloor \frac M 3\rfloor$