Problem: cf2174_e1
Difficulty: medium
Categories: search, math
Time limit: 2000 ms, memory limit: 1024 MB

The two versions have different constraints on $k$, $c$. Solving one of the two versions does not necessarily solve the other. You may want to read both versions.

This is an interactive problem.

During the time of the Ottoman Empire, many scientists were at work. You decided to study their daily life and stumbled upon an interesting game that was popular at that time. One scientist would think of an integer $x$, such that $1 \leq x \leq c$. Another scientist would try to guess it. He could specify the base of the numeral system $2 \leq b \leq c$ and would receive as a response the sum of the digits of the number $x$ in base $b$, if $x \geq b$, or $-1$ if $x < b$.

You wrote a program that can think of a number $x$ and respond to the question. Now you want to play with it and learn to guess using $\leq k$ queries.

Input

The first line contains three integers $t$, $k$, $c$ ($1 \leq t \leq 10^4$, $\mathbf{k = 4}$, $\mathbf{c = 4 \cdot 10^{18}}$). You must guess the number from $1$ to $c$ $t$ times, using $\leq k$ queries. All $t$ games will be played consecutively and are independent.

Interaction

To make a query, you must output ? $b$ for some $2 \leq b \leq c$. Then you must read the response — an integer $a$ ($-1 \leq a < c$) — the response to the query.

To provide the guessed number, you must output ! $x$, where $1 \leq x \leq c$ — your answer. Then you must read the result $r$ ($0 \leq r \leq 1$). If $r = 0$ — your answer is incorrect, you must immediately terminate the program in this case. Otherwise, if $r = 1$ — your answer is correct, you can proceed to the next game or terminate the program if $t$ games have already been played.

It is guaranteed that the interactor is not adaptive. That is, all chosen numbers are fixed in advance and do not change during the games.

After printing each query do not forget to output the end of line and flush$^{\text{∗}}$ the output. Otherwise, you will get Idleness limit exceeded verdict.

If, at any interaction step, you read -2 instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.

$^{\text{∗}}$To flush, use:

* fflush(stdout) or cout.flush() in C++;
* sys.stdout.flush() in Python;
* see the documentation for other languages.

Example

Input

```
3 4 4000000000000000000

1

1

7

1

-1

1

-1

6

6

1
```

Output

```
? 10

? 1000

? 2

! 1000000

? 2

! 1

? 100

? 10

? 5

! 42
```

Note

In the first game, the hidden number is $1000000_{10} = 100_{1000} = 11110100001001000000_2$. The sums of digits are $1$, $1$, and $7$ in these bases.

In the second game, the hidden number is $1$. The query with $b = 2$ returns $-1$.

In the third game, the hidden number is $42_{10} = 132_{5}$.

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 long long hard_cap = 200000;



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


template <typename... Args>
void finish(TResult result, const char* fmt, Args... args) {
    log_metrics();
    quitf(result, fmt, args...);
}



long long get_digit_sum(long long x, long long b) {
    if (x < b) return -1;
    long long s = 0;
    long long temp = x;
    while (temp > 0) {
        s += temp % b;
        temp /= b;
    }
    return s;
}

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

    
    
    int t = inf.readInt();
    int k = inf.readInt();
    long long c = inf.readLong();

    
    
    
    query_limit = (long long)t * k;
    hard_cap = max(200000LL, 10LL * query_limit);

    
    log_metrics();

    
    cout << t << " " << k << " " << c << endl;

    
    for (int i = 0; i < t; i++) {
        long long hidden_x = inf.readLong();
        bool solved = false;

        while (!solved) {
            
            if (queries > hard_cap) {
                finish(_pe, "Hard query limit exceeded (>%lld)", hard_cap);
            }

            
            string token;
            if (!(cin >> token)) {
                finish(_pe, "Unexpected EOF from solution (test case %d)", i + 1);
            }

            if (token == "!") {
                
                long long guess;
                if (!(cin >> guess)) {
                    finish(_pe, "Unexpected EOF reading guess");
                }

                if (guess == hidden_x) {
                    cout << 1 << endl; 
                    solved = true;
                } else {
                    cout << 0 << endl; 
                    finish(_wa, "Incorrect guess on case %d: expected %lld, got %lld", i + 1, hidden_x, guess);
                }
            } else {
                
                long long b;
                if (token == "?") {
                    
                    if (!(cin >> b)) {
                        finish(_pe, "Unexpected EOF reading base b");
                    }
                } else {
                    
                    try {
                        size_t pos;
                        b = stoll(token, &pos);
                        if (pos != token.size()) throw exception();
                    } catch (...) {
                        finish(_pe, "Invalid token '%s' (expected '?', '!', or integer)", token.c_str());
                    }
                }

                
                queries++;
                
                
                if (queries % 50 == 0) log_metrics();

                
                if (b < 2 || b > c) {
                    finish(_wa, "Query base b=%lld out of range [2, %lld]", b, c);
                }

                
                long long ans = get_digit_sum(hidden_x, b);
                cout << ans << endl;
            }
        }
    }

    
    finish(_ok, "All %d test cases passed. Total queries: %lld", t, queries);
    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);

    
    string mode = opt<string>("mode", "non");
    int t = opt<int>("t", 100);
    
    
    long long c = 4000000000000000000LL;
    int k = 4;

    
    cout << t << " " << k << " " << c << "\n";

    if (mode == "adp") {
        
        
        
        for (int i = 0; i < t; ++i) {
            cout << "-1\n";
        }
    } else {
        
        
        vector<long long> cases;

        
        if (t >= 4) {
            cases.push_back(1);
            cases.push_back(c);
            cases.push_back(2);
            cases.push_back(c - 1);
        }

        while ((int)cases.size() < t) {
            int type = rnd.next(1, 100);
            long long x = 1;

            if (type <= 10) {
                
                x = rnd.next(1LL, 2000LL);
            } else if (type <= 20) {
                
                x = rnd.next(c - 2000LL, c);
            } else if (type <= 35) {
                
                
                int p = rnd.next(0, 61);
                x = (1LL << p);
            } else if (type <= 50) {
                
                
                int p = rnd.next(0, 18);
                long long p10 = 1;
                for (int j = 0; j < p; ++j) p10 *= 10;
                x = p10;
            } else if (type <= 60) {
                
                long long sq = 2000000000LL;
                x = rnd.next(sq - 100000LL, sq + 100000LL);
            } else {
                
                x = rnd.next(1LL, c);
            }

            
            if (type <= 50 && rnd.next(3) == 0) {
                x += rnd.next(-10, 10);
            }

            
            if (x < 1) x = 1;
            if (x > c) x = c;
            
            cases.push_back(x);
        }

        
        if (t < 4) {
            cases.clear();
            for (int i = 0; i < t; ++i) cases.push_back(rnd.next(1LL, c));
        }

        
        shuffle(cases.begin(), cases.end());

        for (long long val : cases) {
            cout << val << "\n";
        }
    }

    return 0;
}