1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef set<int> SI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;

const int INF = 1000000001;
const LD EPS = 1e-9;
const int MOD = 1000000007;
const LL LLINF = 1000000000000000001;

#define FOR(i, b, e) for(int i = b; i <= e; i++)
#define FORD(i, b, e) for(int i = b; i >= e; i--)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define DPRINT(x) cout << #x << ": " << x << endl
#define BOOST ios_base::sync_with_stdio(false); cin.tie(0)

#define MP make_pair
#define PB push_back
#define ST first
#define ND second
#define GGL(x) "Case #" << x << ": "

// ゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴ


#include "sonlib.h"


mt19937 mt(5151435);

const int maxn = 442;


void solve3()
{
    int n = GetN();
    VI mov[maxn];

    int curr = 1;
    int prev = -1;

    if(n == 3)
    {
        FORD(i, 3, 1)
        {
            Examine();
            MoveProbe(i);
        }
    }
    if(n == 4)
    {
        VI a(n-1);
        iota(ALL(a), 2);
        bool fixed = false;
        while(!fixed)
        {
            shuffle(ALL(a), mt);
            FOR(i, 0, 2)
            {
                auto res = MoveProbe(a[i]);
                if(res == 1)
                {
                    swap(a[i], a[0]);
                    fixed = true;
                    break;
                }
            }
            if(!fixed)
                MoveProbe(1);
        }
        Examine();
        MoveProbe(a[1]);
        Examine();
        MoveProbe(1);
        Examine();
        MoveProbe(a[2]);
        Examine();
    }
    do
    {
        Examine();
        int pcurr = curr;
        mov[curr].resize(n);
        iota(ALL(mov[curr]), 1);
        while(pcurr == curr)
        {
            shuffle(ALL(mov[curr]), mt);
            auto it = mov[curr].begin();
            while(it != mov[curr].end())
            {
                int x = *it;
                //cout << "idk " << x << endl;
                if(x == curr || (x == prev))
                {
                    it = mov[curr].erase(it);
                    continue;
                }
                auto res = MoveProbe(x);
                it++;
                if(res == 1)
                {
                    mov[curr].erase(it, mov[curr].end());
                    pcurr = x;
                    break;
                }
            }
            if(curr == pcurr)
                MoveProbe(curr);
        }
        prev = curr;
        curr = pcurr;
    } while(curr != 1);

    while(true)
    {
        FOR(i, 0, SIZE(mov[curr])-2)
            MoveProbe(mov[curr][i]);
        Examine();
        MoveProbe(mov[curr].back());
        curr = mov[curr].back();
    }
}

void solve1()
{
    int n = GetN();

    VI Ag = {1};
    VI Bg(n-1); 
    iota(ALL(Bg), 2);
    int nothingc = 0;
    while(nothingc < 3)
    {
        bool found = false;
        auto it = Bg.begin();
        while(it != Bg.end())
        {
            auto res = MoveProbe(*it);
            if(res == 1)
            {
                Ag.PB(*it);
                Bg.erase(it);
                found = true;
                break;
            }
            it++;
        }
        if(found)
            nothingc = 0;
        else
        {
            nothingc++;
            reverse(ALL(Bg));
            MoveProbe(1);
        }
    }
    MoveProbe(Bg.front());
    for(auto i : Ag)
    {
        MoveProbe(i);
        Examine();
        MoveProbe(Bg.front());
    }
    MoveProbe(1);
    for(auto i : Bg)
    {
        MoveProbe(i);
        Examine();
        MoveProbe(1);
    }
}

int n;
VI notSeen;
VI stac;

int scan()
{
    // cout << "scan " << SIZE(notSeen) << endl;
    FOR(i, 0, SIZE(notSeen)-1)
    {
        auto result = MoveProbe(notSeen[i]);
        if(result == 1)
        {
            Examine();
            int r = notSeen[i];
            notSeen.erase(notSeen.begin() + i);
            return r;
        }
    }
    return -1;
}

void fixParity()
{
    FORD(i, SIZE(stac)-2, 0)
        MoveProbe(stac[i]);
    FOR(i, 1, 3)
        if(i != stac[0])
            MoveProbe(i);
    FOR(i, 0, SIZE(stac)-1)
        MoveProbe(stac[i]);
}

void scanAndRun()
{
    while(!stac.empty())
    {
        /*
        cout << "stack: ";
        for(auto i : stac)
            cout << i << ' ';
        cout << endl;
        */
        int x = scan();
        if(x == -1)
        {
            if(SIZE(stac) == 1)
            {
                FOR(i, 1, 3)
                    if(i != stac[0])
                        MoveProbe(i);
                MoveProbe(stac[0]);
                return;
            }
            stac.pop_back();
            MoveProbe(stac.back());
        }
        else
        {
            stac.PB(x);
        }
        fixParity();
    } 
}

void solve2()
{
    n = GetN();

    notSeen = VI(n-3);
    iota(ALL(notSeen), 4);

    for(auto i : {2, 3, 1})
    {
        // cout << i << endl;
        MoveProbe(i); 
        Examine();
        stac = {i};
        scanAndRun();
    }
}


int main(int argc, char ** argv)
{
    BOOST;

    if(GetSubtask() == 3)
        solve3();
    else if(GetSubtask() == 1)
        solve1();
    else 
        solve2();
}