WorldQuant Round 1 (Codeforces Round 1128, Div. 1 + Div. 2)
H
Oh Tree Tree Tree, No Need to Leave
Codeforces
3500
原题
← G→
翻译中…

This is a communication problem.

翻译中…

There are two players: Player A (Isabel) and Player B (Tara). The jury will first interact with player A. After player A ends their interaction, the jury will interact with player B. Note that player A and player B may not directly pass information to each other; both players are only able to send information or receive information from the jury, but they may agree on the strategy they will use to communicate.

翻译中…

Isabel wants to send Tara a binary string $b$, which has size $n$. She can send information using a complete binary tree of exactly $2^h-1$ nodes, where $h$ is a given positive integer that represents the height of the tree. The nodes have been labelled with integers from $1$ to $2^h-1$, such that node $1$ is the root, and for any non-leaf node $v$ its left child is node $2v$ and its right child is node $2v+1$.

翻译中…

Isabel writes a binary value (0 or 1) on each node of the tree. After she writes a value on each node, she sends the final tree to Tara.

翻译中…

However, Lord Verity (their nemesis), will try to ruin their communication attempt. After Isabel sets the value of each node, Lord Verity will perform the following actions in the order they are listed, exactly once:

翻译中…
  1. He chooses some node $u$.
翻译中…
  1. He flips the value that Isabel wrote on node $u$. Specifically, if Isabel wrote 0 on node $u$, he flips it to 1, and vice versa.
翻译中…
  1. If node $u$ is a leaf, he does nothing else. Otherwise, he swaps node $u$'s child pointers. Specifically, $u$'s left child becomes its right and $u$'s right child becomes its left simultaneously. Then, all nodes are re-labelled such that node $1$ is the root and for any non-leaf node $v$, its left child has label $2v$ and its right child has label $2v+1$.
翻译中…

image

翻译中…

Note that Lord Verity's actions may be adaptive. This means that the node he chooses to mess with is not fixed at the start, and may depend on what Isabel writes on her tree.

翻译中…

After Lord Verity messes with the tree, Tara will receive the structure of the new binary tree, and the value written on each node. She does not know which node Lord Verity has messed with. Formally, she receives a new binary string $l$, where $l_u$ is the value written on node $u$ after all nodes were re-labelled.

翻译中…

Her task is to compute the original string $b$.

翻译中…

There are only two possible combinations of $n$ and $h$:

翻译中…
  • $n = 1, h = 2$
  • $n = 2030, h = 11$
翻译中…

The first combination will only be tested in the examples, for which a correct strategy is provided.

翻译中…

Input

翻译中…

Your code will be ran exactly two times on each test. On the first run, you will be Isabel, and on the second Tara.

翻译中…

First Run Input

翻译中…

The first line of the input contains the string first. The purpose of this is so your program recognizes that this is its first run, and it should act as Isabel.

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1024$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $h$ ($n = 1$ and $h = 2$, or $n = 2030$ and $h = 11$) – the size of the string $b$ and the height of the binary tree respectively. Note that the combination $n = 1$ and $h = 2$ only appears in the examples, and not in the main tests.

翻译中…

The next line contains a single binary string of size $n$, representing $b$.

翻译中…

Second Run Input

翻译中…

The first line of the input contains the string second. The purpose of this is so your program recognizes that this is its second run, and it should act as Tara.

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1024$). The description of the test cases follows. Note that this number is equal to $t$ from the first run input.

翻译中…

The first line of each test case contains exactly two integers $n$ and $h$ — the size of the binary string $b$ and the height of the corresponding tree. The test cases may appear in a different order than in the first run.

翻译中…

The second line of each test case contains a binary string of length $2^h-1$, whose characters are $l_1, l_2, \ldots, l_{2^h-1}$. Here, $l_i$ represents the value of the node with label $i$, which is determined according to both Isabel's and Lord Verity's actions.

翻译中…

Note that after Lord Verity's actions, the labels will differ from the labels Isabel used.

翻译中…

Hacks

翻译中…

To make hacks, use the following format.

翻译中…

The first line contains a single integer $t$ ($1 \le t \le 1024$) — the number of test cases.

翻译中…

The first line of each test case contains three integers $n$, $h$ and $p$ ($n = 2030$, $h = 11$, $1 \le p \le 2^h-1$) — the size of the string $b$, the height of the tree, and the label of the node Lord Verity chooses. The label $p$ refers to the labelling of the tree as Isabel wrote it, before Lord Verity's actions.

翻译中…

The second line of each test case contains a binary string $b$ of size $n$ — the string Isabel has to send.

翻译中…

Note that in hacks Lord Verity is not adaptive: he always chooses node $p$, regardless of what Isabel writes.

翻译中…

Output

翻译中…

First Run Output

翻译中…

For each test case, output a binary string of length $2^h-1$, where the $i$-th character represents the value Isabel sets for node with label $i$.

翻译中…

Second Run Output

翻译中…

For each test case, output a binary string of size $n$, which is Tara's guess as to what $b$ is equal to.

翻译中…

Note

翻译中…

When $n=1$, a correct strategy is the following.

翻译中…

Isabel wants to send $1$ bit but she has $2^h-1=2^2-1=3$ nodes available. Since Lord Verity only flips the value written on a single node, a correct strategy is to write the same value $b_1$ on all nodes of the tree. Then, Tara outputs the most frequent value that appears in $l$.

翻译中…

Take a look at the examples. Note that the order the test cases appear in the first run is not necessarily the same as the order they appear in the second run. Here, the two test cases have been swapped.

翻译中…

Isabel and Tara use the aforementioned strategy only for the case $b = 0$: Isabel writes the value $0$ on all nodes of the tree, so she outputs $000$. Then, Lord Verity uses his operation on node with label $3$ and Tara receives $l = 001$. Since $0$ is the most frequent value in $l$, she outputs $0$.

翻译中…

The other case where $b = 1$ is used only to show how Lord Verity's action affects the tree. Specifically, Isabel chooses to write the value $0$ on node $2$ and the value $1$ on nodes $1$ and $3$. Then, Lord Verity chooses to use his operation on node $1$. First, the value written on node $1$ becomes $0$. Then, its two children are swapped, and they are re-labelled: node $2$ gets label $3$ and node $3$ gets label $2$. Therefore we get $l = 010$. Magically, Tara can correctly determine that $b = 1$.

翻译中…

IMPORTANT NOTE: In these examples, Lord Verity chose to operate on nodes $1$ and $3$. However, when your code runs on the examples, it is not guaranteed that Lord Verity chooses the same nodes, and therefore using the same output as the examples is not guaranteed to be correct.

翻译中…

样例

Input #1
first
2
1 2
0
1 2
1
Output #1
000
101
InputCopy OutputCopy
Input #2
second
2
1 2
010
1 2
001
Output #2
1
0
InputCopy OutputCopy
题目信息
题号
2261H
时间限制
3 s
内存限制
512 MB
标签
位运算
communication
构造
DFS 及类似
交互题
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。