Editorial for COCI '15 Contest 7 #5 Prosti
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's assume a fixed (the number of consecutive numbers) and denote the number of happy numbers in the set
with
. It is clear that
.
Additionally, we can use brute force to find the first consecutive composite numbers. If they begin with the number
, then it will hold
for each
.
Lemma: Let and
be integers and let
. Then there exists
from the interval
such that
.
The proof of this lemma is simple and is left as an exercise to the reader.
Now, let's work with the numbers from the task. By applying the lemma to
and
, we know that a solution must exist in the interval
. However, iterating over the entire interval would be too slow.
We use the binary search technique. Let's assume that we know for sure that our solution is in the interval . Let
. Then we can apply the lemma to one of the intervals
or
and solve the task recursively.
The total time complexity of this algorithm is .
Comments