Problem: cf2108_d
Difficulty: easy
Categories: search, math
Time limit: 3000 ms, memory limit: 256 MB

This is an interactive problem.

You found the numbers $k$ and $n$ in the attic, but lost two arrays $A$ and $B$.

You remember that:

* $|A| + |B| = n$, the total length of the arrays is $n$.
* $|A| \geq k$ and $|B| \geq k$, the length of each array is at least $k$.
* The arrays consist only of numbers from $1$ to $k$.
* If you take any $k$ consecutive elements from array $A$, they will all be different. Also, if you take any $k$ consecutive elements from array $B$, they will all be different.

Fortunately, a kind spirit that settled in the attic found these arrays and concatenated them into an array $C$ of length $n$. That is, the elements of array $A$ were first written into array $C$, followed by the elements of array $B$.

You can ask the kind spirit up to $250$ questions. Each question contains an index $i$ ($1 \leq i \leq n$). In response, you will receive the $i$-th element of the concatenated array $C$.

You need to find the lengths of arrays $A$ and $B$, or report that it is impossible to determine them uniquely.

Input

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

The only line of each test case contains two integers $n$ and $k$ ($1 \leq k \leq 50$, $2 k \leq n \leq 10^{6}$).

Note that the sum of $n$ across test cases is not limited.

Interaction

The interaction for each test case begins with reading the integer $n$.

Then you can make up to $250$ queries.

To make a query, output a string in the format "? x" (without quotes) ($1 \leq x \leq n$). After each query, read an integer — the answer to your query.

If you make too many queries, you will receive a verdict of Wrong answer.

To report your answer, output a string in the format "! a b" (without quotes), where $a$ and $b$ are the lengths of arrays $A$ and $B$ that you found, respectively. The answer is not counted when counting the number of queries.

If it is impossible to determine the lengths of the arrays uniquely, output "! -1" (without quotes). Note that if you answer $-1$ while there is a sequence of at most $250$ queries that uniquely determines the lengths of arrays, you will get a Wrong answer verdict.

It is guaranteed that there are arrays $A$ and $B$ that do not contradict the statement, for which the interactor output is correct.

The interactor is not adaptive, which means that the answer is known before the participant makes queries and does not depend on the queries made by the participant.

If your program makes more than $250$ queries, your program should immediately terminate to receive the verdict Wrong answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

After outputting a query, do not forget to output a newline and flush the output buffer. Otherwise, you will receive a verdict of "IL" (Idleness limit exceeded). To flush the buffer, 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.

Example

Input

```
6
5 2

1

2

2

18 4

2

4

1

1

4

3 1

10 5

9 3

3

3

2

12 4

1

3

1

3

1

3
```

Output

```
? 1

? 2

? 3

! 2 3

? 9

? 13

? 10

? 14

? 6

! 9 9

! -1

! 5 5

? 3

? 6

? 9

! 6 3

? 1

? 2

? 5

? 6

? 9

? 10

! -1
```

Note

Consider the first example. We queried the first $3$ elements out of $5$. Now we know that the array $C$ looks like $[1, 2, 2, ?, ?]$. We know for sure that the third element is not from array $A$ — because according to the condition, any $k$ consecutive elements (in our case $k = 2$) in array $A$ are different. Thus, the third element is definitely located in array $B$. This means that the length of array $A$ is $2$, and the length of array $B$ is $3$.

