Problem: ioi16_e
Categories: bit, search
Difficulty: hard
Time limit: 1000 ms | Memory limit: 512 MB

Ilshat is a software engineer working on efficient data structures. One day he invented a new data structure. This data structure can store a set of non-negative $n$-bit integers, where $n$ is a power of two. That is, $n = 2^b$ for some non-negative integer $b$.

The data structure is initially empty. A program using the data structure has to follow the following rules:

* The program can add elements that are $n$-bit integers into the data structure, one at a time, by using the function add\_element(x). If the program tries to add an element that is already present in the data structure, nothing happens.
* After adding the last element the program should call the function compile\_set() exactly once.
* Finally, the program may call the function check\_element(x) to check whether the element $x$ is present in the data structure. This function may be used multiple times.

When Ilshat first implemented this data structure, he made a bug in the function compile\_set(). The bug reorders the binary digits of each element in the set in the same manner. Ilshat wants you to find the exact reordering of digits caused by the bug.

Formally, consider a sequence $p_0, \ldots, p_{n - 1}$ in which every number from $0$ to $n - 1$ appears exactly once. We call such a sequence a permutation. Consider an element of the set, whose digits in binary are $a_0, \ldots, a_{n - 1}$ (with $a_0$ being the most significant bit). When the function compile\_set() is called, this element is replaced by the element $a_{p_0}, a_{p_1}, \ldots, a_{p_{n - 1}}$.

The same permutation $p$ is used to reorder the digits of every element. Any permutation is possible, including the possibility that $p_i = i$ for each $0 \le i \le n - 1$.

For example, suppose that $n = 4$, $p = [2, 1, 3, 0]$, and you have inserted into the set integers whose binary representations are 0000, 1100 and 0111. Calling the function compile\_set changes these elements to 0000, 0101 and 1110, respectively.

Your task is to write a program that finds the permutation $p$ by interacting with the data structure. It should (in the following order):

1. choose a set of $n$-bit integers,
2. insert those integers into the data structure,
3. call the function compile\_set to trigger the bug,
4. check the presence of some elements in the modified set,
5. use that information to determine and return the permutation $p$.

Note that your program may call the function compile\_set only once.

In addition, there is a limit on the number of times your program calls the library functions. Namely, it may

* call add\_element at most $w$ times ($w$ is for "writes"),
* call check\_element at most $r$ times ($r$ is for "reads").

Implementation details

You should implement one function (method):

* int[] restore\_permutation(int n, int w, int r)
  + n: the number of bits in the binary representation of each element of the set (and also the length of $p$).
  + w: the maximum number of add\_element operations your program can perform.
  + r: the maximum number of check\_element operations your program can perform.
  + the function should return the restored permutation $p$.

Library functions

In order to interact with the data structure, your program should use the following three functions (methods):

* void add\_element(string x)

  This function adds the element described by x to the set.

  + x: a string of '0' and '1' characters giving the binary representation of an integer that should be added to the set. The length of x must be $n$.

* void compile\_set()

  This function must be called exactly once. Your program cannot call add\_element() after this call. Your program cannot call check\_element() before this call.

* boolean check\_element(string x)

  This function checks whether the element x is in the modified set.

  + x: a string of '0' and '1' characters giving the binary representation of the element that should be checked. The length of x must be $n$.
  + returns true if element x is in the modified set, and false otherwise.

Note that if your program violates any of the above restrictions, its grading outcome will be "Wrong Answer".

For all the strings, the first character gives the most significant bit of the corresponding integer.

The grader fixes the permutation $p$ before the function restore\_permutation is called.

Please use the provided template files for details of implementation in your programming language.

Input

The sample grader reads the input in the following format:

* line 1: integers $n$, $w$, $r$,
* line 2: $n$ integers giving the elements of $p$.

Output

One string — the return value of restore\_permutation.

Scoring

