Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Hints: Sum of Digits (and Again)

Write $S(x)$ explicitly as a concatenation of several integers: $x$, then $\text{sumDigits}(x)$, then $\text{sumDigits}(\text{sumDigits}(x))$, and so on until you reach a single digit.

If you only know the multiset of digits you must use (the string $s$), what is the most “invariant” thing you can compute from it that must match $S(x)$?

Answer to Hint 1: The total sum of digits over the whole string is fixed. Since $S(x)$ is just a concatenation of numbers’ decimal digits, the total digit-sum of $S(x)$ must equal the digit-sum of the given string.

Let cnt[d] be how many times digit $d$ appears in $s$. Define SUM = Σ cnt[d]*d. If you decide that the first number in the chain is $x$, can $x$ ever exceed SUM?

Answer to Hint 2: No. All digits in $S(x)$ are nonnegative digits that sum to SUM, so $x$ (whose decimal digits are a subset of those digits) can’t be larger than the total digit-sum bound; in particular, searching $x$ only up to SUM is safe.

Now think of the task as: choose some $x$ and then “spend” digits of $s$ to write the full chain $x, d(x), d(d(x)), \dots$ (where $d(\cdot)$ is digit sum), consuming exactly the available digits. What info about $S(x)$ do you need to compare against cnt efficiently?

Answer to Hint 3: You need the digit counts of the whole string $S(x)$ (not just of $x$). Then you can check need[d] ≤ cnt[d] for all digits.

So the key becomes: for many candidate values of $x$, quickly know the 10-length vector need = countDigits(S(x)). Can you precompute this for all $x$ up to some maximum?

Answer to Hint 4: Yes—dynamic precomputation works because digit-sum reduces fast, and digit counts for a number can be built from i/10 and i%10.

Try precomputing:

  • d[i] = digit sum of $i$
  • digits[i][0..9] = digit frequency vector of $i$

How would you turn that into need[i] = digits(S(i)) (digits of the whole chain)?

Answer to Hint 5: Since $S(i)$ is “digits of $i$” plus “digits of $S(d[i])$”, you can do:

need[i] = digits[i] + need[d[i]], with the base case that if $i < 10$, then need[i] = digits[i].

This means you can compute need[i] in increasing $i$ (because $d[i] < i$ for $i ≥ 10$).

Once you have need[x], checking feasibility is easy. But you still must output an actual permutation of digits. If need[x] is feasible, what do you do with the leftover digits cnt - need[x]?

Answer to Hint 6: The leftover digits must correspond exactly to the “extra digits” that appear in $x$ itself beyond the chain’s digit multiset—or equivalently, you can choose to print leftover digits before printing the chain $S(x)$, because the final output is just a permutation of the multiset.

So one constructive approach is:

  1. print all leftover digits (in any order),
  2. then print the chain $x, d[x], d[d[x]], \dots$ (as decimal strings).

There is one more consistency check: after removing need[x] from cnt, what numeric value should the digit-sum of the leftovers equal?

Answer to Hint 7: If leftover digits represent “some number” placed before the chain, their digit-sum is just the sum of leftover digits. In the model-solution-style construction, the leftover digit-sum is forced to equal $x$ itself, because the overall digit-sum SUM splits into:

SUM = digitSum(S(x)) + digitSum(leftovers).

So compute leftSum = Σ (cnt[d] - need[x][d]) * d and require leftSum == x (along with need[x][d] ≤ cnt[d]).

Now you can search $x = 1..SUM$ and pick the first one that passes both tests. How do you print the “leftovers” nicely without risking leading-zero issues?

Answer to Hint 8: Leading zeros don’t matter because you’re outputting a string, not a parsed integer. So you can print leftover digits in any order.

However, printing leftovers in descending order is convenient and deterministic. After that, print the chain: print $x$, then replace $x \leftarrow d[x]$ until $x < 10$, printing each value as digits appended.

What precomputation limit is enough to cover all candidates $x$ you’ll ever try?

Answer to Hint 9: You only need to precompute up to the maximum possible SUM across tests. Since |s| can be $10^5$, the maximum sum of digits is $9 \cdot 10^5$.

So precomputing arrays up to about $10^6$ is safe. Then for each test case:

  • count digits,
  • compute SUM,
  • scan $x=1..SUM$, check need[x] ≤ cnt and leftSum == x,
  • output leftovers + the chain.