Problem: abc286_f
Difficulty: easy
Categories: math
Time limit: 2000 ms, memory limit: 1024 MB

### Problem Statement

This is an **interactive task**, where your and the judge's programs interact via Standard Input and Output.

You and the judge will follow the procedure below.
The procedure consists of phases $1$ and $2$; phase $1$ is immediately followed by phase $2$.

(Phase $1$)

* The judge decides an integer $N$ between $1$ and $10^9$ (inclusive), which is hidden.
* You print an integer $M$ between $1$ and $110$ (inclusive).
* You also print an integer sequence $A=(A_1,A_2,\ldots,A_M)$ of length $M$ such that $1 \leq A_i \leq M$ for all $i = 1, 2, \ldots, M$.

(Phase $2$)

* The judge gives you an integer sequence $B=(B_1,B_2,\ldots,B_M)$ of length $M$. Here, $B_i = f^N(i)$. $f(i)$ is defined by $f(i)=A_i$ for all integers $i$ between $1$ and $M$ (inclusive), and $f^N(i)$ is the integer resulting from replacing $i$ with $f(i)$ $N$ times.
* Based on the given $B$, you identify the integer $N$ that the judge has decided, and print $N$.

After the procedure above, terminate the program immediately to be judged correct.

### Constraints

* $N$ is an integer between $1$ and $10^9$ (inclusive).

### Input and Output

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

(Phase $1$)

* First, print an integer $M$ between $1$ and $110$ (inclusive). It must be followed by a newline.

```
$M$
```

* Then, print a sequence $A=(A_1,A_2,\ldots,A_M)$ of length $M$ consisting of integers between $1$ and $M$ (inclusive), with spaces in between. It must be followed by a newline.

```
$A_1$ $A_2$ $\ldots$ $A_M$
```

(Phase $2$)

* First, an integer sequence $B=(B_1,B_2,\ldots,B_M)$ of length $M$ is given from the input.

```
$B_1$ $B_2$ $\ldots$ $B_M$
```

* Find the integer $N$ and print it. It must be followed by a newline.

```
$N$
```

If you print something illegal, the judge prints `-1`. In that case, your submission is already considered incorrect. Since the judge program terminates at this point, it is desirable that your program terminates too.

### Notes

* **After each output, add a newline and then flush Standard Output. Otherwise, you may get a TLE verdict.**
* **If an invalid output is printed during the interaction, or if the program terminates halfway, the verdict will be indeterminate.**
* After you print the answer (or you receive `-1`), immediately terminate the program normally. Otherwise, the verdict will be indeterminate.
* Note that an excessive newline is also considered an invalid input.

### Sample Interaction

The following is a sample interaction with $N = 2$.

| Input | Output | Description |
| --- | --- | --- |
|  |  | The judge has decided that $N=2$. $N$ is hidden: it is not given as an input. |
|  | `4` | You print $M$. |
|  | `2 3 4 4` | You print $A=(2,3,4,4)$. |
|
| `3 4 4 4` |  | $f^2(1)=3,f^2(2)=4,f^2(3)=4$, and $f^2(4)=4$, so the judge gives $B=(3,4,4,4)$ to your program. |
|  | `2` | Based on $B$, you have identified that $N=2$. Print $N$ and terminate the program normally. |
|

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


















static long long queries = 0;
static long long query_limit = 1;

static int N_hidden;


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


static void invalid_move(const string& reason) {
    cout << "-1" << endl; 
    finish(_pe, "Invalid output: " + reason);
}


static bool parse_long(const string& s, long long& out) {
    if (s.empty()) return false;
    try {
        size_t idx;
        out = stoll(s, &idx);
        return idx == s.size();
    } catch (...) {
        return false;
    }
}

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

    
    N_hidden = inf.readInt(1, 1000000000, "N");

    
    string token;
    if (!(cin >> token)) {
        finish(_pe, "Empty output from solution");
    }

    bool interaction_phase = true;
    long long M_long = 0;

    
    
    
    
    if (token == "!") {
        
        interaction_phase = false;
    } else {
        if (token == "?") {
            
            if (!(cin >> token)) finish(_pe, "Expected M after '?'");
        }
        
        if (!parse_long(token, M_long)) {
            invalid_move("M is not a valid integer");
        }
    }

    if (interaction_phase) {
        
        if (M_long < 1 || M_long > 110) {
            invalid_move(format("M (%lld) out of range [1, 110]", M_long));
        }
        int M = (int)M_long;

        
        vector<int> A(M + 1);
        for (int i = 1; i <= M; ++i) {
            string t;
            if (!(cin >> t)) invalid_move(format("Expected A[%d]", i));
            
            long long val;
            if (!parse_long(t, val)) invalid_move(format("A[%d] not an integer", i));
            if (val < 1 || val > M) invalid_move(format("A[%d]=%lld out of range [1, %d]", i, val, M));
            A[i] = (int)val;
        }

        
        queries++;
        log_metrics();

        
        
        
        static int up[32][120]; 
        
        
        for (int i = 1; i <= M; ++i) up[0][i] = A[i];
        
        
        for (int k = 1; k < 31; ++k) {
            for (int i = 1; i <= M; ++i) {
                up[k][i] = up[k-1][up[k-1][i]];
            }
        }

        
        vector<int> B(M + 1);
        for (int i = 1; i <= M; ++i) {
            int curr = i;
            int tempN = N_hidden;
            for (int k = 0; k < 31; ++k) {
                if ((tempN >> k) & 1) {
                    curr = up[k][curr];
                }
            }
            B[i] = curr;
        }

        
        for (int i = 1; i <= M; ++i) {
            cout << B[i] << (i == M ? "" : " ");
        }
        cout << endl; 

        
        if (!(cin >> token)) finish(_pe, "No final answer provided");
        if (token == "!") {
            if (!(cin >> token)) finish(_pe, "No final answer after '!'");
        }
    } else {
        
        
        if (!(cin >> token)) finish(_pe, "No final answer after '!'");
    }

    
    long long ans;
    if (!parse_long(token, ans)) {
        finish(_pe, "Final answer is not a valid integer");
    }

    
    if (ans == N_hidden) {
        finish(_ok, format("Correct! N=%d", N_hidden));
    } else {
        finish(_wa, format("Wrong Answer. Expected N=%d, found %lld", N_hidden, ans));
    }

    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");

    
    long long seed_val = -1;
    if (argc > 1) {
        string s = argv[1];
        if (!s.empty() && all_of(s.begin(), s.end(), ::isdigit) && s.size() < 19) {
            try {
                seed_val = stoll(s);
            } catch (...) {
                seed_val = -1;
            }
        }
    }

    int n;
    
    if (seed_val == 1) {
        n = 1; 
    } else if (seed_val == 2) {
        n = 1000000000; 
    } else if (seed_val == 3) {
        n = 1000000000 - 1; 
    } else if (seed_val == 4) {
        n = 2; 
    } else if (seed_val == 5) {
        n = 3; 
    } else {
        
        n = rnd.next(1, 1000000000);
    }

    
    cout << n << endl;

    return 0;
}