Problem: cf870_d
Difficulty: easy
Categories: bit
Time limit: 2000 ms, memory limit: 256 MB

D. Something with XOR Queries

This is an interactive problem.

Jury has hidden a permutation *p* of integers from 0 to *n* - 1. You know only the length *n*. Remind that in permutation all integers are distinct.

Let *b* be the inverse permutation for *p*, i.e. *p**b**i* = *i* for all *i*. The only thing you can do is to ask xor of elements *p**i* and *b**j*, printing two indices *i* and *j* (not necessarily distinct). As a result of the query with indices *i* and *j* you'll get the value![](https://espresso.codeforces.com/5dae53b8ed0e62c9af6c21130fc022b384e359ff.png), where![](https://espresso.codeforces.com/c4b7092c0f7ce6535f2574569c98f7b3c2fe2c5f.png) denotes the xor operation. You can find the description of xor operation in notes.

Note that some permutations can remain indistinguishable from the hidden one, even if you make all possible *n*2 queries. You have to compute the number of permutations indistinguishable from the hidden one, and print one of such permutations, making no more than 2*n* queries.

The hidden permutation does not depend on your queries.

Input

The first line contains single integer *n* (1 ≤ *n* ≤ 5000) — the length of the hidden permutation. You should read this integer first.

Output

When your program is ready to print the answer, print three lines.

In the first line print "!".

In the second line print single integer *answers*\_*cnt* — the number of permutations indistinguishable from the hidden one, including the hidden one.

In the third line print *n* integers *p*0, *p*1,..., *p**n* - 1 (0 ≤ *p**i* < *n*, all *p**i* should be distinct) — one of the permutations indistinguishable from the hidden one.

Your program should terminate after printing the answer.

Interaction

To ask about xor of two elements, print a string "? i j", where *i* and *j* — are integers from 0 to *n* - 1 — the index of the permutation element and the index of the inverse permutation element you want to know the xor-sum for. After that print a line break and make flush operation.

