Problem: cf1470_c
Difficulty: hard
Categories: search, math
Time limit: 1000 ms, memory limit: 256 MB

This is an interactive problem.

$n$ people sitting in a circle are trying to shuffle a deck of cards. The players are numbered from $1$ to $n$, so that players $i$ and $i+1$ are neighbours (as well as players $1$ and $n$). Each of them has exactly $k$ cards, where $k$ is even. The left neighbour of a player $i$ is player $i - 1$, and their right neighbour is player $i + 1$ (except for players $1$ and $n$, who are respective neighbours of each other).

Each turn the following happens: if a player has $x$ cards, they give $\lfloor x / 2 \rfloor$ to their neighbour on the left and $\lceil x / 2 \rceil$ cards to their neighbour on the right. This happens for all players simultaneously.

However, one player $p$ is the impostor and they just give all their cards to their neighbour on the right. You know the number of players $n$ and the number of cards $k$ each player has initially, but $p$ is unknown to you. Your task is to determine the value of $p$, by asking questions like "how many cards does player $q$ have?" for an index $q$ of your choice. After each question all players will make exactly one move and give their cards to their neighbours. You need to find the impostor by asking no more than $1000$ questions.

Input

The first line contains two integers $n$ and $k$ ($4 \le n \le 10^5$, $2 \le k \le 10^9$, $k$ is even) — the number of players and the number of cards.

Interaction

You can ask questions by printing "? $q$". The answer to this question is the number of cards player $q$ has now ($1 \le q \le n$). The shuffling process starts immediately after your first question, so the answer to the first one is always equal to $k$.

Once you have identified the impostor, you can output the answer by printing "! $p$", where $p$ is the player who is the impostor ($1 \le p \le n$). Then you have to terminate your program.

You have to find the impostor by asking no more than $1000$ questions.

After printing a query do not forget to output 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 documentation for other languages.

Example

Input

```
4 2

2

1

2

3

2
```

Output

```
? 1

? 1

? 2

? 3

? 4

! 2
```

Note

In the example the cards are transferred in the following way:

* $2$ $2$ $2$ $2$ — player $1$ has $2$ cards.
* $1$ $2$ $3$ $2$ — player $1$ has $1$ card.

After this turn the number of cards remains unchanged for each player.

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


static long long queries = 0;
static long long query_limit = 1000;
static const long long HARD_CAP = 25000; 


static int n;
static long long k;
static int p; 



static vector<long long> current_cards;
static vector<long long> next_cards;



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


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



void simulate() {
    
    fill(next_cards.begin(), next_cards.end(), 0LL);
    
    
    int imp = p - 1;

    for (int i = 0; i < n; i++) {
        long long val = current_cards[i];
        if (val == 0) continue;

        
        int left = (i - 1 + n) % n;
        int right = (i + 1) % n;

        if (i == imp) {
            
            next_cards[right] += val;
        } else {
            
            long long to_left = val / 2;
            long long to_right = (val + 1) / 2;
            next_cards[left] += to_left;
            next_cards[right] += to_right;
        }
    }
    
    
    swap(current_cards, next_cards);
}

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

    
    
    try {
        n = inf.readInt(4, 100000, "n");
        k = inf.readLong(2LL, 1000000000LL, "k");
        p = inf.readInt(1, n, "p");
    } catch (const std::exception& e) {
        finish(_fail, "Failed to read input (n k p): " + string(e.what()));
    }

    
    current_cards.assign(n, k);
    next_cards.resize(n);

    
    cout << n << " " << k << endl;

    
    while (true) {
        
        if (queries % 20 == 0) log_metrics();

        string token;
        if (!(cin >> token)) {
            
            finish(_wa, "Unexpected end of interaction (EOF)");
        }

        if (token == "!") {
            int ans_p;
            if (!(cin >> ans_p)) finish(_wa, "Expected number after '!'");
            if (ans_p == p) finish(_ok, "Correct impostor found");
            finish(_wa, format("Incorrect impostor: expected %d, found %d", p, ans_p));
        }
        if (token != "?") {
            finish(_wa, "Invalid token (expected '?' or '!')");
        }

        int q;
        if (!(cin >> q)) finish(_wa, "Expected number after '?'");
        if (q < 1 || q > n) {
            finish(_wa, format("Query index out of bounds: %d", q));
        }

        if (queries >= query_limit) {
            finish(_wa, "Query limit exceeded");
        }

            
            cout << current_cards[q - 1] << endl;

            
            queries++;
            if (queries > HARD_CAP) {
                finish(_pe, "Hard query limit exceeded");
            }

            
            simulate();
    }

    return 0;
}


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




static int queries_count = 0;
static const int query_limit = 1000;
static bool finished = false;


static int n;
static long long k;
static int p; 





static vector<long long> ref_sim;
static int sim_t = 0; 
static int min_active = 0; 
static int max_active = 0;




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

void finish(TResult verdict, const string& msg) {
    if (finished) return;
    finished = true;
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}






