Editorial for Fives
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
In this subtask, . Thus, we must find the smallest number which is not representable on
hands.
It turns out that the pattern is 6, 16, 66, 166, 666, 1666, 6666, 16666, 66666 ...
In other words, if is odd, the answer is
6...666
, containing
6
's. If is even, the answer is
16...666
, containing
6
's and digits in total.
In python, this can be written compactly as "16"[n%2]+"6"*(n//2)
.
Time Complexity:
Bonus
Interestingly, this is the exact same pattern found here. In roman numerals, the sequence, I, VI, XVI, LXVI, CLXVI, ...
, achieved by concatenating the next type of roman numeral symbol to the left, corresponds exactly to 1, 6, 16, 66, 166, 666, 1666, 6666, 16666, 66666 ...
, including as the smallest number which cannot be represented with
hands.
Credits to
for noticing this.Proof
To prove the pattern above is correct, we first note that it is never better to use three or more hands in one group. This is because:
-
Creating a number
from a single group would require at least
hands, but the same number could be constructed by concatenating
pairs of hands, each pair forming a digit from
to
, with a total of
hands. For all
, it holds that
, so it is never better to use one group to construct numbers greater than or equal to
.
-
The numbers from
to
are not worth constructing with one group, since they would require at least
hands, but they can alternatively be constructed with two hands in separate groups by simply concatenating one hand holding up
finger and the other hand holding up the desired units digit.
-
All numbers from
to
can be constructed with less than or equal to two hands in one group.
Intuitively, concatenation is much more efficient at representing numbers than addition, and addition is only used to give us access to the digits through
.
From this, we can now determine that among all representations of a number which use the minimum possible number of hands, there exists a representation which uses only groups with either or
hands.
With one hand in a group, we can create the digits to
. With two hands in a group, we can create the digits
to
, and we can technically create the sequence of digits
, but we could already do that with two hands in separate groups, one holding up
finger, and the other
. Thus, we can simply limit all possible sequences of digits we can make from groups containing
or
hands to be any single digit from
to
, and we will still be using the minimum number of hands possible.
We can now rephrase the previous statement like so: Among all representations of a number which use the minimum possible number of hands, there exists a representation which constructs and concatenates each digit one by one using groups with either or
hands. Since we know that the digits from
to
can be constructed with
hand and the digits from
to
must be constructed with
hands, we can infer that the minimum number of hands to represent a number
is the number of digits in
in the range
plus two times the number of digits in
in the range
, or in other words,
The minimum number of hands to represent a numberis the number of digits in
plus the number of digits in
which are strictly greater than
.
So, to solve subtask 1, we must find the minimum number where the value described above exceeds
. An intuitive way to find this number is to use a greedy approach. Observe that if
has a fixed number of digits, we should aim to maximize the number of digits of
which are strictly greater than
to maximize the minimum number of hands required to represent
. We should also pick
, the smallest digit which is striclty greater than
, so as to minimize
. If any digits are to be less than or equal to
, we should make them
if they are not leading, and
otherwise. Additionally, notice that the number of hands needed to represent
is unaffected by permuting the digits of
. Thus, we should always put the digits which are less than or equal to
in front (more significant digits) and the digits which are strictly greater than
to the right (less significant digits), so as to minimize
.
For odd , this naturally leads to the construction
6...666
, containing
6
's. Each 6
requires hands to represent, so in total this number needs at least
hands to represent, which exceeds
.
For even , this leads to the construction
16...666
, containing a 1
followed by
6
's. The takes
hand to represent, and each
6
requires hands to represent, so in total this number needs at least
hands to represent, which exceeds
.
Proof that all numbers strictly less than these constructed numbers CAN be represented on
hands (and thus these constructed numbers are truly the first which cannot be represented on
hands)
More formally, we can show that all numbers strictly less than the constructed unrepresentable numbers CAN be represented on hands to prove that our constructed unrepresentable numbers are truly the first of their kind.
Claim: For all odd , all numbers strictly less than
6...666
, which contains
6
's, can be represented on hands (specifically with leading zeros as necessary such that the resulting representation contains exactly
digits).
Proof: Proceed by induction on odd .
Base Case: When , all numbers from
to
can be represented with
hand such that each representation contains exactly
digit.
Induction Hypothesis: Assume for some odd integer that all numbers strictly less than
6...666
, which contains
6
's, can be represented on hands (specifically with leading zeros as necessary such that the resulting representation contains exactly
digits).
Induction Step: The next odd number is . Consider creating
pairs of hands, leaving one remaining hand. If we put the extra hand in its own group and make it the most sigificant digit, it can represent any digit from
to
. The remaining
pairs of hands can create any sequence of
digits from
to
. Thus, all numbers from
0...000
to 59...999
can be represented such that they contain sufficient leading zeros to have exactly digits.
If instead a pair of hands is used to make the most sigificant digit, we will be left with
hands, which by our induction hypothesis, can represent any sequence of digits from
0...000
to 6...6665
such that it contains exactly digits using leading zeros as necessary. So by concatenating the
at the front, we can represent the remaining numbers from
60...000
to 6...6665
such that they contain exactly digits.
Thus, all numbers strictly less than 6...666
, which contains
6
's, can be represented on hands (specifically with leading zeros as necessary such that the resulting representation contains exactly
digits).
By induction, the original claim is true.
Now, for even , we can either create
pairs of hands to represent any sequence of
digits from
to
, which allows us to represent all numbers from
0...000
to 9...999
containing digits, OR, we can use one hand to make the leading digit
, and use the remaining
hands to create any sequence of digits from
0...000
to 6...6665
with leading zeros as necessary to have exactly digits (by our construction for odd
) to represent any number from
10...000
to 16...6665
on hands. Thus, for even
, all numbers strictly less than
16...666
, which contains digits, can be represented on
hands.
Subtask 2
Continuing off of the observations found in the proof for subtask 1, we can now generalize to finding the first number greater than or equal to which is unrepresentable. Note that
is extremely big in this subtask (up to
digits), so we should not store it as an integer, but rather as a string or list of digits/characters.
-
If
is less than or equal to the smallest unrepresentable numbers found in subtask 1, we should simply print the answer to subtask 1.
-
If
is already unrepresentable, we should simply output
. This can be checked by seeing if the number of digits in
plus the number of digits in
which are strictly greater than
exceeds
.
-
Otherwise,
will have somewhere between
and
digits, inclusive. Thus, there will exist a number with the same number of digits as
which is unrepresentable (e.g. all
's requires at least
hands). So, we can fix the number of digits of the next unrepresentable number to be the same as the number of digits of
(let this be
), and we require that the number of digits of the next unrepresentable number which are strictly greater than
to be strictly greater than
.
Now, consider the behaviour when incrementing
until it has strictly greater than
digits which are strictly greater than
. This will result in changes starting from the least significant digits, and will have new
's created (the smallest digit strictly greater than
). Thus, we can consider replacing some suffix of
with
's such that
has more than
digits which are strictly greater than
in total. This can be implemented more easily by looping through the digits of
from least significant to most sigificant until a counter has exceeded
, where each iteration the current digit is replaced with
and the counter is only incremented if it was less than or equal to
. The reason this works is because the last digit which is replaced by a
must be less than or equal to
(so it is increased), and thus all the digits after it are subject to change.
Time Complexity:
Comments