Editorial for Threes
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
We can prove that it is always possible to make divisble by
by replacing some of its digits with
's: simply replace all of the digits with
's! For a
digit number, this will take
moves.
Okay, but we can obviously do better than that. How can we find the minimum number of replacements possible?
Well, there is a well-known divisibility rule for : a number is divisible by
if and only if the sum of its digits is divisible by
as well. Thus, we can reframe the question as finding the minimum number of integers in an array which must be replaced with
's to make the sum of the array divisible by
.
Since we are working with divisbility by , we can take all digits modulo
and our new target is to achieve a sum which is congruent to
. Now, the only remaining values are
,
, and
. Replacing a digit with a
is equivalent to replacing the digit with a
, or simply deleting the digit.
If the sum of the digits of is already congruent to
,
is already divisible by
. Thus, the minimum number of replacements is simply
.
Otherwise, let the sum of the digits of be congruent to
. If there exists a digit in
which is also congruent to
, we can simply replace that digit with a
, which effectively subtracts
from our sum. Now, the sum of the digits of
are congruent to
. Thus, in this case, the answer is
replacement.
If there does not exist a digit in which is congruent to
, then it turns out the answer is always
. The proof of this is a little bit more tricky, and is left as an exercise to the reader.
Comments