void advance_simulation() {
    
    
    int next_min = min_active - 1;
    int next_max = max_active + 1;
    
    
    
    int range_start = next_min;
    int range_end = next_max;
    
    if (range_end - range_start >= n - 1) {
        range_start = 0;
        range_end = n - 1;
    }

    
    
    vector<pair<int, long long>> updates;
    updates.reserve(range_end - range_start + 5);

    for (int i_unwrapped = range_start; i_unwrapped <= range_end; ++i_unwrapped) {
        
        int i = (i_unwrapped % n + n) % n;
        
        
        int L = (i - 1 + n) % n; 
        int R = (i + 1) % n;     
        
        long long from_L = 0;
        long long from_R = 0;
        
        
        
        if (L == 0) {
            
            
            if (i == 1) from_L = ref_sim[L];
            else from_L = 0;
        } else {
            
            
            long long v = ref_sim[L];
            from_L = (v + 1) / 2; 
        }
        
        
        
        if (R == 0) {
            
            from_R = 0;
        } else {
            
            
            long long v = ref_sim[R];
            from_R = v / 2;
        }
        
        long long new_val = from_L + from_R;
        
        
        if (new_val != ref_sim[i]) {
            updates.push_back({i, new_val});
        }
    }
    
    
    for (auto& p : updates) {
        ref_sim[p.first] = p.second;
    }
    
    
    min_active = next_min;
    max_active = next_max;
    sim_t++;
}




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

    
    
    n = inf.readInt(4, 100000, "n");
    k = inf.readLong(2LL, 1000000000LL, "k");
    p = inf.readInt(1, n, "p");
    
    
    cout << n << " " << k << endl;

    
    ref_sim.assign(n, k);
    min_active = 0;
    max_active = 0;

    
    int hard_cap = 20000; 
    
    while (true) {
        string token;
        if (!(cin >> token)) {
            finish(_pe, "Unexpected end of interaction");
        }
        
        if (token == "!") {
            int ans_p;
            if (!(cin >> ans_p)) finish(_wa, "Expected answer after '!'");
            if (ans_p == p) finish(_ok, "Correct");
            finish(_wa, format("Incorrect impostor: expected %d, found %d", p, ans_p));
            
        } else if (token == "?") {
            int q;
            if (!(cin >> q)) finish(_wa, "Expected index after '?'");
            
            if (q < 1 || q > n) finish(_wa, "Query index out of bounds");
            int q_idx = q - 1;      
            int p_idx = p - 1;      
            
            queries_count++;
            if (queries_count > hard_cap) {
                finish(_pe, "Too many queries (hard cap)");
            }
            if (queries_count > query_limit) {
                finish(_wa, "Query limit exceeded");
            }

            
            
            
            
            while (sim_t < queries_count - 1) {
                advance_simulation();
            }

            
            int rel = (q_idx - p_idx + n) % n;
            cout << ref_sim[rel] << endl;
            
            
            if (queries_count % 100 == 0) log_metrics();
            
        } else {
            finish(_wa, "Invalid token (expected '?' or '!')");
        }
    }
    
    return 0;
}


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");
    
    
    int opt_n = opt<int>("n", -1);
    long long opt_k = opt<long long>("k", -1);

    
    
    long long seed_val = -1;
    try {
        string s = argv[1];
        
        if (s.length() <= 18 && s.find_first_not_of("0123456789") == string::npos) {
            seed_val = stoll(s);
        }
    } catch (...) {}

    int n;
    long long k;
    int p;

    
    if (opt_n != -1) {
        n = opt_n;
    } else {
        if (seed_val == 1) n = 4;
        else if (seed_val == 2) n = 100000;
        else if (seed_val == 3) n = 100000;
        else {
            
            int t = rnd.next(1, 10);
            if (t == 1) n = rnd.next(4, 100);
            else if (t <= 4) n = rnd.next(101, 5000);
            else n = rnd.next(5001, 100000);
        }
    }

    
    if (opt_k != -1) {
        k = opt_k;
    } else {
        if (seed_val == 1) k = 2;
        else if (seed_val == 2) k = 1000000000LL;
        else if (seed_val == 3) k = 2;
        else {
            
            int t = rnd.next(1, 3);
            if (t == 1) k = rnd.next(2LL, 100LL);
            else if (t == 2) k = rnd.next(101LL, 100000LL);
            else k = rnd.next(100001LL, 1000000000LL);
        }
    }

    
    if (k < 2) k = 2;
    if (k > 1000000000LL) k = 1000000000LL;
    if (k % 2 != 0) {
        k++;
        if (k > 1000000000LL) k -= 2; 
    }

    
    
    if (opt_n == -1 && seed_val == 1) p = 1;
    else if (opt_n == -1 && seed_val == 2) p = n;
    else if (opt_n == -1 && seed_val == 3) p = n / 2;
    else {
        p = rnd.next(1, n);
    }
    
    
    if (mode == "non") {
        
        cout << n << " " << k << " " << p << endl;
    } else if (mode == "adp") {
        
        
        cout << n << " " << k << " " << p << endl;
    } else {
        quitf(_fail, "Unknown mode: %s", mode.c_str());
    }

    return 0;
}