✔ 最佳答案
Interesting question.
You didn't say whether leading zeros are allowed or not. That changes the answer. However, you can have it either way if you solve the problem for an arbitrary number base (radix) r. If N(r,n) is the number of integers m (0<=m<r^n) with nondecreasing digits *and* leading zeros allowed *and* starting with 0 instead of 1, then:
N(r,10)-1 is the answer you want if leading zeros are allowed, or
either N(9,n) is the answer with leading zeros disallowed.
I think that you didn't want leading zeros, since you didn't allow 000...0 as a solution ("positive numbers") and didn't have a 0 in your example. But derive an expression for N(r,n) and you essentially have both answers. I'll call a number "nondecreasing, radix r", or just "nondecreasing" if if fits the pattern, understanding that this includes 0 even though you don't want to count it.
The number of different digits k in a nondecreasing number m is in the range 1 <= k <= min(r,n). There are C(r,k) ways to exactly which k digits appear. The first (and least) of the k digits appears in the leading position, and there are C(n-1,k-1) ways to pick the first appearance of the remaining k-1 unique digits. So the number N_k(r,n) of nondecreasing with exactly k unique digits is:
N_k(r,k) = C(r,k)*C(n-1,k-1)
Now it's easy to see thant N(r,k) is the sum from k=1 to min(r,k) of N_k(r,k).
For the no-leading-zeros answer in base-10 answer, the unrolled solution N(9,n) looks like:
n=1: N = C(9,1)*C(0,0) = 9*1 = 9
n=2: N = C(9,1)*C(1,0) + C(9,2)*C(1,1) = 9*1 + 36*1 = 45
n=3: N = C(9,1)*C(2,0) + C(9,2)*C(2,1) + C(9,3)*C(2,2) = 9*1 + 36*2 + 84*1 = 165
...
and so on up to n=9. From there on, there are precisely 9 terms in the sum.
And no, I don't know how to simplify this, except maybe to change C(r,k)C(n-1,k-1) to factorials.