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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n)    fwd(i, 0, n)
#define all(X)       X.begin(), X.end()
#define sz(X)        int(size(X))
#define pb           push_back
#define eb           emplace_back
#define st           first
#define nd           second
using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;
using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) {
	*(int *)0 = 0;
});
#	define DTP(x, y)                                      \
		auto operator<<(auto &o, auto a)->decltype(y, o) { \
			o << "(";                                      \
			x;                                             \
			return o << ")";                               \
		}
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) {
	((cerr << x << ", "), ...) << '\n';
}
#	define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#	define deb(...) 0
#endif

const int COLS = 10;
const int ROWS = 10;
const int ANCHOR_COLS = 3;
const int PAYLOAD_COLS = COLS - ANCHOR_COLS;
array<int, 3> ILLEGAL_POPCOUNTS = {4, 5, 6};

const int BITS = 4;

bool is_legal(int val) {
	for (auto illegal_popcount : ILLEGAL_POPCOUNTS) {
		if (__builtin_popcount(val) == illegal_popcount) {
			return false;
		}
	}
	return true;
}

int has_bit(int val, int bit) {
	return (val & (1 << bit)) > 0;
}

const int MAX_VAL = (1 << ROWS) - 1;

struct Edge {
	int prev_state;
	int prev_val;
};

const int MASKS = (1 << BITS);

vi legal_vals;
vector<Edge> edges[MASKS][MAX_VAL + 1];
ll paths[PAYLOAD_COLS][MASKS][MAX_VAL + 1];
ll total_paths = 0;

vector<Edge> final_edges;

void compute_legal_vals() {
	rep(i, MAX_VAL + 1) {
		if (is_legal(i)) {
			legal_vals.push_back(i);
		}
	}
}

void compute_first_edges() {
	for (auto val : legal_vals) {
		int msk = (val % MASKS);
		paths[0][msk][val] = 1;
	}
}

void compute_middle_edges() {
	for (auto val : legal_vals) {
		for (auto prev_val : legal_vals) {
			if (prev_val > val) {
				break;
			}
			int msk_xor = (val & (MASKS - 1));
			rep(prev_msk, MASKS) {
				int cur_msk = (msk_xor ^ prev_msk);
				edges[cur_msk][val].push_back({prev_msk, prev_val});
			}
		}
	}
}

void compute_final_edges() {
	for (auto val : legal_vals) {
		final_edges.push_back({5, val});
	}
}

void compute_paths() {
	fwd(col, 1, PAYLOAD_COLS) {
		for (auto val : legal_vals) {
			rep(cur_msk, MASKS) {
				for (auto [prev_msk, prev_val] : edges[cur_msk][val]) {
					paths[col][cur_msk][val] +=
						paths[col - 1][prev_msk][prev_val];
				}
			}
		}
	}
	for (auto [prev_msk, prev_val] : final_edges) {
		total_paths += paths[PAYLOAD_COLS - 1][prev_msk][prev_val];
	}
}

void preprocess_paths() {
	compute_legal_vals();
	compute_first_edges();
	compute_middle_edges();
	compute_final_edges();
	compute_paths();
	// cerr << "total paths: " << total_paths << '\n';
}

typedef array<int, PAYLOAD_COLS> Payload;
typedef array<int, ANCHOR_COLS> Anchor;
typedef array<int, COLS> Matrix;

vector<vi> anchor_rows = {
	{0, 0, 1},
	{0, 0, 1},
	{0, 1, 1},
	{0, 1, 1},
	{0, 0, 0},
	{1, 0, 0},
	{0, 1, 0},
	{1, 1, 0},
	{1, 0, 1},
	{1, 1, 1}};

Anchor get_anchor() {
	Anchor anchor{};

	rep(i, ROWS) {
		const auto row = anchor_rows[i];
		for (int j = 0; j < ANCHOR_COLS; ++j) {
			anchor[j] |= (row[j] << i);
		}
	}

	return anchor;
}

