Editorial for EGOI '21 P1 - Zeros
Submitting an official solution before solving the problem yourself is a bannable offence.
Solution Video: YouTube
We are interested in computing the number of zeros at the end of the number , that is,
of the least common multiple of numbers
.
Subtask 1
When , we can explicitly compute the lcm that Santa is interested in. The maximum value that it
can attain is
.
One way is to use the facts that and that
, where
is the greatest common divisor of
and
and can be computed using a built-in library function
(e.g.
__gcd
in C++) or using the Euclidean algorithm.
Another possibility is to compute the lcm directly from the definition – loop over every positive integer
and check if it is a multiple of all of . (Return the first number that passes this test.)
Once we have computed the lcm, we can find its number of trailing zeros by dividing it by as many
times as possible.
Subtask 2
Here we can still compute the lcm explicitly. However, we must use the first method above, as now the
maximum value is and we cannot afford to make this
many iterations. Moreover, we must take care to use a 64-bit integer variable type (such as
long long
in
C++).
Subtask 3
One solution is to still compute the lcm explicitly – but now the result can have up to digits, so we
need to either implement our own big-number type, or use Python, which has that built in. However,
there is a much more elegant solution, which consists only of several
if
statements.
We can see that the answer is for
, it can only increase as
grows, and it only increases at powers
of
, by
at a time. Thus we can answer
for
,
for
,
for
,
and
for
. But why is that? To see this, let us make some simple observations (which
will be useful also for the following subtasks).
For two positive integers and
, let us denote by
the multiplicity of
as a divisor of
, that is,
the maximum
such that
divides
. Then we have:
is the number of zeros at the end of
.
. This is because
if and only if
and
.
- For a prime number
,
. This is because if we decompose both
and
into prime factors:
and
, where
are different primes and
, then
.
- As a consequence,
For this subtask, the solution follows by noting that when , then
is the
largest
such that
(in other words, it is
). The term corresponding to
is the largest
such
that
, and of course this second
is no larger, as
(equivalently,
). This value
of
is
for
,
for
,
for
, and
for
(since
).
Subtask 4
Now the input numbers are becoming large (remember to read them as 64-bit integers). However, the
condition means that we can use the
formula above explicitly, by
looping over all
. For each such
we compute
directly, by dividing
by
as many times
as possible; this will take at most
steps; and similarly for
.
Subtask 5
We simply generalize the solution of subtask 3 above: instead of several if
statements, we compute
by starting from
and increasing it as long as
.
Subtask 6
To solve the general task, we will compute the expression (and similarly for
) faster than in
time. That is, we want to find the largest
such that
divides some number
. Clearly, this reduces to checking whether a given
is good, as there are only at most
different
-values to check. To find out whether the interval
contains some multiple of
, we can, for example, round
down to the nearest multiple of
, and check if
is still no larger than
the rounded
. More formally, we compute the largest multiple of
that is at most
(this is
) and
check whether it is no smaller than
. If yes, then it is a multiple of
that lies in the interval
;
otherwise, there is no such multiple.
Comments