Editorial for WC '17 Finals S3 - Explosive Ordinance Disposal
Submitting an official solution before solving the problem yourself is a bannable offence.
In order to minimize the sum of , we'll want to generally use the smallest
values possible. After some thought, it should become clear that very small values will indeed do the trick – in fact, limiting ourselves to just
is always sufficient to produce a valid solution. However, upon further thought, we realize that those values may not quite yield an optimal solution – in particular, it may be more optimal overall to use various products of small primes. For example, an instance of
could allow many other
values to be reduced from
to
. It's difficult to say exactly how large the
values in an optimal solution might get for a given value of
, but limiting ourselves to all possible values between
and
(inclusive) will be more than enough.
That insight isn't enough to directly suggest how the values should optimally be assigned, but at that point we can use dynamic programming to efficiently find an optimal assignment. Let's represent the system of terminals and wires as a tree, rooted at an arbitrary node such as node
. Then, let
be the minimum sum of
values in the subtree rooted at
, such that
.
can be computed recursively based on the
values of
's children. Each child
can be handled independently, and we can simply consider all possible choices for
such that the GCD of
and
satisfies the wire connecting terminals
and
, choosing the one which minimizes
. At the end, the answer will be
over all possible values of
. The time complexity of this algorithm is
.
Comments