Yes, it will work, but I would not suggest memorizing it. It is good to understand where it comes from. Let us start with the case that you have $n$ different digits, none of which are zero, and ask for the sum of all the $n$ digit numbers you can form. There are $n!$ numbers, one for each order of the digits. A specific digit $a$ appears in each position $(n-1)!$ times, so if we sum up its contribution we get $a \times (n-1)! \times (111\dots n \text{ times})$ Then summing over the digits gives the first term in your formula. Then if one of the digits is $0$, we need to account for the fact that we do not consider numbers starting with $0$ to be $n$ digit numbers. The sum of all the numbers starting with $0$ is the sum of the $n-1$ digit numbers formed from the remaining $n-1$ digits. We use the same formula as before, but decrease $n$ by $1$ and get the subtraction term.
I believe your approach to the question is correct, however, there seems to be one mistake. In your solution you claim that there are $\frac{10 \cdot 9 \cdot 8 \cdot 7 \cdot 6}{3!} = 5040$ numbers with a particular starting digit. However, this is not quite correct. To illustrate your mistake here is a simpler example:
A simpler example
Let us say that we have the digits $\{0,0,1,2,3,4\}$ and we want to find all of the numbers of length 4 that start with $1$. Then we can list them all out to find that there are $33$ of them:
- $1234, 1243, 1324, 1342, 1423, 1432$
- $1023, 1024, 1032, 1034, 1042, 1043, 1203, 1204, 1230, 1240, 1302, 1304, 1320, 1340, 1402, 1403, 1420, 1430$
- $1002, 1003, 1004, 1020, 1030, 1040, 1200, 1300, 1400$
How the total number is calculated is perhaps hinted by the way I have arranged the numbers above. Namely, we want to calculate in how many ways we can get a number of length $4$ starting with $1$ such that none of the zeros are used, one of the zeros is used, or all of the zeros are used.
- If no zero gets used we get $ \binom{3}{0} \cdot 3 \cdot 2 \cdot 1 = 6$ results.
- If one zero gets used we get $ \binom{3}{1} \cdot 3 \cdot 2 = 18 $ results.
- If two zeros get used we get $ \binom{3}{2} \cdot 3 = 9$ results.
The sum of these yields exactly $33$. However, if I understood your calculation correctly, you would have claimed that there are $\frac{5 \cdot 4 \cdot 3}{2!} = 30$ results which is incorrect.
Back to the original problem
Now coming back to the original problem let us apply the same procedure. Consider any number $XZZZZZ$ where $X$ is fixed and each $Z$ represents an arbitrary digit.
- If no zero gets used we get $ \binom{5}{0} \cdot \frac{7!}{2!} = 2520 $ results.
- If one zero gets used we get $ \binom{5}{1} \cdot \frac{7!}{3!} = 4200$ results.
- If two zeros get used we get $ \binom{5}{2} \cdot \frac{7!}{4!} = 2100$ results.
- If three zeros get used we get $ \binom{5}{3} \cdot \frac{7!}{5!} = 420 $ results.
So in total there are $9240$ numbers where $X$ is the first digit.
Now note that an integer contributes nothing to the sum if it is $0$ and fix the first two digits so that our number is given by $XYZZZZ$. We then ask ourselves how many numbers of this kind we can make. This is simply a slight alteration of what we did before.
- If no zero gets used we get $ \binom{4}{0} \cdot \frac{6!}{2!} = 360 $ results.
- If one zero gets used we get $ \binom{4}{1} \cdot \frac{6!}{3!} = 480$ results.
- If two zeros get used we get $ \binom{4}{2} \cdot \frac{6!}{4!} = 180$ results.
- If three zeros get used we get $ \binom{4}{3} \cdot \frac{6!}{5!} = 24 $ results.
Now allowing $Y$ to be any non-zero value we see that there are $1044 \cdot 7 = 7308$ numbers which start with $X$ and have a non-zero second coefficient.
Since the same procedure can be applied to the number $XZZZZZ$ where any $Z$ is replaced by a $Y$, we conclude that our answer is:
$$36 \cdot \left( 9240 \cdot 10^5 + 7308 \cdot 11111 \right)$$
A general solution
By now, you might be able to see how we can construct the general solution to this problem. If we have an input of consisting of $m$ zeros, the first $n$ positive integers and you are creating a number of length $r$ with $m \leq r-2$ and $n \geq r$ then I believe the general solution to this problem is given by:
$$ \frac{(n)(n+1)}{2} \cdot
\left(
10^{r-1}\sum_{i=0}^m \left[\binom{r-1}{i} \cdot \frac{(n-1)!}{(n-r+i)!} \right]
+ \left(\sum_{i=0}^{r-2} 10^i\right)
\sum_{i=0}^m\left[\binom{r-2}{i} \cdot \frac{(n-1)!}{(n-r+i)!} \right]
\right)
$$
Comment
Combinatorics is not my strong suit by any stretch of the imagination, so by all means, please point out anything you think is incorrect or is explained poorly.
Best Answer
Think like this For the sum of all digits at thousands place the expression would be
$1000(1+2+3+4+5+6+7+8+9)×{9\choose3}×3!.$ Explanation- You surely dont have $0$ therefore now only $9$ possibilities left for thousands place suppose you take $1$ at thousands place now you need to see how many times does it repeat i.e $9\choose3$ since you need to select $3$ digits out of $9$(0,2,3...9) possibilites also you need to see in how many ways those $3$ would be arranged that is $3!$
Now- For hundreds, tens, units place there would be two separate cases for $0$ coming at those places and ${1,2,3...9}$ coming at those places. For $0$ we see it repeats $9×8×7$ times (but it doesnt affect the sum) For digits ${1,2,...9}$ they repeat $8×8×7$ times ,now how does that come since you dont have $0$ at thousanda place so only $8$ possibilities and for other place $8$ possibilities again (since you can put $0$) now and $7$ possibilities for the last place .
$9×8×7(0)(111)+8×8×7(1+2+3+4+5+6+7+8+9)(111)$$
And the final answer-
$\big(1000(1+2+3+4+5+6+7+8+9)×{9\choose3}×3!\big)+ \big(8×8×7(1+2+3+4+5+6+7+8+9)(111)\big)$ $=1000×9×8×7×45+8×8×7×45×111=24917760$
Or According to your question
$A$ is $8×8×7$ and $B$ is $9×8×7$
Thank you