ll paths2[2][PAYLOAD_COLS][MASKS][MAX_VAL + 1];
// normalized payload
ll payload_rank(const Payload &payload) {
	rep(i, 2) rep(j, PAYLOAD_COLS) rep(k, MASKS) rep(l, MAX_VAL + 1)
		paths2[i][j][k][l] = 0;
	for (auto val : legal_vals) {
		if (val == payload[0]) {
			paths2[1][0][val % MASKS][val] = 1;
		} else if (val < payload[0]) {
			paths2[0][0][val % MASKS][val] = 1;
		}
	}
	fwd(col, 1, PAYLOAD_COLS) {
		for (auto val : legal_vals) {
			rep(cur_msk, MASKS) {
				for (auto [prev_msk, prev_val] : edges[cur_msk][val]) {
					if (val < payload[col]) {
						paths2[0][col][cur_msk][val] +=

							paths2[0][col - 1][prev_msk][prev_val] +
							paths2[1][col - 1][prev_msk][prev_val];
					} else if (val == payload[col]) {
						paths2[0][col][cur_msk][val] +=
							paths2[0][col - 1][prev_msk][prev_val];
						paths2[1][col][cur_msk][val] +=
							paths2[1][col - 1][prev_msk][prev_val];
					} else {
						paths2[0][col][cur_msk][val] +=
							paths2[0][col - 1][prev_msk][prev_val];
					}
				}
			}
		}
	}
	ll rank = 0;
	for (auto [prev_msk, prev_val] : final_edges) {
		rank += paths2[0][PAYLOAD_COLS - 1][5][prev_val];
	}
	return rank;
}

Payload payload_unrank(ll rank) {
	vector<Edge> cur_edges = final_edges;
	int cur_msk = 5;
	Payload payload{};
	fill(all(payload), 0);
	int cur_val = 0;
	for (int col = PAYLOAD_COLS - 1; col >= 0; --col) {
		int xr = cur_val % MASKS;
		for (auto [edge_prev_msk, edge_prev_val] : cur_edges) {
			// if ((cur_msk ^ xr) != edge_prev_msk) {
			// 	continue;
			// }
			ll paths_count = paths[col][edge_prev_msk][edge_prev_val];
			if (paths_count < rank) {
				rank -= paths_count;
			} else {
				payload[col] = edge_prev_val;
				cur_edges = edges[edge_prev_msk][edge_prev_val];
				cur_msk = edge_prev_msk;
				cur_val = edge_prev_val;
				break;
			}
		}
	}
	return payload;
}

void debug_human_readable_matrix(Matrix matrix) {
	for (int i = 0; i < ROWS; ++i) {
		string row_string;
		for (int j = 0; j < COLS; ++j) {
			row_string += (matrix[j] & (1 << i)) ? '1' : '0';
		}
		cerr << row_string << '\n';
	}
}

void debug_human_readable_anchor(Anchor anchor) {
	for (int i = 0; i < ROWS; ++i) {
		string row_string;
		for (int j = 0; j < ANCHOR_COLS; ++j) {
			row_string += (anchor[j] & (1 << i)) ? '1' : '0';
		}
		cerr << row_string << '\n';
	}
}

void debug_human_readable_payload(Payload payload) {
	for (int i = 0; i < ROWS; ++i) {
		string row_string;
		for (int j = 0; j < PAYLOAD_COLS; ++j) {
			row_string += (payload[j] & (1 << i)) ? '1' : '0';
		}
		cerr << row_string << '\n';
	}
}

