Problem: cf1847_e
Difficulty: medium
Categories: math
Time limit: 2000 ms, memory limit: 256 MB

This is an interactive problem.

Made in Heaven is a rather curious Stand. Of course, it is (arguably) the strongest Stand in existence, but it is also an ardent puzzle enjoyer. For example, it gave Qtaro the following problem recently:

Made in Heaven has $n$ hidden integers $a_1, a_2, \dots, a_n$ ($3 \le n \le 5000$, $1 \le a_i \le 4$). Qtaro must determine all the $a_i$ by asking Made in Heaven some queries of the following form:

* In one query Qtaro is allowed to give Made in Heaven three distinct indexes $i$, $j$ and $k$ ($1 \leq i, j, k \leq n$).
* If $a_i, a_j, a_k$ form the sides of a non-degenerate triangle$^\dagger$, Made in Heaven will respond with the area of this triangle.
* Otherwise, Made in Heaven will respond with $0$.

By asking at most $5500$ such questions, Qtaro must either tell Made in Heaven all the values of the $a_i$, or report that it is not possible to uniquely determine them.

Unfortunately due to the universe reboot, Qtaro is not as smart as Jotaro. Please help Qtaro solve Made In Heaven's problem.

 —————————————————————— 

$^\dagger$ Three positive integers $a, b, c$ are said to form the sides of a non-degenerate triangle if and only if all of the following three inequalities hold:

* $a+b > c$,
* $b+c > a$,
* $c+a > b$.

Interaction

The interaction begins with reading $n$ ($3 \le n \le 5000$), the number of hidden integers.

To ask a question corresponding to the triple $(i, j, k)$ ($1 \leq i < j < k \leq n$), output "? $i$ $j$ $k$" without quotes. Afterward, you should read a single integer $s$.

* If $s = 0$, then $a_i$, $a_j$, and $a_k$ are not the sides of a non-degenerate triangle.
* Otherwise, $s = 16 \Delta^2$, where $\Delta$ is the area of the triangle. The area is provided in this format for your convenience so that you need only take integer input.

If the numbers $a_i$ cannot be uniquely determined print "! $-1$" without quotes. On the other hand, if you have determined all the values of $a_i$ print "! $a_1$ $a_2$ $\dots$ $a_n$" on a single line.

The interactor is non-adaptive. The hidden array $a_1, a_2, \dots, a_n$ is fixed beforehand and is not changed during the interaction process.

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

Examples

Input

```
3

63
```

Output

```
? 1 2 3

! -1
```

Input

```
6

0

0

0

63

15

135
```

Output

```
? 1 2 3

? 2 3 4

? 4 5 6

? 1 5 6

? 3 5 6

? 1 2 4

! 3 2 1 4 2 2
```

Input

```
15

3

3

3

3

3

0
```

Output

```
? 1 2 3

? 4 6 7

? 8 9 10

? 11 12 13

? 13 14 15

? 4 5 6

! -1
```

Input

```
15

3

15

0

3

3

3
```

Output

```
? 1 2 3

? 3 4 5

? 4 5 6

? 7 8 9

? 10 11 12

? 13 14 15

! 1 1 1 2 2 4 1 1 1 1 1 1 1 1 1 1
```

Input

```
10

3

48

3

48

63

0
```

Output

```
? 1 3 5

? 4 6 8

? 1 5 9

? 6 8 10

? 4 2 6

? 7 10 8

! 1 3 1 2 1 2 4 2 1 2
```

Note

In the first example, the interaction process happens as follows:

|  |  |  |
| --- | --- | --- |
| Stdin | Stdout | Explanation |
| 3 |  | Read $n = 3$. There are $3$ hidden integers |
|  | ? 1 2 3 | Ask for the area formed by $a_1$, $a_2$ and $a_3$ |
| 63 |  | Received $16\Delta^2 = 63$. So the area $\Delta = \sqrt{\frac{63}{16}} \approx 1.984313$ |
|  | ! -1 | Answer that there is no unique array satisfying the queries. |

From the area received, we can deduce that the numbers that forms the triangle are either ($4$, $4$, $1$) or ($3$, $2$, $2$) (in some order). As there are multiple arrays of numbers that satisfy the queries, a unique answer cannot be found.

In the second example, the interaction process happens as follows:

|  |  |  |  |
| --- | --- | --- | --- |
| Step | Stdin | Stdout | Explanation |
| 1 | 6 |  | Read $n = 6$. There are $6$ hidden integers |
| 2 |  | ? 1 2 3 | Ask for the area formed by $a_1$, $a_2$ and $a_3$ |
| 3 | 0 |  | Does not form a non-degenerate triangle |
| 4 |  | ? 2 3 4 | Ask for the area formed by $a_2$, $a_3$ and $a_4$ |
| 5 | 0 |  | Does not form a non-degenerate triangle |
| 6 |  | ? 4 5 6 | Ask for the area formed by $a_4$, $a_5$ and $a_6$ |
| 7 | 0 |  | Does not form a non-degenerate triangle |
|  |
| 8 |  | ? 1 5 6 | Ask for the area formed by $a_1$, $a_5$ and $a_6$ |
| 9 | 63 |  | Received $16\Delta^2 = 63$. So the area $\Delta = \sqrt{\frac{63}{16}} \approx 1.984313$ |
| 10 |  | ? 3 5 6 | Ask for the area formed by $a_3$, $a_5$ and $a_6$ |
| 11 | 15 |  | Received $16\Delta^2 = 15$. So the area $\Delta = \sqrt{\frac{15}{16}} \approx 0.968245$ |
| 12 |  | ? 1 2 4 | Ask for the area formed by $a_3$, $a_5$ and $a_6$ |
| 13 | 135 |  | Received $16\Delta^2 = 135$. So the area $\Delta = \sqrt{\frac{135}{16}} \approx 2.904738$ |
| 14 |  | ! 3 2 1 4 2 2 | A unique answer is found, which is $a = [3, 2, 1, 4, 2, 2]$. |

From steps $10$ and $11$, we can deduce that the the multiset $\left\{a_3, a_5, a_6\right\}$ must be $\left\{2, 2, 1\right\}$.

From steps $8$ and $9$, the multiset $\left\{a_1, a_5, a_6\right\}$ must be either $\left\{4, 4, 1\right\}$ or $\left\{3, 2, 2\right\}$.

As $\left\{a_3, a_5, a_6\right\}$ and $\left\{a_1, a_5, a_6\right\}$ share $a_5$ and $a_6$, we conclude that $a_5 = a_6 = 2$, as well as $a_1 = 3$, $a_3 = 1$.

From steps $6$ and $7$, we know that $a_5 = a_6 = 2$, and $a_4$, $a_5$ and $a_6$ cannot form a non-degenerate triangle, hence $a_4 = 4$.

With all the known information, only $a_2 = 2$ satisfies the queries made in steps $2$, $3$, $4$, $5$, $12$ and $13$.

In the third example, one array that satisfies the queries is $[1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]$.

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


static long long queries = 0;
static long long query_limit = 5500;
static int n;
static vector<int> a;


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_triangle(long long x, long long y, long long z) {
    if (x <= 0 || y <= 0 || z <= 0) return false;
    return (x + y > z) && (x + z > y) && (y + z > x);
}


long long get_val(long long x, long long y, long long z) {
    if (!is_triangle(x, y, z)) return 0;
    return (x + y + z) * (x + y - z) * (x - y + z) * (-x + y + z);
}




