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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include <bits/stdc++.h>

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

using namespace std;

#ifdef LOCAL
template<typename A, typename B>
auto&operator<<(auto&o,pair<A, B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<&","[!i++]<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

using u16 = uint16_t;
using u32 = unsigned;
using i64 = long long;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<i64, i64>;
using vi = vector<int>;
using vll = vector<i64>;

inline u32 GetBit(u32 mask, int bit) {
	return ((mask >> bit) & 1);
}
inline u32 SetBit(u32 mask, int bit) {
	return mask | (1U << bit);
}
void PrintMaskBit(ostream &os, u32 mask, int nbits) {
	for (int i = 0; i < nbits; ++i) {
		os << GetBit(mask, i);
	}
}
void PrintMaskTrit(ostream &os, u32 mask, int ntrits) {
	for (int i = 0; i < ntrits; ++i) {
		os << (mask % 3);
		mask /= 3;
	}
}

// const int N = 5;
// const int PWR3_N = 243;
const int N = 10;
const int PWR3_N = 59049;

const int NUM_WAYS_CUTOFF = 5'000;
const int MAX_ALLOW_OCCURS = 4;
const int NUM_SPLIT_PATTERNS = 1 << (N - 1);

const i64 RANDOM_SHIFT = 12211612983145995LL;
const i64 SHIFT_MOD = 30000000000000100LL;

i64 binom[N + 1][N + 1];
i64 fact[N + 1];
u16 trits_bits_add[PWR3_N][1 << N];
u16 trit_extract[PWR3_N][3];
u16 to_trit_base[1 << N];

// num_combinations[mask popcount][num masks][sum of masks mod 3]
i64 num_combinations[N + 1][N + 1][PWR3_N];

struct SplitTransition {
	int trits;
	int new_pattern;
};
// split_transitions[split_patterns] = vec of {legal trits, resulting new split pattern}
vector<SplitTransition> split_transitions[NUM_SPLIT_PATTERNS];

// reachability_paths[max mask popcount][used rows so far][split pattern]
i64 reachability_paths[N + 1][N + 1][NUM_SPLIT_PATTERNS];


void Preproc() {
	for (int r = 1; r <= N; ++r) {
		binom[r][0] = 1;
		for (int c = 1; c <= r; ++c) {
			binom[r][c] = binom[r - 1][c - 1] + binom[r - 1][c];
		}
	}

	fact[0] = 1;
	for (int r = 1; r <= N; ++r) {
		fact[r] = fact[r - 1] * r;
	}

	for (int tmask = 0; tmask < PWR3_N; ++tmask) {
		int tmask_copy = tmask;
		for (int b = 0; b < N; ++b) {
			const int trit = tmask_copy % 3;
			tmask_copy /= 3;
			trit_extract[tmask][trit] |= (1 << b);
		}
	}

	for (int bmask = 0; bmask < (1 << N); ++bmask) {
		int tmask = 0, coef = 1;
		for (int b = 0; b < N; ++b) {
			tmask += ((bmask >> b) & 1) * coef;
			coef *= 3;
		}
		to_trit_base[bmask] = tmask;
	}

	for (int tmask = 0; tmask < PWR3_N; ++tmask) {
		const int zero_bmask = trit_extract[tmask][0];
		const int one_bmask = trit_extract[tmask][1];
		const int two_bmask = trit_extract[tmask][2];

		for (int add_bmask = 0; add_bmask < (1 << N); ++add_bmask) {
			// const int new_zero = (two_bmask & add_bmask) | (zero_bmask & (~add_bmask));
			const int new_one = (zero_bmask & add_bmask) | (one_bmask & (~add_bmask));
			const int new_two = (one_bmask & add_bmask) | (two_bmask & (~add_bmask));
			const int new_tmask = to_trit_base[new_one] + to_trit_base[new_two] * 2;
			trits_bits_add[tmask][add_bmask] = new_tmask;
		}
	}

	for (int used_popcount = 0; used_popcount <= N; ++used_popcount) {
		vi bmasks;
		for (int bmask = 0; bmask < (1 << N); ++bmask) {
			if (__builtin_popcount(bmask) == used_popcount) {
				bmasks.push_back(bmask);
			}
		}

		num_combinations[used_popcount][0][0] = 1;
		for (int cnt = 0; cnt < MAX_ALLOW_OCCURS; ++cnt) {
			for (int prev_tmask = 0; prev_tmask < PWR3_N; ++prev_tmask) {
				for (int new_bmask : bmasks) {
					const int new_tmask = trits_bits_add[prev_tmask][new_bmask];
					num_combinations[used_popcount][cnt + 1][new_tmask] +=
						num_combinations[used_popcount][cnt][prev_tmask];
				}
			}
		}
	}

	for (int split_ptn = 0; split_ptn < NUM_SPLIT_PATTERNS; ++split_ptn) {
		for (int tmask = 0; tmask < PWR3_N; ++tmask) {
			// Within each block, trits in tmask must be sorted
			int new_split_ptn = split_ptn;
			bool ok = true;
			int prevt = 0;
			int copy_tmask = tmask;
			for (int b = 0; b < N; ++b) {
				const int curt = copy_tmask % 3;
				copy_tmask /= 3;
				if (b && !GetBit(split_ptn, b - 1) && curt < prevt) {
					ok = false;
					break;
				}
				if (b && curt != prevt) {
					new_split_ptn = SetBit(new_split_ptn, b - 1);
				}
				prevt = curt;
			}

			if (ok) {
				// cerr << "split_ptn="; PrintMaskBit(cerr, split_ptn, N - 1); cerr << ", ";
				// cerr << "trits="; PrintMaskTrit(cerr, tmask, N); cerr << " => ";
				// cerr << "new_ptn="; PrintMaskBit(cerr, new_split_ptn, N - 1); cerr << "\n";
				split_transitions[split_ptn].push_back({
					.trits = tmask,
					.new_pattern = new_split_ptn});
			}
		}
	}

	cerr << "start dp\n";

	// reachability_paths[N][N][NUM_SPLIT_PATTERNS - 1] = 1;
	// for (int popcnt = N - 1; popcnt >= 0; --popcnt) {
	// 	for (int prevtotal = 0; prevtotal <= N; ++prevtotal) {
	// 		for (int nexttotal = prevtotal; nexttotal <= N; ++nexttotal) {
	// 			const int blksize = nexttotal - prevtotal;
	// 			if (blksize > MAX_ALLOW_OCCURS) { continue; }
	// 			if (blksize > min(popcnt, N - popcnt)) { continue; } // heura

	// 			for (int prev_ptn = 0; prev_ptn < NUM_SPLIT_PATTERNS; ++prev_ptn) {
	// 				for (const auto &trans : split_transitions[prev_ptn]) {
	// 					const int tmask = trans.trits;
	// 					const i64 num_ways = num_combinations[popcnt][blksize][tmask] / fact[blksize];
	// 					if (!num_ways) { continue; }
	// 					if (num_ways > NUM_WAYS_CUTOFF) { continue; } // heura
	// 					const int next_ptn = trans.new_pattern;
	// 					reachability_paths[popcnt][prevtotal][prev_ptn] += (
	// 						num_ways * reachability_paths[popcnt + 1][nexttotal][next_ptn]);
	// 				}
	// 			}
	// 		}
	// 	}
	// }

	reachability_paths[N][N][NUM_SPLIT_PATTERNS - 1] = 1;
	for (int popcnt = N - 1; popcnt >= 0; --popcnt) {
		for (int blksize = 0; blksize <= N; ++blksize) {
			if (blksize > MAX_ALLOW_OCCURS) { continue; }
			if (blksize > min(popcnt, N - popcnt)) { continue; } // heura

			for (int prev_ptn = 0; prev_ptn < NUM_SPLIT_PATTERNS; ++prev_ptn) {
				for (const auto &trans : split_transitions[prev_ptn]) {
					const int tmask = trans.trits;
					const i64 num_ways = num_combinations[popcnt][blksize][tmask] / fact[blksize];
					if (!num_ways) { continue; }
					if (num_ways > NUM_WAYS_CUTOFF) { continue; } // heura
					const int next_ptn = trans.new_pattern;
					for (int prevtotal = 0; prevtotal <= N - blksize; ++prevtotal) {
						const int nexttotal = prevtotal + blksize;
						// TODO verify overflows??
						reachability_paths[popcnt][prevtotal][prev_ptn] += (
							num_ways * reachability_paths[popcnt + 1][nexttotal][next_ptn]);
					}

				}
			}
		}

		// for (int prevtotal = 0; prevtotal <= N; ++prevtotal) {
		// 	for (int prev_ptn = 0; prev_ptn < NUM_SPLIT_PATTERNS; ++prev_ptn) {
		// 		auto &x = reachability_paths[popcnt][prevtotal][prev_ptn];
		// 		if (x > 1e17) {
		// 			debug(popcnt, prevtotal, prev_ptn, x);
		// 			x = min<i64>(x, 1e17);
		// 		}
		// 		// assert(x <= 1e16);
		// 	}
		// }
	}

	cerr << reachability_paths[0][0][0] << "\n";
}

vector<vi> GetRowWays(int popcnt, int num_rows, int tmask) {
// vi GetRows(int popcnt, int num_rows, int tmask, int tup_idx) {
	debug(popcnt, num_rows, tmask); //, tup_idx);
	if (num_rows == 0) {
		assert(tmask == 0);
		// assert(tup_idx == 0);
		return {vi{}};
	}

	vi avail_bmasks;
	for (int bmask = 0; bmask < (1 << N); ++bmask) {
		if (__builtin_popcount(bmask) == popcnt) {
			avail_bmasks.push_back(bmask);
		}
	}

	vector<vi> result;
	// int num_found = 0;
	vi cur_rows;

	function<void(int, int)> Bt = [&](int bidx, int cur_tmask) {
		if (SZ(cur_rows) == num_rows - 1) {
			if (trit_extract[cur_tmask][1]) { return; }
			const int twos_cand = trit_extract[cur_tmask][2];
			if (__builtin_popcount(twos_cand) != popcnt) { return; }
			cur_rows.push_back(twos_cand);
			if (is_sorted(ALL(cur_rows))) {
				result.push_back(cur_rows);
			}
			cur_rows.pop_back();
			return;
		}

		while (bidx < SZ(avail_bmasks)) {
			const int bmask = avail_bmasks[bidx];
			cur_rows.push_back(bmask);
			Bt(bidx, trits_bits_add[cur_tmask][bmask]);
			// if (Bt(bidx, trits_bits_add[cur_tmask][bmask])) {
			// 	return true;
			// }
			cur_rows.pop_back();
			++bidx;
		}
		// return false;
	};

	const int rev_tmask = (
		to_trit_base[trit_extract[tmask][1]] * 2 + to_trit_base[trit_extract[tmask][2]]);
	// const bool ans = Bt(0, rev_tmask);
	Bt(0, rev_tmask);
	return result;
	// assert(SZ(ans));

	// return cur_rows;
}

vi MakeMatrix(i64 matrix_idx) {
	debug(matrix_idx);
	assert(matrix_idx < reachability_paths[0][0][0]);

	int cur_popcnt = 0;
	int cur_total = 0;
	int cur_ptn = 0;

	vi matrix;

	while (cur_popcnt < N) {
		assert(matrix_idx < reachability_paths[cur_popcnt][cur_total][cur_ptn]);

		bool found = false;
		int found_total;
		SplitTransition found_trans;
		i64 amount_skipped = 0;

		for (int nexttotal = cur_total; !found && nexttotal <= N; ++nexttotal) {
			const int blksize = nexttotal - cur_total;
			if (blksize > MAX_ALLOW_OCCURS) { continue; }
			if (blksize > min(cur_popcnt, N - cur_popcnt)) { continue; } // heura

			for (const auto &trans : split_transitions[cur_ptn]) {
				const int tmask = trans.trits;
				const i64 num_ways = num_combinations[cur_popcnt][blksize][tmask] / fact[blksize];
				if (!num_ways) { continue; }
				const int next_ptn = trans.new_pattern;
				const i64 paths_offered = num_ways * reachability_paths[cur_popcnt + 1][nexttotal][next_ptn];
				if (paths_offered <= matrix_idx) {
					// debug("encode skip", nexttotal, trans.trits, trans.new_pattern, paths_offered);
					matrix_idx -= paths_offered;
					amount_skipped += paths_offered;
				} else {
					found = true;
					found_total = nexttotal;
					found_trans = trans;
					break;
				}
			}
		}

		assert(found);

		debug(amount_skipped);

		const int blksize = found_total - cur_total;
		const int tmask = found_trans.trits;
		const i64 num_ways = num_combinations[cur_popcnt][blksize][tmask] / fact[blksize];
		debug(cur_popcnt, cur_ptn, blksize, tmask, matrix_idx, matrix_idx % num_ways);
#ifdef LOCAL
		PrintMaskTrit(cerr, tmask, N); cerr << "\n";
#endif
		const auto row_opts = GetRowWays(cur_popcnt, blksize, tmask);
		debug(num_ways, SZ(row_opts));
		assert(SZ(row_opts) >= num_ways);
		for (auto row : row_opts[matrix_idx % num_ways]) {
			matrix.push_back(row);
		}
		matrix_idx /= num_ways;

		++cur_popcnt;
		cur_total = found_total;
		cur_ptn = found_trans.new_pattern;
		debug(matrix_idx);
	}

#ifdef LOCAL
	for (auto row : matrix) {
		PrintMaskBit(cerr, row, N); cerr << "\n";
	}
#endif

	debug("Final idx", matrix_idx);
	return matrix;
}

mt19937 gen(time(NULL));

vi ShuffleMatrix(const vi &A) {
	vi ans(N);

	vi permcol(N), permrow(N);
	iota(ALL(permcol), 0);
	iota(ALL(permrow), 0);

	shuffle(ALL(permcol), gen);
	shuffle(ALL(permrow), gen);

	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			const int pi = permrow[i];
			const int pj = permcol[j];
			ans[i] |= ((A[pi] >> pj) & 1) << j;
		}
	}
	return ans;
}

