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
#define make_pair mp
#define emplace_back pb
#include <bits/stdc++.h>
#include "sonlib.h"
using namespace std;
mt19937 mt_rand(time(0));
const int N = 405;
int n, s;

int norm(int x) {
	x %= n;
	if(x < 0) x += n;
	return x + 1;
}

int nxt(int x) {
	if(MoveProbe(norm(x))) return x;
	return nxt(x + 1);
}

int findS1(int x, int nx) { //Jestem w nx, po zakończeniu w x.
	if(norm(nx - 1) == norm(x+1) || MoveProbe(norm(nx - 1)) || !MoveProbe(norm(x+1))) {
		if(norm(nx - 1) != norm(x + 1)) MoveProbe(norm(x));
		else {
			MoveProbe(norm(nx - 1));
			MoveProbe(norm(x));
		}
		return x + 1;
	}
	MoveProbe(norm(nx - 1));
	MoveProbe(norm(x));
	for(int i=x+2;i<nx;i++) {
	       	if(MoveProbe(norm(i)) || MoveProbe(norm(x + 1))) {
			MoveProbe(norm(i));
			MoveProbe(norm(x));
			return i;
		}
	}
}	

void solveS1() {
	vector<int> p, np;
	for(int i=0;i<n;i++) {
		int nx = nxt(i + 1);
		int sr = findS1(i, nx);
		for(int j=i;j<sr;j++) p.pb(j);
		for(int j=sr;j<nx;j++) np.pb(j);
		i = nxt(i + 1) - 1;
	}

	for(auto x : np) if(x < n) {
		MoveProbe(norm(x));
		Examine();
		MoveProbe(1);
	}
	int y = np[0];
	MoveProbe(norm(y));
	for(auto x : p) if(x < n) {
		MoveProbe(norm(x));
		Examine();
		MoveProbe(norm(y));
	}
}

int nxt_bez(int x, int y) {
	if(norm(x) == norm(y) || !MoveProbe(norm(x))) return nxt_bez(x + 1, y);
	return x;
}

int nxt_bez_odw(int x, int y) {
	if(norm(x) == norm(y) || !MoveProbe(norm(x))) return nxt_bez_odw(x - 1, y);
	return x;
}

int findS3(int x, int y) { //zaczynam w x, kończę w y
	for(int i=x+1;i<y;i++) if(norm(i) != norm(y) && norm(i) != norm(x)) if(MoveProbe(norm(i)) || MoveProbe(norm(y))) return i;
}

int findS3odw(int x, int y) { //zaczynam w x, kończę w y
	for(int i=x-1;i>y;i--) if(norm(x) != norm(i) && norm(i) != norm(y)) if(MoveProbe(norm(i)) || MoveProbe(norm(y))) return i;
}

int nxx[N];
bool odw[N];

void solveS3() {
	if(n == 4) {
		Examine();
		int x = nxt_bez(1, 0);
		Examine();
		for(int i=2;i<=4;i++) if(i != norm(x)) {
			MoveProbe(i);
			Examine();
			MoveProbe(norm(x));
		}
		return;
	}

	int prev = 0;
	bool prev_odw = 0; 
	int x = 0;
	for(;;) {
		Examine();
		int nx = nxt_bez(x + 1, x);
		if(norm(nx) == norm(prev)) {
			if(prev_odw) nxt_bez_odw(prev - 1, prev);
			else nxt_bez(prev + 1, prev);
			nx = nxt_bez_odw(x - 1, x);
			prev_odw = 1;
		}
		else prev_odw = 0;
		prev = x;
		nxx[norm(x)] = nx;
		odw[norm(x)] = prev_odw;
		x = nx;
		if(norm(x) == 1) break;
	}

	if(n % 2 == 1) return;

	x = 0;
	for(;;) {
		int y;
		if(odw[norm(x)]) y = findS3odw(x, nxx[norm(x)]);
		else y = findS3(x, nxx[norm(x)]);
		MoveProbe(norm(y));
		Examine();
		x = nxx[norm(x)];
		MoveProbe(norm(x));
		if(norm(x) == 1) break;
	}
}
int main()
{
	n = GetN();
	s = GetSubtask();

	if(s == 1) solveS1();
	if(s == 3) solveS3();

	return 0;
}