Problem: cf1291_f
Difficulty: medium
Categories: data structures
Time limit: 1000 ms, memory limit: 256 MB

This is the easy version of the problem. You can find the hard version in the Div. 1 contest. Both versions only differ in the number of times you can ask your friend to taste coffee.

This is an interactive problem.

You're considering moving to another city, where one of your friends already lives. There are $n$ cafés in this city, where $n$ is a power of two. The $i$-th café produces a single variety of coffee $a_i$.

As you're a coffee-lover, before deciding to move or not, you want to know the number $d$ of distinct varieties of coffees produced in this city.

You don't know the values $a_1, \ldots, a_n$. Fortunately, your friend has a memory of size $k$, where $k$ is a power of two.

Once per day, you can ask him to taste a cup of coffee produced by the café $c$, and he will tell you if he tasted a similar coffee during the last $k$ days.

You can also ask him to take a medication that will reset his memory. He will forget all previous cups of coffee tasted. You can reset his memory at most $30\ 000$ times.

More formally, the memory of your friend is a queue $S$. Doing a query on café $c$ will:

* Tell you if $a_c$ is in $S$;
* Add $a_c$ at the back of $S$;
* If $|S| > k$, pop the front element of $S$.

Doing a reset request will pop all elements out of $S$.

Your friend can taste at most $\dfrac{2n^2}{k}$ cups of coffee in total. Find the diversity $d$ (number of distinct values in the array $a$).

Note that asking your friend to reset his memory does not count towards the number of times you ask your friend to taste a cup of coffee.

In some test cases the behavior of the interactor is adaptive. It means that the array $a$ may be not fixed before the start of the interaction and may depend on your queries. It is guaranteed that at any moment of the interaction, there is at least one array $a$ consistent with all the answers given so far.

Input

The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 1024$, $k$ and $n$ are powers of two).

It is guaranteed that $\dfrac{2n^2}{k} \le 20\ 000$.

Interaction

You begin the interaction by reading $n$ and $k$.

* To ask your friend to taste a cup of coffee produced by the café $c$, in a separate line output

  ? $c$

  Where $c$ must satisfy $1 \le c \le n$. Don't forget to flush, to get the answer.

  In response, you will receive a single letter Y (yes) or N (no), telling you if variety $a_c$ is one of the last $k$ varieties of coffee in his memory.
* To reset the memory of your friend, in a separate line output the single letter R in upper case. You can do this operation at most $30\ 000$ times.
* When you determine the number $d$ of different coffee varieties, output

  ! $d$

In case your query is invalid, you asked more than $\frac{2n^2}{k}$ queries of type ? or you asked more than $30\ 000$ queries of type R, the program will print the letter E and will finish interaction. You will receive a Wrong Answer verdict. Make sure to exit immediately to avoid getting other verdicts.

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.

Examples

Input

```
4 2
N
N
Y
N
N
N
N
```

Output

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

Input

```
8 8
N
N
N
N
Y
Y
```

Output

```
? 2
? 6
? 4
? 5
? 2
? 5
! 6
```

Note

In the first example, the array is $a = [1, 4, 1, 3]$. The city produces $3$ different varieties of coffee ($1$, $3$ and $4$).

The successive varieties of coffee tasted by your friend are $1, 4, \textbf{1}, 3, 3, 1, 4$ (bold answers correspond to Y answers). Note that between the two ? 4 asks, there is a reset memory request R, so the answer to the second ? 4 ask is N. Had there been no reset memory request, the answer to the second ? 4 ask is Y.

In the second example, the array is $a = [1, 2, 3, 4, 5, 6, 6, 6]$. The city produces $6$ different varieties of coffee.

