Problem: cf1867_e2
Difficulty: medium
Categories: bit
Time limit: 1000 ms, memory limit: 256 MB

This is the hard version of the problem. The only difference between the versions is the limit on the number of queries. In this version, you can make no more than 57 queries.

This is an interactive problem!

salyg1n has given you a positive integer $k$ and wants to play a game with you. He has chosen an array of $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$). You must print $a_1 \oplus a_2 \oplus \ldots \oplus a_n$, where $\oplus$ denotes the bitwise XOR operation. You can make queries of the following type:

* $?$ $i$: in response to this query, you will receive $a_i \oplus a_{i + 1} \oplus \ldots \oplus a_{i + k - 1}$. Also, after this query, the subarray $a_i, a_{i + 1}, \ldots, a_{i + k - 1}$ will be reversed, i.e., the chosen array $a$ will become: $a_1, a_2, \ldots a_{i - 1}, a_{i + k - 1}, a_{i + k - 2}, \ldots, a_{i + 1}, a_i, a_{i + k}, \ldots, a_n$.

You can make no more than $57$ queries to answer the problem.

Input

The first line contains a single integer $t$ ($1 \leq t \leq 1000$) – the number of test cases.

Interaction

The interaction between your program and the jury's program begins with reading two positive even integers $n$ and $k$ ($1 \leq k \leq n \leq k^2 \leq 2500$) – the length of the chosen array and the length of the query subarray, respectively.

To find the value of $a_i \oplus a_{i + 1} \oplus \ldots \oplus a_{i + k - 1}$, print the query in the format $?$ $i$ ($1 \leq i \leq n - k + 1$). Then read a single integer – the answer to your query.

You can make no more than $57$ queries. When you are ready to print the answer, output it in the format $!$ $x$. After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as one of the $57$ queries.

If your program makes more than $57$ queries for one set of input data, or makes an invalid query, then the response to the query will be -1. After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, it may receive any other 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. To do this, use:

* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see the documentation for other languages.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10000$. The interactor in this problem is not adaptive.

Example

Input

```
2
4 2

6

4

6 6

4
```

Output

```? 1? 3! 2? 1! 4
```

Note

In the first test case, the jury has chosen the array $a$ $=$ $[4, 2, 5, 1]$

In the second test case, the jury has chosen the array $a$ $=$ $[5, 7, 1, 3, 3, 7]$

Non-adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

static long long queries = 0;
static long long query_limit = 0;

static void log_metrics() {
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}

static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}

static bool read_token(string& tok) {
    if (!(cin >> tok)) return false;
    return true;
}

static void respond_invalid_query(const string& reason) {
    cout << -1 << endl;
    cout.flush();
    finish(_wa, reason);
}

int main(int argc, char** argv) {
    registerInteraction(argc, argv);
    atexit(log_metrics);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = inf.readInt(1, 1000, "t");
    cout << t << endl;
    cout.flush();

    for (int tc = 0; tc < t; ++tc) {
        int n = inf.readInt(1, 2500, "n");
        int k = inf.readInt(1, n, "k");

        vector<long long> a(n);
        long long expected = 0;
        for (int i = 0; i < n; ++i) {
            a[i] = inf.readLong(1, 1000000000LL, "a_i");
            expected ^= a[i];
        }

        cout << n << " " << k << endl;
        cout.flush();

        int q = 0;
        const int QLIM = 57;
        query_limit += QLIM;
        log_metrics();

        while (true) {
            string tok;
            if (!read_token(tok)) {
                finish(_wa, "Unexpected EOF from contestant");
            }

            if (tok == "?") {
                int i = 0;
                if (!(cin >> i)) {
                    respond_invalid_query("Bad query format");
                }
                if (q >= QLIM) {
                    respond_invalid_query("Too many queries");
                }
                if (i < 1 || i > n - k + 1) {
                    respond_invalid_query("Query index out of range");
                }

                long long x = 0;
                for (int j = 0; j < k; ++j) x ^= a[(i - 1) + j];
                reverse(a.begin() + (i - 1), a.begin() + (i - 1) + k);

                cout << x << endl;
                cout.flush();

                q++;
                queries++;
                if (queries % 200 == 0) log_metrics();
                continue;
            }

            if (!tok.empty() && tok[0] == '?') respond_invalid_query("Bad query format");

            if (tok == "!") {
                long long ans = 0;
                if (!(cin >> ans)) {
                    finish(_wa, "Bad answer format");
                }
                if (ans != expected) {
                    finish(_wa, format("Wrong answer: expected %lld, got %lld", expected, ans));
                }
                break;
            }

            if (!tok.empty() && tok[0] == '!') finish(_wa, "Bad answer token");

            finish(_wa, "Unexpected token (expected '?' or '!')");
        }
    }

    finish(_ok, "All test cases processed");
    return 0;
}


Adaptive interactor (C++):

Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);

    
    
    
    

    
    
    const int SUMN = 10000;

    struct Test {
        int n;
        int k;
    };

    vector<Test> tests;
    tests.reserve(200);
    int sum_n = 0;

    auto add_test = [&](int n, int k) -> bool {
        if ((int)tests.size() >= 1000) return false; 
        if (n % 2 != 0 || k % 2 != 0) return false;
        if (k < 2 || n < k) return false;
        if (k > 50) return false;             
        if (n > 2500) return false;           
        if (1LL * k * k < n) return false;    
        if (sum_n + n > SUMN) return false;   
        tests.push_back({n, k});
        sum_n += n;
        return true;
    };

    
    
    add_test(2500, 50);  
    add_test(2498, 50);  
    add_test(2302, 48);  

    
    
    int attempts = 0;
    while (sum_n < SUMN && (int)tests.size() < 1000 && attempts < 50000) {
        attempts++;
        int k = rnd.next(1, 25) * 2; 
        int r = rnd.next(0, k / 2) * 2; 
        if (r == k) r = k - 2; 

        
        int n;
        if (rnd.next(5) == 0) {
            
            int max_n = k * k;
            int wiggle = rnd.next(0, min(50, max_n / 4));
            n = max_n - 2 * wiggle;
            if (n < k) n = k;
            if (n % 2 != 0) n--;
            if (n < k) n = k;
        } else {
            
            n = k + r;
        }

        
        if (n < k) n = k;
        if (n % 2 != 0) n--;
        if (n < k) n = k;
        if (n > k * k) n = k * k;
        if (n > 2500) n = 2500;
        if (n % 2 != 0) n--;
        if (n < k) n = k;

        
        if (n < k || n > k * k) continue;

        
        int remaining = SUMN - sum_n;
        if (remaining < 2) break;
        if (n > remaining) {
            
            int kk = min(50, max(2, (int)std::sqrt((double)remaining) / 2 * 2));
            kk = max(2, min(kk, 50));
            if (kk % 2 != 0) kk--;
            if (kk < 2) break;
            int nn = kk;
            if (nn > remaining) break;
            if (!add_test(nn, kk)) break;
            continue;
        }

        
        if (!tests.empty() && tests.back().n == n && tests.back().k == k) continue;
        if (!add_test(n, k)) {
            
            int kk = 2;
            int nn = 2;
            if (!add_test(nn, kk)) break;
        }
    }

    cout << (int)tests.size() << endl;
    for (const auto& tc : tests) {
        int n = tc.n;
        int k = tc.k;
        cout << n << " " << k << endl;

        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            
            if (rnd.next(5) == 0) a[i] = rnd.next(1, 1024);
            else a[i] = rnd.next(1, 1000000000);
        }
        println(a);
    }

    return 0;
}