How many ways to select 3 numbers from $1-30$ so that the sum of them is a multiple of 3? How many ways to select 3 numbers from $1-30$ (each number is used only one time) so that the sum of them is a multiple of 3?
I got the answer is $1360$ by programming (check the sum of every combination), and
I also know the formula is $10^{3}+3\cdot\binom{10}{3}$, but how to explain it?
 A: Consider the general case where $S\subset \mathbb{Z}$. 
Let $A_i:=\{n\in S: n\equiv i \pmod{3} \}$,  for $i=0,1,2$.
Then the sum of three distinct numbers of $S=A_0\cup A_1\cup A_2$ is divisible by $3$ iff it has one of these forms:
$$a_0+a_0+a_0,\quad a_1+a_1+a_1,\quad a_2+a_2+a_2,\quad a_0+a_1+a_2$$
with $a_i\in A_i$.
Now enumerate them and you will get:
$$\binom{|A_0|}{3}+\binom{|A_1|}{3}+\binom{|A_2|}{3}+|A_0||A_1||A_2|$$
where $|A_i|$ denotes the cardinality of the set $A_i$.
A: i find this answer for 1-300 and sum is divisible by 3 i hope it can help you
In how many ways can three numbers be selected from the numbers 1,2,…,300
such that their sum is divisible by 3?

So we can choose the first two numbers how we like. The third has to have a definite residue class mod
3
to make the total divisible by
3
.
If the third residue class is distinct from the residue class the first two numbers, the first two have to be from different residue classes. The number of ways of choosing one from each residue class is ${300 \choose 1} $${200 \choose 1} $${100 \choose 1} $ but there are six different orders in which the same three numbers can be selected.
If the final residue class is the same as one of the previous ones, they all have to be the same. We choose one of the three residue classes, and there are then ${100\choose3}$
ways of choosing a triple.
So the overall number of ways is
$\frac{1}{6}×{300\choose1}×{200\choose1}×{100\choose1}+3×{100\choose3}$
And this is equal to
${100\choose1}×{100\choose1}×{100\choose1}+3×{100\choose3}$
you can find this answer here answer
