Project Euler Problem 885

For a positive integer d, let f(d) be the number created by sorting the digits of d in ascending order, removing any zer

Project Euler Problem 885

Solution

Answer: 827850196

Let the nonzero digits of a number $d$ be

$$1^{c_1}2^{c_2}\cdots 9^{c_9},$$

where $c_i\ge 0$, and let

$$k=c_1+\cdots+c_9$$

be the number of nonzero digits.

Then

$$f(d)=\underbrace{11\ldots1}{c_1} \underbrace{22\ldots2}{c_2} \cdots \underbrace{99\ldots9}_{c_9},$$

i.e. the digits sorted increasingly with all zeros removed.

We must compute

$$S(18)=\sum_{\substack{d\ge1\ \text{at most 18 digits}}} f(d) \pmod{1123455689}.$$


1. Mathematical analysis

Step 1 — Fix the nonzero digits

Suppose the multiset of nonzero digits is fixed:

$$(c_1,c_2,\dots,c_9).$$

Then the value of $f(d)$ is fixed.

The only remaining freedom is:

  • how many zeros there are,
  • how all digits are arranged.

Let the number contain $c_0$ zeros, so total length is

$$n=k+c_0.$$

Because we only consider positive integers, the leading digit cannot be zero.


Step 2 — Count valid numbers

The total number of permutations of the multiset is

$$\frac{n!}{c_0!c_1!\cdots c_9!}.$$

Among these, the fraction whose first digit is nonzero is

$$\frac{k}{n},$$

because exactly $k$ of the $n$ positions contain nonzero digits.

Hence the number of valid $n$-digit integers is

$$\frac{k}{n}\cdot \frac{n!}{c_0!c_1!\cdots c_9!} = \frac{k(n-1)!}{c_0!c_1!\cdots c_9!}.$$

Now rewrite:

$$\frac{k(n-1)!}{c_0!} = \frac{k!}{1}\binom{n-1}{c_0}.$$

Indeed,

$$k\frac{(k+c_0-1)!}{c_0!} = k!\binom{k+c_0-1}{c_0}.$$

Therefore the count becomes

$$\frac{k!}{c_1!\cdots c_9!} \binom{k+c_0-1}{c_0}.$$


Step 3 — Sum over all possible zero counts

For fixed nonzero counts $(c_1,\dots,c_9)$, the zero count may be

$$0\le c_0\le 18-k.$$

Thus the total multiplicity is

$$\frac{k!}{c_1!\cdots c_9!} \sum_{c_0=0}^{18-k} \binom{k+c_0-1}{c_0}.$$

Using the hockey-stick identity,

$$\sum_{t=0}^{m}\binom{k+t-1}{t} = \binom{k+m}{m}.$$

With $m=18-k$,

$$\sum_{c_0=0}^{18-k} \binom{k+c_0-1}{c_0} = \binom{18}{k}.$$

So every multiset contributes

$$f \cdot \frac{k!}{c_1!\cdots c_9!} \binom{18}{k}.$$

Hence

$$S(18) = \sum_{k=1}^{18} \binom{18}{k} \sum_{c_1+\cdots+c_9=k} f(c_1,\dots,c_9) \frac{k!}{c_1!\cdots c_9!}.$$

This is now finite and small.

The number of 9-tuples with total $k\le18$ is

$$\binom{18+9}{9}\approx 4.7\times10^4,$$

which is tiny.


2. Python implementation

from math import factorial, comb

MOD = 1123455689
N = 18

# Precompute factorials
fact = [factorial(i) for i in range(N + 1)]

answer = 0

def dfs(digit, remaining, counts):
    """
    Recursively generate all count vectors:
        c1 + c2 + ... + c9 <= N
    """
    global answer

    if digit == 10:
        k = sum(counts)

        # Ignore the empty multiset
        if k == 0:
            return

        # Build f(d):
        # digits sorted increasingly with repetitions
        s = []
        for d, c in enumerate(counts, start=1):
            s.extend(str(d) * c)

        f_value = int("".join(s))

        # multinomial factor = k! / (c1! c2! ... c9!)
        mult = fact[k]
        for c in counts:
            mult //= fact[c]

        # Contribution
        contribution = f_value * mult * comb(N, k)

        answer += contribution
        return

    # Choose count for current digit
    for c in range(remaining + 1):
        dfs(digit + 1, remaining - c, counts + [c])

dfs(1, N, [])

print(answer % MOD)

3. Code walkthrough

Factorials

fact = [factorial(i) for i in range(N + 1)]

Used for multinomial coefficients.


Recursive generation of digit counts

dfs(digit, remaining, counts)

Generates all tuples

$$(c_1,\dots,c_9)$$

with total at most $18$.


Constructing $f(d)$

s.extend(str(d) * c)

If $c_3=2$, this appends "33".

For example:

  • counts corresponding to digits $3,3,4$
  • produce "334"
  • so $f(d)=334$

Multinomial coefficient

mult = fact[k]
for c in counts:
    mult //= fact[c]

Computes

$$\frac{k!}{c_1!\cdots c_9!}.$$


Final contribution

contribution = f_value * mult * comb(N, k)

This is exactly the formula derived above.


4. Verification

The program reproduces the given check:

$$S(5)=1543545675.$$

So the derivation is consistent with the statement.

The final computation gives

$$S(18)\bmod 1123455689 = 827850196.$$


Answer: 827850196