Given the numbers

$$$1, 2, \dots, 20$$$

, you want to choose some subset of these numbers such that no number you choose is divisible by any other number you choose. What's the largest subset you can get?One simple idea is to choose all the primes: 2, 3, 5, 7, 11, 13, 17, and 19, leading to a subset of size 8. But you can do better! Try to figure out the solution yourself before reading below.

For 1292F - Nora's Toy Boxes from Round 614 Div. 1, after you make several observations and pull out a sick counting DP, you get a solution that is

$$$\displaystyle \mathcal{O} \left (N^2 \cdot 2^S \right )$$$

, where $$$S$$$ is the number of values in our array $$$A$$$ that are 1) not divisible by any other value in $$$A$$$ and 2) have at least two other values in $$$A$$$ divisible by it. (These are the only values we should consider using as $$$a_i$$$ in the operation described in the problem.) So what is that time complexity really?Don't worry if you haven't actually read or tried to solve the problem. In the rest of this post I'll focus on this question: out of all subsets of

$$$1, 2, \dots, N$$$

, what is the maximum possible value of $$$S$$$? I'll show that we can attain a precise bound of$$$\displaystyle \frac{N}{6}$$$

.Let's call the numbers that satisfy our constraints above special. First, since each of our special numbers needs to have two other multiples in $$$A$$$, any special number $$$x$$$ must satisfy

$$$3x \leq N$$$

, so$$$\displaystyle x \leq \left \lfloor \frac{N}{3} \right \rfloor$$$

. So we should just find the maximum subset of$$$\displaystyle 1, 2, \dots, \left \lfloor \frac{N}{3} \right \rfloor$$$

such that no number in our subset divides another, and then we can add every number greater than$$$\displaystyle \left \lfloor \frac{N}{3} \right \rfloor$$$

to ensure that we hit the second requirement of at least two multiples for each.With

$$$N = 60$$$

in our case, this leads to the problem described above: given the numbers$$$1, 2, \dots, 20$$$

, what is the largest subset of these numbers we can choose such that no number we choose is divisible by another number we choose? The answer turns out to be 10. First we provide a simple construction: choose$$$11, 12, \dots, 20$$$

. (We can also generalize this: for$$$1, 2, \dots, K$$$

the answer is$$$\displaystyle \left \lceil \frac{K}{2} \right \rceil$$$

.)Now we need to prove that this is the maximum possible. We can do this by building the following chains of numbers:

- 1 — 2 — 4 — 8 — 16
- 3 — 6 — 12
- 5 — 10 — 20
- 7 — 14
- 9 — 18
- 11
- 13
- 15
- 17
- 19

Every number occurs in exactly one chain, and we can never take two numbers from the same chain. So our subset can't be larger than the number of chains, which is again exactly 10. (In general it's the number of odd numbers up to $$$K$$$, which is again

$$$\displaystyle \left \lceil \frac{K}{2} \right \rceil$$$

.)So in the general case, the maximum value of $$$S$$$ is exactly

$$$\displaystyle \left \lceil \frac{\left \lfloor \frac{N}{3} \right \rfloor}{2} \right \rceil = \left \lfloor \frac{N + 3}{6} \right \rfloor$$$

, meaning the algorithm has a complexity of$$$\displaystyle \mathcal{O} \left (N^2 \cdot 2^{\frac{N}{6}} \right )$$$

.Thanks scott_wu for discussing and thinking through this problem.