bool check_ambiguity() {
    array<int, 5> cnt{};
    for (int x : a) cnt[x]++;

    auto pair_exists = [&](const array<int, 5>& c, int u, int v) -> bool {
        if (u == v) return c[u] >= 2;
        return c[u] >= 1 && c[v] >= 1;
    };

    auto equal_on_present_pairs = [&](int x, int y, const array<int, 5>& rest) -> bool {
        for (int u = 1; u <= 4; u++) {
            for (int v = u; v <= 4; v++) {
                if (!pair_exists(rest, u, v)) continue;
                if (get_val(x, u, v) != get_val(y, u, v)) return false;
            }
        }
        return true;
    };

    
    
    for (int x = 1; x <= 4; x++) {
        if (cnt[x] == 0) continue;
        array<int, 5> rest = cnt;
        rest[x]--;
        for (int y = 1; y <= 4; y++) {
            if (y == x) continue;
            if (equal_on_present_pairs(x, y, rest)) return true;
        }
    }

    
    for (int x = 1; x <= 4; x++) {
        for (int y = x + 1; y <= 4; y++) {
            if (cnt[x] == 0 || cnt[y] == 0) continue;
            array<int, 5> rest = cnt;
            rest[x]--;
            rest[y]--;
            if (equal_on_present_pairs(x, y, rest)) return true;
        }
    }

    return false;
}

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

    
    n = inf.readInt(3, 5000, "n");
    a.resize(n);
    for (int i = 0; i < n; i++) {
        a[i] = inf.readInt(1, 4, "a_i");
    }

    
    cout << n << endl;

    string token;
    while (cin >> token) {
        if (token.empty()) continue;

        if (token[0] == '!') {
            
            string first_val_str;
            if (token.size() > 1) {
                first_val_str = token.substr(1);
            } else {
                if (!(cin >> first_val_str)) finish(_pe, "Unexpected EOF after '!'");
            }

            if (first_val_str == "-1") {
                
                if (check_ambiguity()) {
                    finish(_ok, "Correctly identified ambiguity.");
                } else {
                    finish(_wa, "Claimed ambiguity but the array is uniquely determined.");
                }
            } else {
                
                vector<int> user_a;
                try {
                    user_a.push_back(stoi(first_val_str));
                } catch (...) {
                    finish(_pe, "Invalid integer format in answer.");
                }

                for (int i = 1; i < n; i++) {
                    int val;
                    if (!(cin >> val)) finish(_pe, "Unexpected EOF reading answer array.");
                    user_a.push_back(val);
                }

                if (user_a == a) {
                    finish(_ok, "Correct array.");
                } else {
                    finish(_wa, "Incorrect array.");
                }
            }
            return 0; 
        } else {
            
            int i, j, k;
            
            
            
            if (token == "?") {
                if (!(cin >> i >> j >> k)) finish(_pe, "Invalid query format.");
            } else {
                
                try {
                    i = stoi(token);
                } catch (...) {
                    finish(_pe, "Expected '?' or integer query index.");
                }
                if (!(cin >> j >> k)) finish(_pe, "Invalid query format.");
            }

            queries++;
            
            if (queries % 100 == 0) log_metrics();
            if (queries > 200000) finish(_pe, "Hard query limit exceeded.");

            
            if (i < 1 || i > n || j < 1 || j > n || k < 1 || k > n) {
                finish(_pe, "Query indices out of range.");
            }
            if (i == j || j == k || i == k) {
                finish(_pe, "Query indices must be distinct.");
            }

            
            long long val = get_val(a[i-1], a[j-1], a[k-1]);
            cout << val << endl;
        }
    }

    finish(_pe, "Unexpected EOF.");
}


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 n_arg = opt<int>("n", -1);

    
    long long seed = 0;
    if (argc > 1) seed = atoll(argv[1]);

    int n;
    if (n_arg != -1) {
        n = n_arg;
    } else {
        
        if (seed == 1) n = 3;
        else if (seed == 2) n = 6;
        else if (seed == 3) n = 15;
        else if (seed == 4) n = 100;
        else {
            int p = rnd.next(100);
            if (p < 10) n = rnd.next(3, 20);      
            else if (p < 50) n = rnd.next(21, 500); 
            else n = rnd.next(4000, 5000);        
        }
    }

    
    if (mode == "adp") {
        cout << n << endl;
        return 0;
    }

    
    vector<int> a(n);

    
    if (seed == 1 && n == 3) {
        
        a = {1, 4, 4};
    } else if (seed == 2 && n == 6) {
        
        a = {3, 2, 1, 4, 2, 2};
    } else if (seed == 3 && n == 15) {
        
        fill(a.begin(), a.end(), 3);
    } else {
        
        int strat = rnd.next(5);
        if (strat == 0) {
            
            for (int i = 0; i < n; ++i) a[i] = rnd.next(1, 4);
        } else if (strat == 1) {
            
            for (int i = 0; i < n; ++i) {
                if (rnd.next(100) < 60) a[i] = (rnd.next(2) ? 1 : 4);
                else a[i] = rnd.next(1, 4);
            }
        } else if (strat == 2) {
            
            for (int i = 0; i < n; ++i) {
                if (rnd.next(100) < 60) a[i] = (rnd.next(2) ? 2 : 3);
                else a[i] = rnd.next(1, 4);
            }
        } else if (strat == 3) {
            
            int u = rnd.next(1, 4);
            int v = rnd.next(1, 4);
            while (u == v) v = rnd.next(1, 4);
            for (int i = 0; i < n; ++i) a[i] = (rnd.next(2) ? u : v);
        } else {
            
            int bs = rnd.next(5, max(6, n / 5));
            int cur = rnd.next(1, 4);
            for (int i = 0; i < n; ++i) {
                if (i > 0 && i % bs == 0) cur = rnd.next(1, 4);
                a[i] = cur;
            }
        }
    }

    
    cout << n << endl;
    for (int i = 0; i < n; ++i) {
        cout << a[i] << (i == n - 1 ? "" : " ");
    }
    cout << endl;

    return 0;
}