The successive varieties of coffee tasted by your friend are $2, 6, 4, 5, \textbf{2}, \textbf{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 void log_metrics() {
    try {
        tout << "queries=" << queries << '\n';
        tout << "query_limit=" << query_limit << '\n';
        tout.flush();
    } catch (...) {
        
    }
}

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

static inline string trim_copy(const string& s) {
    size_t l = 0, r = s.size();
    while (l < r && isspace((unsigned char)s[l])) ++l;
    while (r > l && isspace((unsigned char)s[r - 1])) --r;
    return s.substr(l, r - l);
}

static bool parse_int64_strict(const string& s, long long& out) {
    if (s.empty()) return false;
    int i = 0;
    bool neg = false;
    if (s[i] == '+' || s[i] == '-') {
        neg = (s[i] == '-');
        ++i;
        if (i == (int)s.size()) return false;
    }
    long long val = 0;
    for (; i < (int)s.size(); ++i) {
        if (!isdigit((unsigned char)s[i])) return false;
        int d = s[i] - '0';
        if (val > (LLONG_MAX - d) / 10) return false;
        val = val * 10 + d;
    }
    out = neg ? -val : val;
    return true;
}

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

    int n = 0, k = 0;
    vector<int> a;
    try {
        n = inf.readInt(1, 1024, "n");
        k = inf.readInt(1, n, "k");
        a.resize(n);
        for (int i = 0; i < n; i++) {
            a[i] = inf.readInt(-2000000000, 2000000000, ("a[" + to_string(i) + "]").c_str());
        }
    } catch (...) {
        query_limit = 0;
        log_metrics();
        quitf(_fail, "Invalid hidden input");
    }

    query_limit = (2LL * n * n) / k;
    if (query_limit < 0) query_limit = 0;

    
    log_metrics();

    
    cout << n << " " << k << '\n';
    cout.flush();

    unordered_set<int> distinct;
    distinct.reserve((size_t)n * 2);
    for (int x : a) distinct.insert(x);
    const long long correct_d = (long long)distinct.size();

    deque<int> mem;
    unordered_map<int, int> freq;
    freq.reserve((size_t)k * 2 + 16);

    long long resets = 0;
    const long long hard_cap = max(200000LL, 10LL * query_limit);

    auto send_E_and_WA = [&](const string& reason) -> void {
        cout << "E\n";
        cout.flush();
        finish(_wa, reason);
    };

    string line;
    while (true) {
        if (!std::getline(cin, line)) {
            finish(_pe, "Unexpected EOF from contestant");
        }
        line = trim_copy(line);
        if (line.empty()) {
            finish(_pe, "Empty command line");
        }

        stringstream ss(line);
        string cmd;
        ss >> cmd;
        if (!ss) finish(_pe, "Malformed command");

        auto has_extra_tokens = [&]() -> bool {
            string extra;
            if (ss >> extra) return true;
            return false;
        };

        if (cmd == "R") {
            if (has_extra_tokens()) finish(_pe, "Extra tokens after R");
            ++resets;
            if (resets > 30000) {
                send_E_and_WA("Too many resets");
            }
            mem.clear();
            freq.clear();
            continue;
        }

        if (cmd == "?" || (!cmd.empty() && cmd[0] == '?' && cmd.size() > 1)) {
            string ctok;
            if (cmd == "?") {
                if (!(ss >> ctok)) finish(_pe, "Missing cafe index after ?");
            } else {
                ctok = cmd.substr(1);
            }
            if (has_extra_tokens()) finish(_pe, "Extra tokens after query");

            long long c = 0;
            if (!parse_int64_strict(ctok, c)) finish(_pe, "Non-integer cafe index");
            if (c < 1 || c > n) {
                send_E_and_WA("Cafe index out of range");
            }

            ++queries;
            if (queries > hard_cap) {
                finish(_pe, "Hard cap exceeded");
            }

            int val = a[(int)c - 1];
            bool seen = false;
            auto it = freq.find(val);
            if (it != freq.end() && it->second > 0) seen = true;

            cout << (seen ? 'Y' : 'N') << '\n';
            cout.flush();

            mem.push_back(val);
            freq[val] += 1;
            if ((int)mem.size() > k) {
                int front = mem.front();
                mem.pop_front();
                auto it2 = freq.find(front);
                if (it2 != freq.end()) {
                    it2->second -= 1;
                    if (it2->second <= 0) freq.erase(it2);
                }
            }
            continue;
        }

        if (cmd == "!" || (!cmd.empty() && cmd[0] == '!' && cmd.size() > 1)) {
            string dtok;
            if (cmd == "!") {
                if (!(ss >> dtok)) finish(_pe, "Missing answer after !");
            } else {
                dtok = cmd.substr(1);
            }
            if (has_extra_tokens()) finish(_pe, "Extra tokens after answer");

            long long ans = 0;
            if (!parse_int64_strict(dtok, ans)) finish(_pe, "Non-integer answer");
            if (ans == correct_d) {
                finish(_ok, "OK");
            } else {
                finish(_wa, "Wrong answer");
            }
        }

        finish(_pe, "Unknown command");
    }
}


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 resets = 0;

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

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

static void protocol_error(const string& msg) {
    cout << "E" << endl;
    cout.flush();
    finish(_pe, msg);
}

