I've had some fun digging through the constructive problem K: you are given a positive integer *k* up to 10^{6}, and need to produce any set of at most 30 integers with absolute values not exceeding 10^{16} such that the number of its subsets with zero sum, including the empty subset, is exactly *k*. I doubt it's possible to solve this problem completely in one's head or on paper, so if you're interested, I encourage you to try some idea out with a computer :)

*random*permutation of size

*n*<=15000 onto this tape in the following manner: you write the first number somewhere, then you either stay in the same cell or move one cell to the left or to the right, then write the second number there, then again either stay or move to an adjacent cell, write the third number there, and so on. Whenever you write a number into a cell that already has a number, the old number is overwritten and therefore discarded. After all

*n*numbers have been written, we look at the resulting sequence written on the tape from left to right. What is the maximum possible size of an increasing subsequence of this sequence? Note that the given permutation is guaranteed to have been picked uniformly at random from all permutations of size

*n*.

The first idea is relatively standard for problems of this type: overwriting is hard to deal with, so let us look at the process from the end. Then instead of overwriting we will just skip writing numbers to cells that already have a number, and therefore we will always have a segment of cells that have numbers that will not change anymore, and the rest of the cells free.

The second thing to get out of the way is the "increasing subsequence" complication. In the new setup where we go from the end, it's clear that there's no point at all writing numbers that will not end up in that increasing subsequence, as we can simply stay in the current position and skip writing the unnecessary number instead of moving left/right to write it. The only exception to this is the very first number we write (in reverse order, so the last number of the input permutation) which will always be written even if it's useless for the increasing subsequence. We can deal with this peculiar property of the last number separately, so for the remainder of this post I will forget about it for simplicity.

Now all numbers we write must form an increasing sequence. The property "increasing" is local, so all subsequent numbers will only be compared with the first or the last number written so far, which leads us to a dynamic programming solution. Having processed some prefix of the reversed permutation, only four things matter:

- what is the first number of the sequence we have so far
- what is the last number of the sequence we have so far
- what is the length of the sequence we have so far
- where in this sequence are we

*n*

^{5}) states, so while polynomial, this solution does not really cut it for

*n*<=15000. So we need a few tricks to reduce the state space.

*n*

^{0.5}) instead of O(

*n*) (more info). Therefore our position inside the sequence also has just O(

*n*

^{0.5}) options, so we have reduced the number of states to O(

*n*

^{4}) just by staring at the solution for a bit and without changing it :)

*smallest*last number for each combination of (first number, length, our position), which brings us down to O(

*n*

^{3}) states. Symmetrically, we could have also stored the

*biggest*first number for each combination of (last number, length, our position).

*n*

^{2.5}).

*n*

^{1.5}), finally something manageable for

*n*<=15000.

*n*) transitions for each state and O(

*n*

^{2.5}) running time.

*n*)), solving our problem in O(

*n*

^{1.5}log(

*n*)), which is fast enough. I hope you enjoyed this long process of unwrapping the present as much as I did :)

## No comments:

## Post a Comment