Editorial for DMOPC '21 Contest 4 P2 - Festive Groups


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.

Author: Riolku

Subtask 1

The key observation to this problem is that after sorting, it is optimal to greedily fill groups. Specifically, take the smallest element, create a group containing all multiples of this element, and repeat. Proof is left as an exercise.

For this subtask, we can do this naively.

Time Complexity: \mathcal O(N^2)

Subtask 2

For the full solution, we can do a Sieve of Eratosthenes-like approach, marking all multiples as found. Specifically, use a boolean array, each time an element is not already marked, mark all its multiples and increment the counter.

Time Complexity: \mathcal O(\max(J_i)\ \log N)


Comments

There are no comments at the moment.