static void wrong_answer(const string& msg) {
    finish(_wa, msg);
}

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

    int n = 0, k = 0;
    try {
        n = inf.readInt(1, 1024, "n");
        k = inf.readInt(1, 1024, "k");
    } catch (...) {
        finish(_pe, "failed to read n,k from hidden input");
    }

    if ((n & (n - 1)) != 0 || (k & (k - 1)) != 0 || k > n) {
        finish(_pe, "hidden input violates power-of-two constraints");
    }

    query_limit = (2LL * n * n) / k;

    
    log_metrics();

    vector<long long> rest;
    try {
        while (!inf.seekEof()) rest.push_back(inf.readLong());
    } catch (...) {
        finish(_pe, "failed while reading hidden tail");
    }

    vector<long long> a(n + 1, 0);
    long long true_d = 0;

    if ((int)rest.size() == n) {
        
        unordered_set<long long> uniq;
        uniq.reserve((size_t)n * 2);
        for (int i = 1; i <= n; i++) {
            a[i] = rest[i - 1];
            uniq.insert(a[i]);
        }
        true_d = (long long)uniq.size();
    } else if ((int)rest.size() == 1) {
        
        true_d = rest[0];
        if (true_d < 1 || true_d > n) finish(_pe, "hidden d out of range");
        for (int i = 1; i <= n; i++) a[i] = (long long)(((i - 1) % (int)true_d) + 1);
    } else {
        finish(_pe, "hidden input tail must be either n integers (array) or 1 integer (d)");
    }

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

    deque<long long> mem;
    unordered_map<long long, int> freq;
    freq.reserve((size_t)k * 2 + 16);

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

    while (true) {
        string cmd;
        if (!(cin >> cmd)) {
            protocol_error("unexpected EOF from contestant");
        }

        if (cmd == "?") {
            long long c;
            if (!(cin >> c)) protocol_error("missing index after '?'");
            if (c < 1 || c > n) protocol_error("index out of range");

            queries++;
            if (queries > hard_cap) protocol_error("too many queries (hard cap exceeded)");

            long long val = a[(int)c];
            bool inMem = false;
            auto it = freq.find(val);
            if (it != freq.end() && it->second > 0) inMem = true;

            cout << (inMem ? 'Y' : 'N') << endl;
            cout.flush();

            mem.push_back(val);
            freq[val]++;

            if ((int)mem.size() > k) {
                long long front = mem.front();
                mem.pop_front();
                auto it2 = freq.find(front);
                if (it2 != freq.end()) {
                    it2->second--;
                    if (it2->second <= 0) freq.erase(it2);
                }
            }
        } else if (cmd == "R") {
            resets++;
            if (resets > 30000) protocol_error("too many resets");
            mem.clear();
            freq.clear();
        } else if (cmd == "!") {
            long long ans;
            if (!(cin >> ans)) protocol_error("missing answer after '!'");
            if (ans == true_d) finish(_ok, "OK");
            wrong_answer("wrong diversity");
        } else {
            protocol_error("unknown command");
        }
    }
}


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

static bool isPow2(int x) {
    return x > 0 && (x & (x - 1)) == 0;
}

static int floorPow2(int x) {
    int p = 1;
    while ((p << 1) > 0 && (p << 1) <= x) p <<= 1;
    return p;
}

static int ceilPow2(int x) {
    if (x <= 1) return 1;
    int p = 1;
    while (p < x) p <<= 1;
    return p;
}

static void shuffleVec(vector<int> &v) {
    for (int i = (int)v.size() - 1; i >= 1; --i) {
        int j = rnd.next(0, i);
        swap(v[i], v[j]);
    }
}

static void pickNK(int seed, int &n, int &k) {
    
    vector<int> ns = {128, 256, 512, 1024};
    int nPick;
    {
        int roll = rnd.next(0, 99);
        if (roll < 70) nPick = 1024;
        else if (roll < 90) nPick = 512;
        else if (roll < 97) nPick = 256;
        else nPick = 128;
    }
    n = nPick;

    
    long long need = (2LL * n * n + 20000LL - 1) / 20000LL; 
    if (need < 1) need = 1;
    int minK = ceilPow2((int)need);
    if (minK > n) {
        
        for (int i = (int)ns.size() - 1; i >= 0; --i) {
            int candN = ns[i];
            long long need2 = (2LL * candN * candN + 20000LL - 1) / 20000LL;
            int minK2 = ceilPow2((int)max(1LL, need2));
            if (minK2 <= candN) {
                n = candN;
                minK = minK2;
                break;
            }
        }
        if (minK > n) {
            n = 1024;
            minK = 128;
        }
    }

    vector<int> ks;
    for (int kk = 1; kk <= n; kk <<= 1) {
        if (kk >= minK && 2LL * n * n <= 20000LL * kk) ks.push_back(kk);
    }
    if (ks.empty()) {
        
        k = n;
        return;
    }

    int roll = rnd.next(0, 99);
    if (roll < 75) k = ks.front();          
    else if (roll < 90) k = ks.back();      
    else k = ks[rnd.next(0, (int)ks.size() - 1)];
}