|  |  |  |
| --- | --- | --- |
| Subtask | Points | Additional Input Constraints |
| 1 | 20 | $n = 8$, $w = 256$, $r = 256$, $p_i \neq i$ for at most 2 indices $i$ ($0 \le i \leq n - 1$) |
| 2 | 18 | $n = 32$, $w = 320$, $r = 1024$ |
| 3 | 11 | $n = 32$, $w = 1024$, $r = 320$ |
| 4 | 21 | $n = 128$, $w = 1792$, $r = 1792$ |
| 5 | 30 | $n = 128$, $w = 896$, $r = 896$ |

Example

Input

```
4 16 16
2 1 3 0
```

Output

```
2 1 3 0
```

Note

Example

The grader makes the following function call:

* restore\_permutation(4, 16, 16). We have $n = 4$ and the program can do at most $16$ "writes" and $16$ "reads".

The program makes the following function calls:

* add\_element("0001")
* add\_element("0011")
* add\_element("0100")
* compile\_set()
* check\_element("0001") returns false
* check\_element("0010") returns true
* check\_element("0100") returns true
* check\_element("1000") returns false
* check\_element("0011") returns false
* check\_element("0101") returns false
* check\_element("1001") returns false
* check\_element("0110") returns false
* check\_element("1010") returns true
* check\_element("1100") returns false

Only one permutation is consistent with these values returned by check\_element(): the permutation $p = [2, 1, 3, 0]$. Thus, restore\_permutation should return [2, 1, 3, 0].

Interactor / grader files:
grader.cpp: #include <vector>
#include <cstdio>
#include <string>
#include <set>
#include <cstdlib>
#include <iostream>
#include "messy.h"

using namespace std;

namespace helper {

    set<string> set_;
    bool compiled = false;
    int n;
    vector<int> p;
    int w;
    int r;

    int read_int() {
        int x;
        cin >> x;
        return x;
    }

}

using namespace helper;


// A convenience function.
int get_p(int i) {
    int ret = p[i];
    return ret;
}

int main() {
    n = read_int();
    w = read_int();
    r = read_int();
    p = vector<int>(n);
    for (int i = 0; i < n; i++) {
        p[i] = read_int();
    }
    vector<int> answer = restore_permutation(n, w, r);
    
    if (answer.size() != n) {
        printf("WA\n");
        return 0;
    }


    printf("%d", answer[0]);

    for (int i = 1; i < n; i++) {
        printf(" %d", answer[i]);
    }
    printf("\n");
    return 0;
}

void wa() {
    printf("WA\n");
    exit(0);
}

bool check(const string& x) {
    if ((int)x.length() != n) {
        return false;
    }
    for (int i = 0; i < n; i++) {
        if (x[i] != '0' && x[i] != '1') {
            return false;
        }
    }
    return true;
}

void add_element(string x) {
    if (--w < 0 || compiled || !check(x)) {
        wa();
    }
    set_.insert(x);
}

bool check_element(string x) {
    if (--r < 0 || !compiled || !check(x)) {
        wa();
    }
    return set_.count(x);
}

void compile_set() {
    if (compiled) {
        wa();
    }
    compiled = true;
    set<string> compiledSet;
    string compiledElement = string(n, ' ');
    for (set<string>::iterator it = set_.begin(); it != set_.end(); it++) {
        string s = *it;
        for (int i = 0; i < n; i++) {
            compiledElement[i] = s[get_p(i)];
        }
        compiledSet.insert(compiledElement);
    }
    set_ = compiledSet;
}

messy.cpp: #include <vector>

#include "messy.h"

std::vector<int> restore_permutation(int n, int w, int r) {
    add_element("0");
    compile_set();
    check_element("0");
    return std::vector<int>();
}

messy.h: #pragma once

#include <vector>
#include <string>

void add_element(std::string x);
bool check_element(std::string x);
void compile_set();

std::vector<int> restore_permutation(int n, int w, int r);