Editorial for Yet Another Contest 2 P2 - Secret Sequence
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For this editorial, denote as the interactor's response after making a query with
and
. Also,
is the sequence which we are trying to find. Finally, let
denote the bitwise XOR operator.
If we do not restrict ourselves to maximising the value of , then there are many possible solutions, one of which will be described here.
First, we can query ,
and
, which allows us to find
,
and
.
Then, for each from
up to
, we can find
by calculating
.
Again, there are many constructions which produce a value of . It turns out that this is the maximum possible value of
. A proof of this will follow after an example of a possible construction.
First, find the value of for
. This lets us work out the value of
for
, since
.
Then, we can work out the value of for
with one additional query for each
, since
. Cumulative prefix XOR sums can be used to speed up this computation.
Finally, we can work out as equal to
. Note that
has already been found from the first step, so this does not require an extra query.
This solves the problem with queries and
.
Now, let's consider why this value of is maximal. Let
represent the prefix XOR sequence, where
. For convenience, let
. Note that being able to work out sequence
is a necessary and sufficient condition for being able to work out sequence
, so we will turn our attention to finding sequence
.
We can see that . Therefore, if we know
and one of
or
, then we can work out the other value out of
or
.
This motivates the following idea: create a graph containing nodes labelled
. Whenever we make a query asking for
, then add a bidirectional edge between
and
. Then, two nodes
and
are connected if and only if knowing the value of
allows us to figure out
, and vice versa.
After performing queries, we require the graph to be connected; otherwise, there is no unique solution. Since there are
nodes and
edges, the edges must form a spanning tree of this graph. This also explains why the minimal number of queries to find sequence
is
.
Recall that the value of is the minimum value of
, for all edges
in the graph. Now, consider node
. Since the edges form a spanning tree, this node must be connected to another node. However, the minimum value of
for
is
. This proves that
cannot exceed
. On the other hand, we have already shown via construction that this value of
is attainable, so
is the maximum possible value of
.
This graph idea also lends itself to a different construction. First, generate any spanning tree of the nodes such that
for all edges
. For each edge
in this graph with
, let it have a weight of
. We know that
. Performing a DFS/BFS on this graph, we can work out the entire sequence
, as
is simply the XOR sum of all edges on the path from node
to node
. Once we know
, we can easily find sequence
, since
for all
.
Comments