static int pickD(int n, int k, int seed) {
    
    int mod = seed % 7;
    if (mod == 0) return min(n, k + 1);              
    if (mod == 1) return n;                          
    if (mod == 2) return 1;                          
    if (mod == 3) return max(2, min(n, k));          
    int roll = rnd.next(0, 99);
    if (roll < 40) return min(n, k + 1);
    if (roll < 60) return min(n, 2 * k + 1);
    if (roll < 70) return 1;
    if (roll < 85) return n;
    return rnd.next(2, n);
}

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

    string mode = opt<string>("mode", "non");
    int nArg = opt<int>("n", -1);
    int kArg = opt<int>("k", -1);

    int seed = 1;
    if (argc >= 2) seed = atoi(argv[1]);

    int n = -1, k = -1, d = -1;
    vector<int> a;

    auto finalizeNK = [&]() {
        if (n < 1) n = 1;
        if (n > 1024) n = 1024;
        if (!isPow2(n)) n = floorPow2(n);

        if (k < 1) k = 1;
        if (k > n) k = n;
        if (!isPow2(k)) k = floorPow2(k);

        
        if (2LL * n * n > 20000LL * k) {
            long long need = (2LL * n * n + 20000LL - 1) / 20000LL;
            int minK = ceilPow2((int)max(1LL, need));
            if (minK <= n) k = max(k, minK);
            else {
                
                int nn = n;
                while (nn > 1 && 2LL * nn * nn > 20000LL * k) nn >>= 1;
                n = nn;
                if (k > n) k = n;
            }
        }
        
        if (k > n) k = n;
        if (!isPow2(n)) n = floorPow2(n);
        if (!isPow2(k)) k = floorPow2(k);
        if (n < 1) n = 1;
        if (k < 1) k = 1;
        if (k > n) k = n;
        
        if (2LL * n * n > 20000LL * k) {
            
            n = 1024;
            k = 128;
            if (2LL * n * n > 20000LL * k) {
                n = 512;
                k = 64;
            }
        }
    };

    if (seed == 1) {
        n = 1; k = 1; d = 1;
        a = {123456789};
    } else if (seed == 2) {
        n = 1024; k = 1024; d = 1024;
        a.resize(n);
        for (int i = 0; i < n; i++) a[i] = 1000000000 - i;
    } else if (seed == 3) {
        n = 1024; k = 128; d = 129;
        a.resize(n);
        for (int i = 0; i < n; i++) a[i] = 100000 + (i % d);
    } else {
        if (nArg != -1) n = nArg;
        if (kArg != -1) k = kArg;

        if (n == -1 || k == -1) {
            pickNK(seed, n, k);
        }
        finalizeNK();
        d = pickD(n, k, seed);

        if (mode == "non") {
            a.resize(n);
            if (d <= 1) {
                int val = 42424242;
                for (int i = 0; i < n; i++) a[i] = val;
            } else if (d >= n) {
                for (int i = 0; i < n; i++) a[i] = 1000000000 - i;
                
                for (int i = n - 1; i >= 1; --i) {
                    int j = rnd.next(0, i);
                    swap(a[i], a[j]);
                }
            } else {
                vector<int> base(d);
                for (int i = 0; i < d; i++) base[i] = 700000000 - i; 
                shuffleVec(base);

                int shift = rnd.next(0, d - 1);
                
                for (int i = 0; i < n; i++) {
                    a[i] = base[(i + shift) % d];
                }

                
                
                if (n >= 4 && rnd.next(0, 99) < 35) {
                    int cut = rnd.next(n / 4, 3 * n / 4);
                    rotate(a.begin(), a.begin() + cut, a.end());
                }
            }
        }
    }

    if (mode != "non" && mode != "adp") {
        mode = "non";
    }

    
    if (mode == "non") {
        if (n == -1 || k == -1) { n = 1024; k = 128; }
        finalizeNK();
        if ((int)a.size() != n) {
            a.assign(n, 1);
            d = 1;
        }
        cout << n << " " << k << "\n";
        for (int i = 0; i < n; i++) {
            if (i) cout << ' ';
            cout << a[i];
        }
        cout << "\n";
    } else {
        if (n == -1 || k == -1) { n = 1024; k = 128; }
        finalizeNK();
        if (d == -1) d = pickD(n, k, seed);
        d = max(1, min(d, n));
        cout << n << " " << k << "\n";
        cout << d << "\n";
    }
    return 0;
}