I'm solving a problem which asks me to find how many 5-digit numbers with distinct digits are there which are also divisible by $3$. I am familiar that there are $9\cdot9\cdot8\cdot7\cdot6=27216$ five-digit numbers with distinct digits. I know that a natural number $n$ is divisible by $3$ iff the digits sum up to a number divisible by $3$. Now, brute-forcing by a computer algorithm, i found that the result is $20928$. However, I am interested, if there is a mathematical solution for this. I know how to find amount of numbers divisible by $4$ or $5$. We just fix some amount on the last two (or last one) digit. But criterion for divisibility by $3$ is very different from those and I have no idea how to start. Eventually, I would like to find a general formula for $n$-digit numbers.
Edit: Equivallently we want to find how many solution does the equation $a+b+c+d+e=3k$ for $a,b,c,d,e\in\{0,1,2,...,9\}$, $a\neq 0$ also $a,b,c,d,e$ are all distinct and $k\in\mathbb{Z}$ (the highest possible $k$ is in this case $k=11$, the smallest being $k=5$)
Edit (2): According to the ideas from the answer. Make $A=\{0,3,6,9\},B=\{1,4,7\}, C=\{2,5,8\}$. Let's go through the cases that in the number $n$ there are $0,1,2,3,4$ digits from $A$.
i) Suppose none of $A$ is in $n$. We can't form a number divisible by three. We have to take 5 numbers, that is $3$ from $A$ and $2$ from $B$ or $2$ from $A$ and $3$ from $B$. None of the expressions $3(3k+1)+2(3k+2)$ and $2(3k+1)+3(3k+2)$ are divisible by three.
ii) Let $1$ digit from $A$ be in $n$. The amount of ways to choose the digit is $5$. Now we are left to choose $4$ digits. The only possible way is to choose $2$ from $A$ and $2$ from $B$. The amount of ways to do this is ${3\choose 2}^2=9$ which together gives $5\times 9$ ways which can be permuted and alltogether $5\times 9\times 5!=5400$ possible ways.
iii) Let $2$ digits from $A$ be in $n$. The amount of ways to choose the digit is ${5\choose 2}=10$. We are left with $3$ digits. Either all of them are from $A$ or all of them are from $B$. That's $2$ possible ways and all together $20$ possible ways which we permute to obtain $20\times 5!=2400$ possible ways.
iv) Let $3$ digits from $A$ be in $n$. The amount of ways to choose the digits from $A$ is ${5\choose 3}=10$. We are to choose $2$ digits. We must take exactly one from $B$ and exactly one from $C$. The amount of ways to do this is ${3\choose 1}^2=9$. Alltogether $9\times 10$ possible ways which we permute to get $90\cdot5!=10800$ possible ways
v) In the last case where all four digits from $A$ are present, there is no way how to make the number divisible by $3$.
Summing this all up gives $5400+2400+10800=18600$ which is less than what my algorithm yielded. We also have to subtract these which begin with $0$. Can anyone spot the mistake? (If i did any?)