Editorial for WC '17 Finals S3 - Explosive Ordinance Disposal


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

In order to minimize the sum of V_{1 \dots N}, we'll want to generally use the smallest V values possible. After some thought, it should become clear that very small values will indeed do the trick – in fact, limiting ourselves to just 1 \le V_i \le 3 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 V_i = 6 could allow many other V values to be reduced from 3 to 2. It's difficult to say exactly how large the V values in an optimal solution might get for a given value of N, but limiting ourselves to all possible values between 1 and 30 (inclusive) will be more than enough.

That insight isn't enough to directly suggest how the V 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 1. Then, let DP[i][v] be the minimum sum of V values in the subtree rooted at i, such that V_i = v. DP[i][v] can be computed recursively based on the DP values of i's children. Each child c can be handled independently, and we can simply consider all possible choices for V_c such that the GCD of V_i and V_c satisfies the wire connecting terminals i and c, choosing the one which minimizes DP[c][V_c]. At the end, the answer will be \min\{DP[1][v]\} over all possible values of v. The time complexity of this algorithm is \mathcal O(N).


Comments

There are no comments at the moment.