Payload extract_payload(Matrix matrix) {
	sort(all(matrix), [](int col_a, int col_b) {
		return is_legal(col_a) < is_legal(col_b);
	});

	Anchor perturbed_anchor{};
	rep(i, ANCHOR_COLS) {
		perturbed_anchor[i] = matrix[i];
	}
	sort(all(perturbed_anchor), [](int col_a, int col_b) {
		return __builtin_popcount(col_a) < __builtin_popcount(col_b);
	});
	assert(__builtin_popcount(perturbed_anchor[0]) == 4);
	assert(__builtin_popcount(perturbed_anchor[1]) == 5);
	assert(__builtin_popcount(perturbed_anchor[2]) == 6);

	vector<pii> row_splits(ROWS);
	rep(i, ROWS) {
		row_splits[i] = {0, 0};
		rep(j, ANCHOR_COLS) {
			row_splits[i].first |= ((perturbed_anchor[j] >> i) & 1) << j;
		}
		fwd(j, ANCHOR_COLS, COLS) {
			row_splits[i].second |= ((matrix[j] >> i) & 1) << (j - ANCHOR_COLS);
		}
	}
	sort(all(row_splits), [](pii a, pii b) {
		auto [a_anchor, a_payload] = a;
		auto [b_anchor, b_payload] = b;
		bool is_four_a = a_anchor == 4;
		bool is_four_b = b_anchor == 4;
		bool is_six_a = a_anchor == 6;
		bool is_six_b = b_anchor == 6;
		bool is_special_a = is_four_a || is_six_a;
		bool is_special_b = is_four_b || is_six_b;
		if (is_special_a != is_special_b) {
			return is_special_a > is_special_b;
		}
		if (!is_special_a) {
			return a_anchor < b_anchor;
		}
		if (is_four_a != is_four_b) {
			return is_four_a > is_four_b;
		}
		return (__builtin_popcount(a_payload) & 1) >
			   (__builtin_popcount(b_payload) & 1);
	});

	auto debug_rows_human_readable = [&](vector<pii> rowers) {
		for (auto [anchor_part, payload_part] : rowers) {
			string anchor_str, payload_str;
			rep(j, ANCHOR_COLS) {
				anchor_str += ((anchor_part >> j) & 1) ? '1' : '0';
			}
			rep(j, PAYLOAD_COLS) {
				payload_str += ((payload_part >> j) & 1) ? '1' : '0';
			}
			cerr << anchor_str << " | " << payload_str << '\n';
		}
	};

	auto payload_from_rows = [&](const vector<pii> &rows) {
		Payload payload{};
		rep(i, PAYLOAD_COLS) {
			payload[i] = 0;
			rep(j, ROWS) {
				payload[i] |= ((rows[j].second >> i) & 1) << j;
			}
		}

		sort(all(payload));
		debug_human_readable_payload(payload);
		return payload;
	};

	return payload_from_rows(row_splits);
}

Matrix combine_anchor_and_payload(Anchor anchor, Payload payload) {
	Matrix matrix{};
	rep(i, ANCHOR_COLS) {
		matrix[i] = anchor[i];
	}
	rep(i, PAYLOAD_COLS) {
		matrix[ANCHOR_COLS + i] = payload[i];
	}
	return matrix;
}

void print_matrix(const Matrix &matrix) {
	rep(i, ROWS) {
		string row_string;
		rep(j, COLS) {
			row_string += (matrix[j] & (1 << i)) ? '1' : '0';
		}
		cout << row_string << '\n';
	}
	cout.flush();
}

void encode() {
	ll k;
	cin >> k;
	Payload payload = payload_unrank(k);
	Anchor anchor = get_anchor();
	// debug_human_readable_anchor(anchor);
	Matrix matrix = combine_anchor_and_payload(anchor, payload);
	print_matrix(matrix);
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
Matrix read_matrix() {
	Matrix matrix{};
	vector<string> matrix_strings(ROWS);
	auto transpose_matrix_strings = [&]() {
		vector<string> transposed(COLS, string(ROWS, '0'));
		rep(i, ROWS) {
			rep(j, COLS) {
				transposed[j][i] = matrix_strings[i][j];
			}
		}
		return transposed;
	};
	rep(i, ROWS) {
		cin >> matrix_strings[i];
	}
	shuffle(all(matrix_strings), rng);
	matrix_strings = transpose_matrix_strings();
	shuffle(all(matrix_strings), rng);
	matrix_strings = transpose_matrix_strings();
	rep(i, ROWS) {
		string row_string = matrix_strings[i];
		rep(j, COLS) {
			if (row_string[j] == '1') {
				matrix[j] |= (1 << i);
			}
		}
	}
	return matrix;
}

void decode() {
	Matrix matrix = read_matrix();
	Payload payload = extract_payload(matrix);
	ll k = payload_rank(payload);
	cout << k << '\n';
	cout.flush();
}

int32_t main() {
	preprocess_paths();
	string whoami;
	cin >> whoami;
	bool encoder = (whoami == "Algosia");
	ll bound;
	cin >> bound;
	assert(bound <= total_paths);
	int tests;
	cin >> tests;
	rep(_, tests) {
		if (encoder) {
			encode();
		} else {
			decode();
		}
	}
#ifdef LOCF
	cout.flush();
	cerr << "- - - - - - - - -\n";
	(void)!system(
		"grep VmPeak /proc/$PPID/status | sed s/....kB/\' MB\'/1 >&2"); // 4x.kB
																		// ....kB
#endif
	return 0;
}