PROBLEM LINK:
Author: Kevin Atienza
Tester: Kevin Atienza and Gedi Zheng
Editorialist: Kevin Atienza
PREREQUISITES:
String processing, divisibility rules
PROBLEM:
Turn a given number N into a multiple of 9 using a sequence of moves. The only allowed moves are to increment or decrement a digit by one, but you cannot increase a digit 9 or decrease a digit 0, and the resulting number must not contain any leading zeroes unless N has a single digit. (This means that a number can only have a leading zero if it is 0)
What is the minimum number of moves needed?
QUICK EXPLANATION:
Let S be the sum of the digits of N, and let M be S \bmod 9. If N has more than one digit and S < 9, then the answer is 9 - M. Otherwise, the answer is \min(M, 9-M).
EXPLANATION:
Before we answer the problem, letâs first discuss which numbers are divisible by 9.
Divisibility by 9
When is a number N divisible by 9? You might remember a really simple way of checking whether a number is divisible by 9:
A number x is divisible by 9 if and only if the sum of the digits of x is divisible by 9.
Letâs show why this is true. Actually, weâll show the following stronger statement:
The remainder when x is divided by 9 is equal to the remainder when the sum of the digits of x is divided by 9.
Proof:
Suppose the digits of x from most to least significant are x_{d-1}, x_{d-2}, \ldots, x_0, i.e.
The sum of digits of x, which we denote as S(x), is therefore
Now, x - S(x) is equal to the following:
x - S(x) is clearly divisible by 9, but this can only happen if x and S(x) have the same remainder when divided by 9.
Computing the answer
The fact above actually allows us to quickly compute the remainder of N when divided by 9: N \bmod 9 is simply S(N) \bmod 9. But S(N) is easy to compute, and from the upper limit of N, we are sure that S(N) \le 9\cdot 10^5. Therefore, we can easily compute S(N) \bmod 9 with an additional step.
This is great, but how does this help us answer the question? Well, note that the only allowed operations are to increment or decrement a digit, and each operation increases or decreases the sum of digits by exactly one. This gives us a few observations:
- If there are I increment operations and D decrement operations, then the resulting sum of digits is S(N) + I - D, and we want this to be divisible by 9.
- An optimal sequence of operations contains only increment operations or only decrement operations. This is because each pair of increment and decrement operation cancels each other out in terms of the sum of digits.
- If a sequence of operations consists purely of increment operations (i.e. D = 0), then I = (9 - S(N)) \bmod 9. (Note that this is always nonnegative)
- If a sequence of operations consists purely of increment operations (i.e. I = 0), then D = S(N) \bmod 9.
Thus, our current answer is \min((9 - S(N)) \bmod 9, S(N) \bmod 9). But is this correct? Consider the following case:
We have S(N) = 4, so our âanswerâ is \min((9 - 4) \bmod 9, 4 \bmod 9) = \min(5, 4) = 4. However, we cannot decrement this number four times, because we are not allowed to introduce leading zeroes! How do we fix this?
Well, we can fix this by noting that we can only introduce leading zeroes by decrementing digits, and that we are only forced to introduce leading zeroes if the sum of the digits itself is less than 9. Therefore, our âadjustedâ solution is the following:
This solution now successfully gives the correct answer 5 for the N above.
But there is still a problem! Consider the case N = 1. The formula above gives the answer 8, but in fact we can decrement N to become 0 this time, because N has a single digit! Therefore, we need to adjust the above answer again:is the following:
Now thatâs better!
What about incrementing? Will there ever be a case where we are required to increment so many digits that we run out of places to increment? The answer is no, because we only get stuck if the number consists purely of 9 digits, i.e. 99999999\ldots 999, but this number is divisible by 9, so we are guaranteed not to exceed this number!
Time Complexity:
O(D) = O(\log N), where D is the number of digits of N