Official algorithmic problem description and constraints.
This is an interactive problem.
There is a permutation
β
π
of length π
.
Someone secretly chose two integers π,π
(1β€πβ€πβ€π
) and modified the permutation in the following way:
Let π
denote the resulting array obtained by modifying the permutation.
You are given an integer π
denoting the length of the permutation π
.
In one query, you are allowed to choose two integers π,π
(1β€πβ€πβ€π
) and ask for the sum of the subarray either of the original permutation π[πβ¦π]
or of the modified array π[πβ¦π]
. The answer to such a query will be the corresponding integer sum.
Your task is to find the pair (π,π)
that was chosen to obtain π
in no more than 40
queries.
β
A permutation of length π
is an array consisting of π
distinct integers from 1
to π
in any order. For example, [2,3,1,5,4]
is a permutation, but [1,2,2]
is not a permutation (the number 2
appears twice in the array), and [1,3,4]
is also not a permutation (π=3
, but the array contains 4
).
Input
The first line of input contains a single integer π‘
(1β€π‘β€10
4
) β the number of test cases.
Each test case contains a single integer π
(1β€πβ€2β 10
4
) β the length of the permutation.
It is guaranteed that the sum of π
over all the test cases does not exceed 2β 10
4
.
Interaction
The interaction for each test case begins by reading the integer π
.
You can ask two types of queries.
The permutation π
and the chosen integers π
, π
are fixed beforehand and can't be changed during the time of interaction.
You can output the final answer by printing "! π π
", where π
, π
denote the integers that were chosen to obtain π
. After printing the answer, your program should proceed to the next test case or terminate if there are no more.
You can ask no more than 40
queries per testcase. Printing the answer doesn't count as a query. If your program performs more than 40
queries for one test case or makes an invalid query, you may receive a Wrong Answer verdict.
After printing a query, do not forget to output the end of line and flush
β
the output. Otherwise, you will get Idleness limit exceeded.
Hacks
To make a hack, use the following test format.
The first line should contain a single integer π‘
(1β€π‘β€10
4
)
β the number of testcases.
The first line of each test case should contain a single integer π
(1β€πβ€2β 10
4
) β the length of the permutation π
.
The second line of each test case should contain π
integers π
π
(1β€π
π
β€π
) β denoting the permutation π
.
The third line of each test case should contain two space-separated integers π
, π
(1β€πβ€πβ€π
) β the chosen integers.
For example, the following is the hack format of the example test:
2 3 3 1 2 2 2 4 2 1 3 4 2 4
β
To flush, use:
Example
Input
Copy
2 3 4 5 4 8 8 9
Output
Copy
1 1 2 2 1 2 ! 2 2 1 2 4 2 1 3 2 3 4 ! 2 4
Note
For the first testcase, π=[3,1,2]
and π=2
, π=2
. Hence, the modified array π
will be equal to [3,2,2]
.
So, querying "π· π· πΈ
" gives π
1
+π
2
=3+1=4
. And querying "πΈ π· πΈ
" gives π
1
+π
2
=3+2=5
.
For the second testcase, π=[2,1,3,4]
and π=2
, π=4
.
Note that the queries shown in the sample test are only for demonstration purposes, and they may not correspond to any optimal solution.
Detailed video explanations, mathematical intuition, and clean C++ implementation code.