There is a hidden permutation of the numbers .
You can ask questions in the form with . Note that and does not have to be distinct.
The evaluator responds with the most significant bit of the bitwise AND of and . Here, the most significant bit of a number is the exponent of the highest power of that appears when writing in base . For example, the most significant bit of is , since appears in the binary representation of . If the bitwise AND of and is , the evaluator responds with .
Task
Your task is to determine the hidden permutation of at most numbers by asking no more than questions.
Interaction protocol
Firstly, you need to include the definitions of the functions which you will use/implement in your program; use #include "find-the-permutation.h"
.
Please note, you don't need to write any main
function. You instead need to implement the following function:
vector<int> solve(int N);
This function will be called a single time by the jury throught the execution of the program.
This function will return the hidden permutation, indexed from 1 (the vector starts with index which is meaningless; the permutation is contained between indexes and ).
You can ask questions by using this function:
int ask(int i, int j);
where and describe the question .
For each question, if and are between and (inclusive) and the solution has not asked more than questions, you receive the answer.
Otherwise, you receive and the evaluator terminates the interaction immediately with an "Wrong answer" verdict.
Once found, you have to provide the hidden permutation to the jury by returning it in the previously described manner.
Constraints and clarifications
- The evaluator is not adaptive, meaning that in each test, the hidden permutation is fixed before your program asks any questions.
- Let denote the number of questions asked by your program in a test case. Then, your score for this test case is calculated in the following way:
- If you will get of the points for the test;
- If you will get of the points for the test;
- If you will get of the points for the test;
- If you will get of the points for the test;
- If you will get of the points for the test.
- A permutation of the numbers is a sequence of length consisting of the numbers , containing each number exactly once.
- The bitwise AND of two numbers and is the number that contains the common bits of and . For example, if and , then their bitwise AND will be .
Example
vector<int> solve(int N) // suppose N = 5
{
ask(1, 5); // 2
ask(2, 3); // 1
ask(4, 3); // -1
ask(4, 4); // 0
return vector<int>{-100, 4, 3, 2, 1, 5};
}
Explanation
Suppose that the hidden permutation is .
The first question asks the most significant bit of the numbers and , which is .
The second one asks about the hidden numbers and , for which the answer is .
The bitwise AND of numbers and is and thus, the answer for the third question will be .
Lastly, the answer to the fourth question is , because the bitwise AND of and is .
Note that this is just a sample interaction to showcase the process of asking questions, receiving answers, and guessing the hidden permutation. It does not necessarily represent an approach to finding the hidden permutation.