i64 DecodeMatrix(vi A) {
	auto FixColumns = [&]() {
		vi trits_by_cnt(N);
		for (int row : A) {
			const int cnt = __builtin_popcount(row);
			assert(cnt < N);
			int &dest = trits_by_cnt[cnt];
			dest = trits_bits_add[dest][row];
		}

		vector<string> cnt_trit_matrix(N, string(N, '.'));
		for (int i = 0; i < N; ++i) {
			int val = trits_by_cnt[i];
			for (int j = 0; j < N; ++j) {
				cnt_trit_matrix[i][j] = '0' + val % 3;
				val /= 3;
			}
		}

		vi column_order(N);
		iota(ALL(column_order), 0);
		sort(ALL(column_order), [&](int lhs, int rhs) {
			for (int r = 0; r < N; ++r) {
				if (cnt_trit_matrix[r][lhs] != cnt_trit_matrix[r][rhs]) {
					return cnt_trit_matrix[r][lhs] < cnt_trit_matrix[r][rhs];
				}
			}
			assert(false);
		});
#ifdef LOCAL
		for (int i = 0; i < N; ++i) {
			PrintMaskTrit(cerr, trits_by_cnt[i], N); cerr << "\n";
		}
#endif
		debug(column_order);

		for (int &row : A) {
			int new_row = 0;
			for (int i = 0; i < N; ++i) {
				if (GetBit(row, column_order[i])) {
					new_row = SetBit(new_row, i);
				}
			}
			row = new_row;
		}
	};

	auto FixRows = [&]() {
		sort(ALL(A), [](int lhs, int rhs) {
			return pii{__builtin_popcount(lhs), lhs} < pii{__builtin_popcount(rhs), rhs};
		});
	};

	FixColumns();
	FixRows();
#ifdef LOCAL
	for (int i = 0; i < N; ++i) {
		PrintMaskBit(cerr, A[i], N); cerr << "\n";
	}
#endif


	int cur_total = 0;
	int cur_ptn = 0;

	vll amount_skipped(N);
	vll value_div(N);
	vll value_mod(N);

	for (int popcnt = 0; popcnt < N; ++popcnt) {
		int next_total = cur_total;
		while (next_total < N && __builtin_popcount(A[next_total]) == popcnt) {
			++next_total;
		}
		const int blksize = next_total - cur_total;

		int next_ptn = cur_ptn;
		vi col_sums(N);
		for (int r = cur_total; r < next_total; ++r) {
			for (int c = 0; c < N; ++c) {
				col_sums[c] += GetBit(A[r], c);
			}
		}
		for (int c = 0; c < N - 1; ++c) {
			if (col_sums[c] % 3 != col_sums[c + 1] % 3) {
				next_ptn = SetBit(next_ptn, c);
			}
		}

		int tmask = 0;
		{
			int coef = 1;
			for (int c = 0; c < N; ++c) {
				tmask += coef * (col_sums[c] % 3);
				coef *= 3;
			}
		}
		debug(tmask);

		debug(popcnt, blksize, next_ptn);
		bool found = false;

		for (int skipnexttotal = cur_total; !found && skipnexttotal <= N; ++skipnexttotal) {
			const int skipblksize = skipnexttotal - cur_total;
			if (skipblksize > MAX_ALLOW_OCCURS) { continue; }
			if (skipblksize > min(popcnt, N - popcnt)) { continue; } // heura

			for (const auto &trans : split_transitions[cur_ptn]) {
				const int skiptmask = trans.trits;
				const i64 num_ways = num_combinations[popcnt][skipblksize][skiptmask] / fact[skipblksize];
				if (!num_ways) { continue; }
				const int skipnext_ptn = trans.new_pattern;
				const i64 paths_offered = num_ways * reachability_paths[popcnt + 1][skipnexttotal][skipnext_ptn];
				if (skipnexttotal == next_total && skiptmask == tmask && skipnext_ptn == next_ptn) {
					found = true;
					break;
				} else {
					// debug("decode skip", skipnexttotal, trans.trits, trans.new_pattern, paths_offered);
					amount_skipped[popcnt] += paths_offered;
				}
			}
		}

		const auto row_opts = GetRowWays(popcnt, blksize, tmask);
		const i64 num_ways = num_combinations[popcnt][blksize][tmask] / fact[blksize];
		assert(SZ(row_opts) >= num_ways);
		const auto iter = find(ALL(row_opts), vi(A.begin() + cur_total, A.begin() + next_total));
		assert(iter != row_opts.end());
		const int iter_idx = iter - row_opts.begin();
		assert(iter_idx < num_ways);
		debug(iter_idx, num_ways);

		value_div[popcnt] = num_ways;
		value_mod[popcnt] = iter_idx;

		cur_total = next_total;
		cur_ptn = next_ptn;
	}

	i64 matrix_idx = 0;
	for (int popcnt = N - 1; popcnt >= 0; --popcnt) {
		debug(popcnt, amount_skipped[popcnt], value_div[popcnt], value_mod[popcnt]);
		matrix_idx *= value_div[popcnt];
		matrix_idx += value_mod[popcnt];
		matrix_idx += amount_skipped[popcnt];
		debug(popcnt, matrix_idx);
	}

	return matrix_idx;
}

void TestEncoder() {
	i64 val;
	cin >> val;
	val = (val + RANDOM_SHIFT) % SHIFT_MOD;

	vi X = MakeMatrix(val);
	assert(SZ(X) == N);
	for (auto row : X) {
		for (int c = 0; c < N; ++c) {
			cout << GetBit(row, c);
		}
		cout << "\n";
	}
	cout << flush;
}

void TestDecoder() {
	vi X(N);
	for (int r = 0; r < N; ++r) {
		string s;
		cin >> s;
		for (int c = 0; c < N; ++c) {
			if (s[c] == '1') {
				X[r] = SetBit(X[r], c);
			}
		}
	}

	i64 val = DecodeMatrix(X);
	val = (val + SHIFT_MOD - RANDOM_SHIFT) % SHIFT_MOD;
	cout << val << endl;
}

int main() {
	Preproc();

	string agent;
	cin >> agent;
	ll n, t;
	cin >> n >> t;

	for (int tidx = 0; tidx < t; ++tidx) {
		if (agent == "Algosia") {
			TestEncoder();
		} else {
			TestDecoder();
		}
	}
}