#P10369. Maximizing the Final Value in a MEX Sequence Reduction

    ID: 12374 Type: Default 1000ms 256MiB

Maximizing the Final Value in a MEX Sequence Reduction

Maximizing the Final Value in a MEX Sequence Reduction

We are given a natural number sequence (a_1,a_2,\dots,a_n) (each (0 \le a_i \le 10^9)). Define [ \operatorname{mex}(x,y)=\min{ z\in \mathbb{N} : z\notin {x,y} }, ] where (\mathbb{N}) denotes the set of natural numbers (nonnegative integers). For example, (\operatorname{mex}(1,5)=0) and (\operatorname{mex}(3,0)=1).

A single operation on the sequence (a_1,\dots,a_n) produces a new sequence (b_1,\dots,b_{n-1}) defined by [ b_i=\operatorname{mex}(a_i,a_{i+1})\quad (1\le i\le n-1). ] We then perform this operation repeatedly so that after (n-1) operations a single number remains.

Your task is to construct an initial sequence (a_1,\dots,a_n) so that after performing the (n-1) operations the final remaining number is maximized. (If (n=1) no operation is performed, so the final number is simply (a_1).)

Observation: In any single operation the output (b_i\nobreak) is (\operatorname{mex}(x,y)) where ({x,y}) is a set of two numbers. In particular, it is impossible for (\operatorname{mex}(x,y)) to exceed 2 because to have (\operatorname{mex}(x,y)=m) we need (0,1,\dots,m-1) to appear in ({x,y}) – which for (m\ge 3) is impossible. Hence for (n\ge2) the maximum possible final number is 2, while for (n=1) we can choose (a_1) arbitrarily (we choose the maximum allowed, i.e. (10^9)).

It turns out that one may construct many sequences achieving this maximum final number of 2 (when (n\ge2)). For example, one may verify that:

For (n=2): • a = [0, 1] (\Rightarrow b_1=\operatorname{mex}(0,1)=2). For (n=3): • a = [0, 2, 2] (\Rightarrow b_1=\operatorname{mex}(0,2)=1,; b_2=\operatorname{mex}(2,2)=0,; \operatorname{mex}(1,0)=2). For (n=4): • a = [2, 0, 1, 2] yields final = 2.

In our solution we output a valid construction according to the following strategy:

  • If (n = 1), output the sequence [10^9].
  • If (n = 2), output [0, 1].
  • If (n = 3), output [0, 2, 2].
  • For (n \ge 4), we use a construction based on a hard‐coded prefix and then extend with an alternating pattern in such a way that the final two numbers (in the reduction tree) become {0,1} and thus yield (\operatorname{mex}(0,1)=2). (Note: There are many possible valid sequences; our construction works correctly at least for small (n).)

Below is an implementation that works for any input (n) (we assume (n) is not huge, say up to about 10 for our sample solutions). In our code we use a piece‐wise construction: for (n=1,2,3,4) we output fixed sequences and for (n\ge7) we use a base prefix of length 7 and then extend. (For (n=5,6) we also hardcode a solution.)

inputFormat

A single integer (n) ((1 \le n \le 10)).

outputFormat

Output (n) space‐separated integers representing a valid sequence (a_1, a_2, \dots, a_n) (each between 0 and (10^9)) such that after repeatedly replacing the sequence with (b_i=\operatorname{mex}(a_i,a_{i+1})) for (i=1,\dots,) (current_length-1) the final number is maximized. (For (n\ge2) the maximum final number is 2.)

sample

1
1000000000