After printing the query your program should read single integer — the value of![](https://espresso.codeforces.com/5dae53b8ed0e62c9af6c21130fc022b384e359ff.png).

For a permutation of length *n* your program should make no more than 2*n* queries about xor-sum. Note that printing answer doesn't count as a query. Note that you can't ask more than 2*n* questions. If you ask more than 2*n* questions or at least one incorrect question, your solution will get "Wrong answer".

If at some moment your program reads -1 as an answer, it should immediately exit (for example, by calling exit(0)). You will get "Wrong answer" in this case, it means that you asked more than 2*n* questions, or asked an invalid question. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.

Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including for the final answer.

To flush you can use (just after printing line break):

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

Examples

Input

```
3  
0  
0  
3  
2  
3  
2
```

Output

```? 0 0? 1 1? 1 2? 0 2? 2 1? 2 0!  
1  
0 1 2
```

Input

```
4  
2  
3  
2  
0  
2  
3  
2  
0
```

Output

```? 0 1? 1 2? 2 3? 3 3? 3 2? 2 1? 1 0? 0 0!  
2  
3 1 2 0
```

Note

xor operation, or bitwise exclusive OR, is an operation performed over two integers, in which the *i*-th digit in binary representation of the result is equal to 1 if and only if exactly one of the two integers has the *i*-th digit in binary representation equal to 1. For more information, see here.

In the first example *p* = [0, 1, 2], thus *b* = [0, 1, 2], the values![](https://espresso.codeforces.com/5dae53b8ed0e62c9af6c21130fc022b384e359ff.png) are correct for the given *i*, *j*. There are no other permutations that give the same answers for the given queries.

The answers for the queries are:

*![](https://espresso.codeforces.com/b36cf4ccf629eaa3a225cda06a5697eb030eb4d7.png),
*![](https://espresso.codeforces.com/88e848d2e69ecfb3b8c8d63fdc8949c7cc1e9f28.png),
*![](https://espresso.codeforces.com/233e40a031e99efe33bdf68f4a383163a23e1e7b.png),
*![](https://espresso.codeforces.com/3ec81fca9911a6bde9c2c7cae6c189514105085f.png),
*![](https://espresso.codeforces.com/be5d5595e56564765e3db6b865de2219cdadadeb.png),
*![](https://espresso.codeforces.com/3355af6093e240986c338bf13071b479e2604ca9.png).

In the second example *p* = [3, 1, 2, 0], and *b* = [3, 1, 2, 0], the values![](https://espresso.codeforces.com/5dae53b8ed0e62c9af6c21130fc022b384e359ff.png) match for all pairs *i*, *j*. But there is one more suitable permutation *p* = [0, 2, 1, 3], *b* = [0, 2, 1, 3] that matches all *n*2 possible queries as well. All other permutations do not match even the shown queries.

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 int n;
static vector<int> p;
static vector<int> b;


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





bool is_valid_X(int X) {
    for (int k = 0; k < n; ++k) {
        
        
        int idx = b[k] ^ X;
        if (idx < 0 || idx >= n) return false;
        
        
        
        if ((p[idx] ^ X) != k) return false;
    }
    return true;
}

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

    
    n = inf.readInt();
    p.resize(n);
    b.resize(n);
    for (int i = 0; i < n; ++i) {
        p[i] = inf.readInt();
        
        if (p[i] < 0 || p[i] >= n) quitf(_fail, "Hidden permutation value out of range");
        b[p[i]] = i;
    }

    
    query_limit = 2LL * n;

    
    
    vector<int> valid_Xs;
    for (int X = 0; X < n; ++X) {
        if (is_valid_X(X)) {
            valid_Xs.push_back(X);
        }
    }
    int real_cnt = (int)valid_Xs.size();

    
    cout << n << endl;

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

    while (true) {
        string token;
        if (!(cin >> token)) {
            finish(_wa, "Unexpected end of output (expected query or answer)");
        }

        if (token == "!") {
            
            int user_cnt;
            if (!(cin >> user_cnt)) finish(_wa, "Expected integer count after '!'");

            vector<int> user_p(n);
            vector<bool> seen(n, false);
            for (int i = 0; i < n; ++i) {
                if (!(cin >> user_p[i])) finish(_wa, "Expected permutation element");
                if (user_p[i] < 0 || user_p[i] >= n) finish(_wa, "Permutation element out of bounds");
                if (seen[user_p[i]]) finish(_wa, "Duplicate element in permutation");
                seen[user_p[i]] = true;
            }

            
            if (user_cnt != real_cnt) {
                finish(_wa, format("Incorrect count: expected %d, got %d", real_cnt, user_cnt));
            }

            
            
            int X = user_p[0] ^ p[0];

            
            bool X_valid = false;
            for (int v : valid_Xs) {
                if (v == X) {
                    X_valid = true;
                    break;
                }
            }

            if (!X_valid) {
                finish(_wa, format("Permutation corresponds to invalid X=%d", X));
            }

            
            for (int i = 0; i < n; ++i) {
                if (user_p[i] != (p[i] ^ X)) {
                    finish(_wa, "Permutation inconsistent with derived X");
                }
            }

            finish(_ok, format("Correct! Count=%d, X=%d", real_cnt, X));
        }

        
        int i, j;
        if (token == "?") {
            if (!(cin >> i >> j)) finish(_wa, "Expected two integers after '?'");
        } else {
            
            try {
                size_t idx;
                i = stoi(token, &idx);
                if (idx != token.size()) finish(_wa, "Invalid query token (trailing chars)");
                if (!(cin >> j)) finish(_wa, "Expected second integer for query");
            } catch (...) {
                finish(_wa, "Invalid query format");
            }
        }

        queries++;
        
        
        if (queries > hard_cap) {
            finish(_pe, "Hard query limit exceeded (infinite loop protection)");
        }

        
        if (i < 0 || i >= n || j < 0 || j >= n) {
            finish(_wa, format("Query indices out of bounds: %d %d", i, j));
        }

        
        int ans = p[i] ^ b[j];
        cout << ans << endl;

        
        if (queries % 100 == 0) log_metrics();
    }

    return 0;
}


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

using namespace std;


static int n_val = 0;
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());
}


vector<int> p_hidden;
vector<int> b_hidden;


struct Query {
    int i, j, ans;
};
vector<Query> history_log;



long long count_solutions() {
    
    vector<vector<pair<int, int>>> adj_p(n_val); 
    vector<vector<pair<int, int>>> adj_b(n_val); 
    
    for (const auto& q : history_log) {
        adj_p[q.i].push_back({q.j, q.ans});
        adj_b[q.j].push_back({q.i, q.ans});
    }
    
    
    
    
    {
        int visited_nodes = 0;
        vector<bool> vis_p(n_val, false);
        vector<bool> vis_b(n_val, false);
        queue<pair<int, int>> q; 
        
        
        q.push({0, 0});
        vis_p[0] = true;
        visited_nodes++;
        
        while(!q.empty()){
            auto [type, u] = q.front(); q.pop();
            if (type == 0) { 
                for (auto& edge : adj_p[u]) {
                    int v = edge.first; 
                    if (!vis_b[v]) {
                        vis_b[v] = true;
                        visited_nodes++;
                        q.push({1, v});
                    }
                }
            } else { 
                for (auto& edge : adj_b[u]) {
                    int v = edge.first; 
                    if (!vis_p[v]) {
                        vis_p[v] = true;
                        visited_nodes++;
                        q.push({0, v});
                    }
                }
            }
        }
        
        
        if (visited_nodes < 2 * n_val) {
             return -1;
        }
    }
    
    
    
    long long cnt = 0;
    vector<int> cur_p(n_val);
    vector<int> cur_b(n_val);
    vector<int> q_bfs; q_bfs.reserve(2 * n_val);
    static vector<bool> seen_vals; 
    if ((int)seen_vals.size() != n_val) seen_vals.resize(n_val);
    
    for (int start_val = 0; start_val < n_val; start_val++) {
        fill(cur_p.begin(), cur_p.end(), -1);
        fill(cur_b.begin(), cur_b.end(), -1);
        
        bool possible = true;
        
        
        q_bfs.clear();
        cur_p[0] = start_val;
        q_bfs.push_back(0); 
        
        int head = 0;
        while(head < (int)q_bfs.size()) {
            int u = q_bfs[head++];
            
            if (u < n_val) { 
                int p_val = cur_p[u];
                for (auto& edge : adj_p[u]) {
                    int j = edge.first;
                    int target_val = p_val ^ edge.second;
                    if (cur_b[j] == -1) {
                        if (target_val < 0 || target_val >= n_val) { possible = false; break; }
                        cur_b[j] = target_val;
                        q_bfs.push_back(j + n_val);
                    } else {
                        if (cur_b[j] != target_val) { possible = false; break; }
                    }
                }
            } else { 
                int j = u - n_val;
                int b_val = cur_b[j];
                for (auto& edge : adj_b[j]) {
                    int i = edge.first;
                    int target_val = b_val ^ edge.second;
                    if (cur_p[i] == -1) {
                        if (target_val < 0 || target_val >= n_val) { possible = false; break; }
                        cur_p[i] = target_val;
                        q_bfs.push_back(i);
                    } else {
                        if (cur_p[i] != target_val) { possible = false; break; }
                    }
                }
            }
            if (!possible) break;
        }
        
        if (possible) {
            
            fill(seen_vals.begin(), seen_vals.end(), false);
            bool distinct = true;
            for (int x : cur_p) {
                if (x < 0) { distinct = false; break; } 
                if (seen_vals[x]) { distinct = false; break; }
                seen_vals[x] = true;
            }
            
            if (distinct) {
                
                bool inverse_ok = true;
                for (int j = 0; j < n_val; j++) {
                    if (cur_b[j] < 0 || cur_p[cur_b[j]] != j) { inverse_ok = false; break; }
                }
                if (inverse_ok) cnt++;
            }
        }
    }
    
    return cnt;
}

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

    
    n_val = inf.readInt();
    query_limit = 2LL * n_val;
    
    
    log_metrics();
    
    
    cout << n_val << endl;
    
    
    p_hidden.resize(n_val);
    bool has_fixed = false;
    
    
    if (!inf.seekEof()) {
        try {
             for(int i=0; i<n_val; ++i) {
                 p_hidden[i] = inf.readInt();
             }
             has_fixed = true;
        } catch (...) {
             has_fixed = false;
        }
    }
    
    if (!has_fixed) {
        
        
        mt19937 rng(n_val * 1337 + 42); 
        iota(p_hidden.begin(), p_hidden.end(), 0);
        shuffle(p_hidden.begin(), p_hidden.end(), rng);
    }
    
    
    b_hidden.resize(n_val);
    for(int i=0; i<n_val; ++i) b_hidden[p_hidden[i]] = i;
    
    
    string token;
    while (cin >> token) {
        if (token == "!") {
            
            long long user_cnt;
            if (!(cin >> user_cnt)) finish(_pe, "Expected answer count after '!'");
            
            vector<int> user_p(n_val);
            vector<bool> seen(n_val, false);
            for (int i=0; i<n_val; ++i) {
                if (!(cin >> user_p[i])) finish(_pe, "Expected permutation");
                if (user_p[i] < 0 || user_p[i] >= n_val) finish(_wa, "Permutation element out of range");
                if (seen[user_p[i]]) finish(_wa, "Permutation elements not distinct");
                seen[user_p[i]] = true;
            }
            
            
            vector<int> user_b(n_val);
            for(int i=0; i<n_val; ++i) user_b[user_p[i]] = i;
            
            for (const auto& q : history_log) {
                if ((user_p[q.i] ^ user_b[q.j]) != q.ans) {
                    finish(_wa, "Output permutation not consistent with query history");
                }
            }
            
            
            long long real_cnt = count_solutions();
            
            if (real_cnt == -1) {
                
                
                if (user_cnt < 1) finish(_wa, "Count must be at least 1 (solution exists)");
                finish(_ok, "Correct (Soft verify: Graph disconnected)");
            } else {
                if (user_cnt != real_cnt) {
                    finish(_wa, "Incorrect number of indistinguishable permutations. Expected " + to_string(real_cnt) + ", found " + to_string(user_cnt));
                }
                finish(_ok, "Correct");
            }
            
            break; 
        } else if (token == "?") {
            
            queries++;
            
            if (queries > max(200000LL, 10 * query_limit)) {
                finish(_pe, "Too many queries (hard cap)");
            }
            
            int i, j;
            if (!(cin >> i >> j)) finish(_pe, "Invalid query format");
            
            
            if (i < 0 || i >= n_val || j < 0 || j >= n_val) {
                cout << -1 << endl;
                finish(_wa, "Invalid query indices");
            }
            
            if (queries > query_limit) {
                cout << -1 << endl;
                finish(_wa, "Query limit exceeded");
            }
            
            int ans = p_hidden[i] ^ b_hidden[j];
            history_log.push_back({i, j, ans});
            
            cout << ans << endl;
            
            if (queries % 100 == 0) log_metrics();
        } else {
            finish(_pe, "Unexpected token " + token);
        }
    }
    
    
    finish(_pe, "Unexpected EOF");
    
    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 n_arg = opt<int>("n", 0);

    
    int n;
    vector<int> p;

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

    
    bool manual = (n_arg == 0 && seed >= 1 && seed <= 5);

    if (n_arg > 0) {
        n = n_arg;
        p.resize(n);
        iota(p.begin(), p.end(), 0);
        shuffle(p.begin(), p.end());
    } else if (manual) {
        if (seed == 1) {
            
            n = 3;
            p = {0, 1, 2};
        } else if (seed == 2) {
            
            n = 4;
            p = {3, 1, 2, 0};
        } else if (seed == 3) {
            
            n = 4;
            p = {0, 1, 2, 3};
        } else if (seed == 4) {
            
            n = 5000;
            p.resize(n);
            iota(p.begin(), p.end(), 0);
            shuffle(p.begin(), p.end());
        } else if (seed == 5) {
            
            n = 5000;
            p.resize(n);
            iota(p.begin(), p.end(), 0);
        }
    } else {
        
        n = rnd.next(1, 5000);
        p.resize(n);
        iota(p.begin(), p.end(), 0);

        
        if (rnd.next(100) >= 10) {
            shuffle(p.begin(), p.end());
        }
    }

    
    if (mode == "adp") {
        
        cout << n << endl;
    } else {
        
        cout << n << endl;
        for (int i = 0; i < n; ++i) {
            cout << p[i] << (i == n - 1 ? "" : " ");
        }
        cout << endl;
    }

    return 0;
}