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
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>

// This doesn't work :((((((((((((((((((
// But I'm sending it anyways

typedef long long ll;

struct point_t
{
	ll x, y;
};

const point_t baseCase[] = {
	{ 1, 0 },
	{ 2, 1 },
	{ 3, 2 },
	{ 2, 3 },
	{ 1, 2 },
	{ 2, 1 },
	{ 3, 0 }
};

inline static ll bottomTime(ll n)
{
	// Mommy look, imma hacker!
	// 2 ^ (2 * n + 1) - 2
	return ((1LL << (n << 1LL)) - 1LL) << 1LL;
}

inline static ll bottomTimeOffset(ll n, ll offset)
{
	// Assuming we are goint up at first
	const ll length = (1LL << (n + 1LL)) - 1LL;
	ll offsetAddition;
	switch (offset) {
	case 0: offsetAddition = (length + 5) / 6; break;
	case 1: offsetAddition = (length + 1) / 6; break;
	case 2: offsetAddition = (length + 3) / 6; break;
	default:
		abort();
	}

	const ll ones = (n % 2 == 1) ? ((length + 5) / 6) : ((length + 1) / 6);

	return 2 * (offsetAddition - ones) + bottomTime(n);
}

inline static ll sideTime(ll n)
{
	// I can into bit magic, just a bit
	static const ll base = 3074457345618258602LL;
	return base & ((1LL << (n << 1LL)) - 1LL);
}

inline static ll sideTimeOffset(ll n, ll offset)
{
	return 42; // TODO
}

inline static ll topTime(ll n)
{
	// Actually the same as the side time
	return sideTime(n);
}

inline static ll innerTime(ll n)
{
	return bottomTime(n) - 2 * sideTime(n - 1);
}

point_t solveBottom(ll time, ll level, ll offset);
point_t solveSide(ll time, ll level, ll offset);
point_t solveInner(ll time, ll level);
point_t solveTop(ll time, ll level);
point_t solveSymmetricCorridor(ll time, ll level);
point_t solveAsymetricCorridor(ll time, ll level);
point_t solve(ll time, ll level);

point_t solveBottom(ll time, ll level, ll offset)
{
	if (time == 0) {
		return{ 1, 0 };
	}

	if (level == 1) {
		return baseCase[time];
	}

	const ll diameter = 1LL << (level + 1LL);

	// Check if we can use recurrence on the first half
	const ll sideTime = sideTimeOffset(level - 1, offset);
	if (time <= sideTime) {
		return solveSide(time, level - 1, offset);
	}

	// Check if we can go into the middle, biggest branch
	time -= sideTime;
	const ll middleTime = innerTime(level);
	if (time <= middleTime) {
		return solveInner(time, level);
	}

	// We need to go to the rightmost branch
	time -= middleTime;
	const ll offset2 = (diameter / 2 - ((diameter / 2 - 1) / 6) * 6 + 5) % 3; // WTF
	auto p = solveSide(sideTime - time, level - 1, offset2);
	return{ diameter - p.x, p.y };
}

point_t solveSide(ll time, ll level, ll offset)
{
	if (time == 0) {
		return{ 1, 0 };
	}

	if (time == 1) {
		return{ time + 1, time };
	}

	const ll diameter = 1LL << (level + 1LL);

	// Check if we can recur to the first half
	const ll bottomTime = bottomTimeOffset(level - 1, offset);
	if (time <= bottomTime) {
		return solveBottom(time, level - 1, offset);
	}

	// Check if we are in the small segment in the middle
	time -= bottomTime;
	if (time == 1) {
		return{ diameter / 2, 1 };
	}
	if (time == 2) {
		return{ diameter / 2 + 1, 0 };
	}

	time -= 2;
	// Go recursively to the right branch
	const ll offset2 = (diameter / 2 - ((diameter / 2 - 1) / 6) * 6 + 5) % 3; // WTF
	auto p = solveSide(time, level - 1, offset2);
	return{ diameter / 2 + p.x, p.y };
}

point_t solveInner(ll time, ll level)
{
	const ll diameter = 1LL << (level + 1LL);

	// It will be easier if we check for the mirror case
	const ll overallTime = innerTime(level);
	if (time > overallTime / 2) {
		auto p = solveInner(overallTime - time, level);
		return{ diameter / 2 - p.x, p.y };
	}

	if (time == 0) {
		return{ diameter - 1, 0 };
	}

	time--;

	// Check the lesser corridor
	const ll corridorTime = topTime(level - 1);
	if (time <= corridorTime) {
		auto p = solveTop(time, level - 1);
		return{ diameter / 2 - p.y, p.x };
	}

	// Check the dreaded assymetric corridor
	time -= corridorTime + 1;
	auto p = solveAsymetricCorridor(time, level - 1);
	return{ diameter / 2 - p.x, diameter / 2 + p.y };
}

point_t solveTop(ll time, ll level)
{
	const ll diameter = 1LL << (level + 1LL);

	// It will be easier if we check for the mirror case
	const ll overallTime = topTime(level);
	if (time > overallTime / 2) {
		auto p = solveTop(overallTime - time, level);
		return{ diameter / 2 - p.x, p.y };
	}

	if (level == 1) {
		return{ time + 1, -time };
	}

	// Check if we can use recurrence on the first half
	const ll leftTime = topTime(level);
	if (time <= leftTime) {
		return solveTop(time, level - 1);
	}

	// Go inside
	time -= leftTime + 1;
	const ll middleTime = 2LL * sideTime(level - 1);
	return solveSymmetricCorridor(time, level);
}

point_t solveSymmetricCorridor(ll time, ll level)
{
	const ll overallTime = sideTime(level);
	return solveSide(overallTime - time, level, 2);
}

point_t solveAsymetricCorridor(ll time, ll level)
{
	const ll diameter = 1LL << (level + 1LL);

	const ll offset1 = (level % 2 == 1) ? 4 : 0;
	const ll firstTime = sideTimeOffset(level, offset1);
	if (time <= firstTime) {
		return solveSide(time, level, offset1);
	}

	time -= firstTime;
	const ll offset2 = (level % 2 == 1) ? 0 : 4;
	const ll secondTime = bottomTimeOffset(level, offset2);
	auto p = solveBottom(time, level, offset2);
	return{ diameter - p.x, p.y };
}

point_t solve(ll time, ll level)
{
	const ll bTime = bottomTime(level);
	if (time == bTime + 1) {
		return{ 1LL << (level + 1), 1 };
	}

	const ll offset = (level % 2 == 1) ? 1 : 0;
	auto p = solveBottom(time, level, offset);
	return{ p.x, std::abs(p.y) };
}

int main()
{
	ll n;
	int t;
	scanf("%lld %d", &n, &t);

	while (t--> 0) {
		int x;
		scanf("%d", &x);
		auto p = solve(x, n);
		printf("%lld %lld\n", p.x, p.y);
	}
}