The picture shows arrays from all test cases. The elements whose values were requested are marked in yellow.

 ![](https://espresso.codeforces.com/1981313348269cab100360e478a64874954e4b27.png)

Non-adaptive interactor (C++):
#include "testlib.h"
#include <csignal>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

static int queries = 0;
static long long query_limit = -1;

void log_queries() {
    tout << "queries=" << queries << "\n";
    tout << "query_limit=" << query_limit << "\n";
}

void quit_with(TResult verdict, const string& msg) {
    log_queries();
    quitf(verdict, "%s", msg.c_str());
}

static bool _all_distinct_window(const vector<int>& c, int start, int k) {
    vector<int> seen(k + 1, 0);
    for (int i = 0; i < k; i++) {
        int x = c[start + i];
        if (x < 1 || x > k) {
            return false;
        }
        if (seen[x]) {
            return false;
        }
        seen[x] = 1;
    }
    return true;
}

int main(int argc, char* argv[]) {
    registerInteraction(argc, argv);
    signal(SIGPIPE, SIG_IGN);
    atexit(log_queries);

    const int t = inf.readInt();
    cout << t << "\n" << flush;

    const int per_test_limit = 250;
    query_limit = 1LL * per_test_limit * t;

    for (int tc = 0; tc < t; tc++) {
        const int n = inf.readInt();
        const int k = inf.readInt();
        vector<int> c(n + 1);
        for (int i = 1; i <= n; i++) {
            c[i] = inf.readInt();
        }

        vector<int> bad_eq_prefix(max(0, n - k) + 2, 0);
        for (int i = 1; i + k <= n; i++) {
            bad_eq_prefix[i] = bad_eq_prefix[i - 1] + (c[i] != c[i + k]);
        }

        auto all_eq = [&](int l, int r) -> bool {
            if (l > r) {
                return true;
            }
            if (l < 1) {
                l = 1;
            }
            if (r > n - k) {
                r = n - k;
            }
            return bad_eq_prefix[r] - bad_eq_prefix[l - 1] == 0;
        };

        vector<char> window_distinct(n + 2, 0);
        if (k <= n) {
            vector<int> freq(k + 1, 0);
            int dup = 0;
            for (int i = 1; i <= k; i++) {
                int x = c[i];
                if (x < 1 || x > k) {
                    dup = 1;
                    break;
                }
                freq[x]++;
                if (freq[x] == 2) {
                    dup++;
                }
            }
            window_distinct[1] = (dup == 0);
            for (int s = 2; s + k - 1 <= n; s++) {
                int out = c[s - 1];
                if (1 <= out && out <= k) {
                    if (freq[out] == 2) {
                        dup--;
                    }
                    freq[out]--;
                } else {
                    dup = 1;
                }

                int in = c[s + k - 1];
                if (1 <= in && in <= k) {
                    freq[in]++;
                    if (freq[in] == 2) {
                        dup++;
                    }
                } else {
                    dup = 1;
                }
                window_distinct[s] = (dup == 0);
            }
        }

        vector<int> valid;
        if (n >= 2 * k && k >= 1 && window_distinct[1]) {
            for (int a = k; a <= n - k; a++) {
                if (!all_eq(1, a - k)) {
                    continue;
                }
                if (!window_distinct[a + 1]) {
                    continue;
                }
                if (!all_eq(a + 1, n - k)) {
                    continue;
                }
                valid.push_back(a);
            }
        }

        bool unique = (valid.size() == 1);
        int expected_a = unique ? valid[0] : -1;

        cout << n << " " << k << "\n" << flush;

        int used = 0;
        while (true) {
            string cmd;
            if (!(cin >> cmd)) {
                quit_with(_pe, "Unexpected EOF");
            }

            if (cmd == "?") {
                int x;
                if (!(cin >> x)) {
                    cout << "-1\n" << flush;
                    quit_with(_pe, "Invalid query");
                }
                used++;
                queries++;
                if (used > per_test_limit) {
                    cout << "-1\n" << flush;
                    quit_with(_pe, "Too many queries");
                }
                if (queries > query_limit) {
                    cout << "-1\n" << flush;
                    quit_with(_pe, "Too many queries (overall)");
                }
                if (x < 1 || x > n) {
                    cout << "-1\n" << flush;
                    quit_with(_pe, "Query index out of range");
                }
                cout << c[x] << "\n" << flush;
                continue;
            }

            if (!cmd.empty() && cmd[0] == '?' && cmd.size() > 1) {
                int x = -1;
                try {
                    x = stoi(cmd.substr(1));
                } catch (...) {
                    cout << "-1\n" << flush;
                    quit_with(_pe, "Invalid query token");
                }
                used++;
                queries++;
                if (used > per_test_limit) {
                    cout << "-1\n" << flush;
                    quit_with(_pe, "Too many queries");
                }
                if (queries > query_limit) {
                    cout << "-1\n" << flush;
                    quit_with(_pe, "Too many queries (overall)");
                }
                if (x < 1 || x > n) {
                    cout << "-1\n" << flush;
                    quit_with(_pe, "Query index out of range");
                }
                cout << c[x] << "\n" << flush;
                continue;
            }

            if (cmd == "!") {
                long long a;
                if (!(cin >> a)) {
                    quit_with(_pe, "Missing answer");
                }
                if (a == -1) {
                    if (unique) {
                        quit_with(_wa, "Answered -1 but unique split exists");
                    }
                    break;
                }
                long long b;
                if (!(cin >> b)) {
                    quit_with(_pe, "Missing b");
                }
                if (a + b != n) {
                    quit_with(_wa, "a+b != n");
                }
                if (!unique) {
                    quit_with(_wa, "Answered lengths but split is not unique");
                }
                if ((int)a != expected_a || (int)b != n - expected_a) {
                    quit_with(_wa, "Wrong lengths");
                }
                break;
            }

            quit_with(_pe, "Unknown command: " + cmd);
        }
    }

    quit_with(_ok, "All tests correct");
}


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);

    string mode = opt<string>("mode", "non");
    bool adaptive = (mode == "adp");

    
    long long seed_val = -1;
    try {
        if (argc > 1) seed_val = stoll(string(argv[1]));
    } catch (...) {}

    int t;
    
    if (seed_val >= 1 && seed_val <= 3) {
        t = 5; 
    } else {
        t = rnd.next(10, 50);
    }

    cout << t << "\n";

    for (int i = 0; i < t; ++i) {
        int n, k, la;
        vector<int> p, q;

        if (seed_val == 1) {
            
            
            k = rnd.next(1, 50);
            n = 2 * k;
            la = k;
            p = rnd.perm(k, 1);
            if (rnd.next(0, 1)) q = p; 
            else q = rnd.perm(k, 1);
        } else if (seed_val == 2) {
            
            
            k = rnd.next(2, 20);
            n = rnd.next(2 * k + 1, 100);
            la = rnd.next(k, n - k);
            p = rnd.perm(k, 1);
            q = p;
        } else if (seed_val == 3) {
            
            k = rnd.next(3, 10);
            n = rnd.next(2 * k, 60);
            la = rnd.next(k, n - k);
            p = rnd.perm(k, 1);
            q = p;
            int s = rnd.next(1, k - 1);
            rotate(q.begin(), q.begin() + s, q.end());
        } else {
            
            int type = rnd.next(1, 100);
            if (type <= 10) {
                
                k = rnd.next(1, 50);
                n = 2 * k;
            } else if (type <= 40) {
                
                k = rnd.next(1, 10);
                n = rnd.next(2 * k, 50);
            } else if (type <= 80) {
                
                k = rnd.next(10, 30);
                n = rnd.next(2 * k, 500);
            } else {
                
                k = rnd.next(30, 50);
                n = rnd.next(2 * k, 2000);
            }

            la = rnd.next(k, n - k);
            p = rnd.perm(k, 1);
            
            int rel = rnd.next(1, 100);
            if (rel <= 10) {
                
                q = p;
            } else if (rel <= 30 && k > 1) {
                
                q = p;
                int s = rnd.next(1, k - 1);
                rotate(q.begin(), q.begin() + s, q.end());
            } else {
                
                q = rnd.perm(k, 1);
            }
        }

        if (adaptive) {
            cout << n << " " << k << "\n";
        } else {
            cout << n << " " << k << "\n";
            
            int lb = n - la;
            vector<int> c(n);
            
            for (int j = 0; j < la; ++j) {
                c[j] = p[j % k];
            }
            
            for (int j = 0; j < lb; ++j) {
                c[la + j] = q[j % k];
            }
            
            for (int j = 0; j < n; ++j) {
                cout << c[j] << (j == n - 1 ? "" : " ");
            }
            cout << "\n";
        }
    }

    return 0;
}