Editorial for COCI '12 Contest 1 #5 Snaga
Submitting an official solution before solving the problem yourself is a bannable offence.
An important observation is that, for any large , the following number in the sequence – the smallest positive integer that doesn't divide it – is very small. Thus, there are very few possible numbers following any large number, hence it is natural to approach the problem as follows: for each of the possible followers
we can count the numbers between
and
followed by
, making it easy to compute the sum of their strengths – they all have a strength of
.
How can we count the numbers for a given ? What are the conditions for a number
to be followed by
? It has to be divisible by all numbers less than
(otherwise one of them would be the follower of
), which means it is also divisible by their least common multiple,
. Furthermore, it must not be divisible by
itself. These two conditions are obviously both necessary and sufficient.
Thus, let us count the numbers between and
divisible by
. From that number, we need to subtract the numbers divisible by
. The numbers in the intersection of the two sets (divisible by both
and
) are also divisible by
, so it is easy to find and subtract their count between
and
. Generally, we can count the numbers between
and
(inclusive) divisible by
using the formula (think about why it is correct):
.
Finally, for which numbers do we need to carry out the computation? From the discussion above, it is obvious that as soon as
becomes greater than
, we are done: there are no numbers between
and
with such (or any greater)
(since they aren't divisible by
, being smaller than it). It turns out that the largest
will be less than
. It is easy to compute the strength for such
, increment it by
and add it to the total sum as many times as there are numbers between
and
whose follower is
.
The last thing to consider is calculating . Notice that
so if we have calculated in the previous step, then
can be obtained using the formula
, which is valid for any two positive integers.
, the greatest common divisor, is easily obtained using Euclid's algorithm.
Comments