#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
const long long N_MOD = 30000000000000001LL;
const long long MAX_CAPACITY = 4406846758545031168LL; // Exactly 8008 * 8008 * 2^36
// Binomial coefficient for combinations ranking
long long nCr(int n, int k) {
if (k < 0 || k > n) return 0;
long long res = 1;
for (int i = 1; i <= k; ++i) {
res = res * (n - i + 1) / i;
}
return res;
}
// Maps a strictly sorted array of 6 elements (0-15) to a rank (0 to 8007)
long long rank_combo(int combo[6]) {
long long rank = 0;
int last = -1;
for (int i = 0; i < 6; ++i) {
for (int v = last + 1; v < combo[i]; ++v) {
rank += nCr(15 - v, 5 - i);
}
last = combo[i];
}
return rank;
}
// Maps a rank (0 to 8007) to a strictly sorted array of 6 elements (0-15)
void unrank_combo(long long rank, int combo[6]) {
int last = -1;
for (int i = 0; i < 6; ++i) {
for (int v = last + 1; v <= 15; ++v) {
long long ways = nCr(15 - v, 5 - i);
if (rank < ways) {
combo[i] = v;
last = v;
break;
} else {
rank -= ways;
}
}
}
}
// Core Bajtek Decoder - Now strictly evaluates ALL possible core structures
long long decode(int M[10][10]) {
long long max_K = -1;
// Check all permutations of 4 rows that could form the Anchor
for(int r0 = 0; r0 < 10; ++r0) {
for(int r1 = 0; r1 < 10; ++r1) { if(r1 == r0) continue;
for(int r2 = 0; r2 < 10; ++r2) { if(r2 == r0 || r2 == r1) continue;
for(int r3 = 0; r3 < 10; ++r3) { if(r3 == r0 || r3 == r1 || r3 == r2) continue;
int masks[10];
for(int c = 0; c < 10; ++c) {
masks[c] = (M[r0][c] << 3) | (M[r1][c] << 2) | (M[r2][c] << 1) | M[r3][c];
}
// Find ALL combinations of 4 columns that match the asymmetric signatures
for(int c0 = 0; c0 < 10; ++c0) { if(masks[c0] != 15) continue;
for(int c1 = 0; c1 < 10; ++c1) { if(c1 == c0 || masks[c1] != 14) continue;
for(int c2 = 0; c2 < 10; ++c2) { if(c2 == c0 || c2 == c1 || masks[c2] != 12) continue;
for(int c3 = 0; c3 < 10; ++c3) { if(c3 == c0 || c3 == c1 || c3 == c2 || masks[c3] != 8) continue;
bool valid = true;
// Extract and validate the 6 row labels
int row_labels[6], row_idx[6];
int r_idx_cnt = 0;
for(int r = 0; r < 10; ++r) {
if(r == r0 || r == r1 || r == r2 || r == r3) continue;
row_labels[r_idx_cnt] = (M[r][c0] << 3) | (M[r][c1] << 2) | (M[r][c2] << 1) | M[r][c3];
row_idx[r_idx_cnt] = r;
r_idx_cnt++;
}
// Sort row labels while keeping track of original indices
for(int i = 0; i < 6; ++i) {
for(int j = i + 1; j < 6; ++j) {
if(row_labels[i] > row_labels[j]) {
swap(row_labels[i], row_labels[j]);
swap(row_idx[i], row_idx[j]);
} else if(row_labels[i] == row_labels[j]) {
valid = false; // Duplicates invalidate the core
}
}
}
if(!valid) continue;
// Extract and validate the 6 column labels
int col_labels[6], col_idx[6];
int c_idx_cnt = 0;
for(int c = 0; c < 10; ++c) {
if(c == c0 || c == c1 || c == c2 || c == c3) continue;
col_labels[c_idx_cnt] = masks[c];
col_idx[c_idx_cnt] = c;
c_idx_cnt++;
}
// Sort col labels while keeping track of original indices
for(int i = 0; i < 6; ++i) {
for(int j = i + 1; j < 6; ++j) {
if(col_labels[i] > col_labels[j]) {
swap(col_labels[i], col_labels[j]);
swap(col_idx[i], col_idx[j]);
} else if(col_labels[i] == col_labels[j]) {
valid = false;
}
}
}
if(!valid) continue;
// Reconstruct the 36-bit payload exactly as it was encoded
long long payload = 0;
for(int i = 0; i < 6; ++i) {
for(int j = 0; j < 6; ++j) {
long long bit = M[row_idx[i]][col_idx[j]];
payload |= (bit << (i * 6 + j));
}
}
// Calculate ranks and combine into K
long long rank_r = rank_combo(row_labels);
long long rank_c = rank_combo(col_labels);
long long K_found = rank_c * 8008LL * (1LL << 36) + rank_r * (1LL << 36) + payload;
if(K_found > max_K) max_K = K_found;
}}}}
}}}}
return max_K;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string role;
if (!(cin >> role)) return 0;
long long max_N;
int t;
cin >> max_N >> t;
if (role == "Algosia") {
while (t--) {
long long N;
cin >> N;
long long max_c = (MAX_CAPACITY - 1 - N) / N_MOD;
// Iterate downwards to map to the largest valid integer representation
// Ensures our correct matrix always dominates any random fake-core collisions
for (long long c = max_c; c >= 0; --c) {
long long K = N + c * N_MOD;
long long payload = K & ((1LL << 36) - 1);
long long K_div = K >> 36;
long long r_idx = K_div % 8008;
long long c_idx = K_div / 8008;
int r_combo[6], c_combo[6];
unrank_combo(r_idx, r_combo);
unrank_combo(c_idx, c_combo);
int M[10][10] = {0};
// 1. Insert Top-Left 4x4 Asymmetric Anchor
int core[4][4] = {{1,1,1,1}, {1,1,1,0}, {1,1,0,0}, {1,0,0,0}};
for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) M[i][j] = core[i][j];
// 2. Map row sorting labels into anchor columns
for (int i = 0; i < 6; ++i) {
int r = i + 4, label = r_combo[i];
M[r][0] = (label >> 3) & 1; M[r][1] = (label >> 2) & 1;
M[r][2] = (label >> 1) & 1; M[r][3] = label & 1;
}
// 3. Map column sorting labels into anchor rows
for (int j = 0; j < 6; ++j) {
int col = j + 4, label = c_combo[j];
M[0][col] = (label >> 3) & 1; M[1][col] = (label >> 2) & 1;
M[2][col] = (label >> 1) & 1; M[3][col] = label & 1;
}
// 4. Fill Matrix Payload Block
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 6; ++j) {
M[i + 4][j + 4] = (payload >> (i * 6 + j)) & 1;
}
}
// Local verification check.
// Since Bajtek runs identically on his permuted grid, finding the same K is foolproof.
if (decode(M) == K) {
for(int i = 0; i < 10; ++i) {
for(int j = 0; j < 10; ++j) cout << M[i][j];
cout << "\n";
}
cout << flush;
break;
}
}
}
}
else if (role == "Bajtek") {
while (t--) {
int M[10][10];
for (int i = 0; i < 10; ++i) {
string s;
cin >> s;
for (int j = 0; j < 10; ++j) M[i][j] = s[j] - '0';
}
long long max_K = decode(M);
cout << (max_K % N_MOD) << "\n";
cout << flush;
}
}
return 0;
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; const long long N_MOD = 30000000000000001LL; const long long MAX_CAPACITY = 4406846758545031168LL; // Exactly 8008 * 8008 * 2^36 // Binomial coefficient for combinations ranking long long nCr(int n, int k) { if (k < 0 || k > n) return 0; long long res = 1; for (int i = 1; i <= k; ++i) { res = res * (n - i + 1) / i; } return res; } // Maps a strictly sorted array of 6 elements (0-15) to a rank (0 to 8007) long long rank_combo(int combo[6]) { long long rank = 0; int last = -1; for (int i = 0; i < 6; ++i) { for (int v = last + 1; v < combo[i]; ++v) { rank += nCr(15 - v, 5 - i); } last = combo[i]; } return rank; } // Maps a rank (0 to 8007) to a strictly sorted array of 6 elements (0-15) void unrank_combo(long long rank, int combo[6]) { int last = -1; for (int i = 0; i < 6; ++i) { for (int v = last + 1; v <= 15; ++v) { long long ways = nCr(15 - v, 5 - i); if (rank < ways) { combo[i] = v; last = v; break; } else { rank -= ways; } } } } // Core Bajtek Decoder - Now strictly evaluates ALL possible core structures long long decode(int M[10][10]) { long long max_K = -1; // Check all permutations of 4 rows that could form the Anchor for(int r0 = 0; r0 < 10; ++r0) { for(int r1 = 0; r1 < 10; ++r1) { if(r1 == r0) continue; for(int r2 = 0; r2 < 10; ++r2) { if(r2 == r0 || r2 == r1) continue; for(int r3 = 0; r3 < 10; ++r3) { if(r3 == r0 || r3 == r1 || r3 == r2) continue; int masks[10]; for(int c = 0; c < 10; ++c) { masks[c] = (M[r0][c] << 3) | (M[r1][c] << 2) | (M[r2][c] << 1) | M[r3][c]; } // Find ALL combinations of 4 columns that match the asymmetric signatures for(int c0 = 0; c0 < 10; ++c0) { if(masks[c0] != 15) continue; for(int c1 = 0; c1 < 10; ++c1) { if(c1 == c0 || masks[c1] != 14) continue; for(int c2 = 0; c2 < 10; ++c2) { if(c2 == c0 || c2 == c1 || masks[c2] != 12) continue; for(int c3 = 0; c3 < 10; ++c3) { if(c3 == c0 || c3 == c1 || c3 == c2 || masks[c3] != 8) continue; bool valid = true; // Extract and validate the 6 row labels int row_labels[6], row_idx[6]; int r_idx_cnt = 0; for(int r = 0; r < 10; ++r) { if(r == r0 || r == r1 || r == r2 || r == r3) continue; row_labels[r_idx_cnt] = (M[r][c0] << 3) | (M[r][c1] << 2) | (M[r][c2] << 1) | M[r][c3]; row_idx[r_idx_cnt] = r; r_idx_cnt++; } // Sort row labels while keeping track of original indices for(int i = 0; i < 6; ++i) { for(int j = i + 1; j < 6; ++j) { if(row_labels[i] > row_labels[j]) { swap(row_labels[i], row_labels[j]); swap(row_idx[i], row_idx[j]); } else if(row_labels[i] == row_labels[j]) { valid = false; // Duplicates invalidate the core } } } if(!valid) continue; // Extract and validate the 6 column labels int col_labels[6], col_idx[6]; int c_idx_cnt = 0; for(int c = 0; c < 10; ++c) { if(c == c0 || c == c1 || c == c2 || c == c3) continue; col_labels[c_idx_cnt] = masks[c]; col_idx[c_idx_cnt] = c; c_idx_cnt++; } // Sort col labels while keeping track of original indices for(int i = 0; i < 6; ++i) { for(int j = i + 1; j < 6; ++j) { if(col_labels[i] > col_labels[j]) { swap(col_labels[i], col_labels[j]); swap(col_idx[i], col_idx[j]); } else if(col_labels[i] == col_labels[j]) { valid = false; } } } if(!valid) continue; // Reconstruct the 36-bit payload exactly as it was encoded long long payload = 0; for(int i = 0; i < 6; ++i) { for(int j = 0; j < 6; ++j) { long long bit = M[row_idx[i]][col_idx[j]]; payload |= (bit << (i * 6 + j)); } } // Calculate ranks and combine into K long long rank_r = rank_combo(row_labels); long long rank_c = rank_combo(col_labels); long long K_found = rank_c * 8008LL * (1LL << 36) + rank_r * (1LL << 36) + payload; if(K_found > max_K) max_K = K_found; }}}} }}}} return max_K; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string role; if (!(cin >> role)) return 0; long long max_N; int t; cin >> max_N >> t; if (role == "Algosia") { while (t--) { long long N; cin >> N; long long max_c = (MAX_CAPACITY - 1 - N) / N_MOD; // Iterate downwards to map to the largest valid integer representation // Ensures our correct matrix always dominates any random fake-core collisions for (long long c = max_c; c >= 0; --c) { long long K = N + c * N_MOD; long long payload = K & ((1LL << 36) - 1); long long K_div = K >> 36; long long r_idx = K_div % 8008; long long c_idx = K_div / 8008; int r_combo[6], c_combo[6]; unrank_combo(r_idx, r_combo); unrank_combo(c_idx, c_combo); int M[10][10] = {0}; // 1. Insert Top-Left 4x4 Asymmetric Anchor int core[4][4] = {{1,1,1,1}, {1,1,1,0}, {1,1,0,0}, {1,0,0,0}}; for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) M[i][j] = core[i][j]; // 2. Map row sorting labels into anchor columns for (int i = 0; i < 6; ++i) { int r = i + 4, label = r_combo[i]; M[r][0] = (label >> 3) & 1; M[r][1] = (label >> 2) & 1; M[r][2] = (label >> 1) & 1; M[r][3] = label & 1; } // 3. Map column sorting labels into anchor rows for (int j = 0; j < 6; ++j) { int col = j + 4, label = c_combo[j]; M[0][col] = (label >> 3) & 1; M[1][col] = (label >> 2) & 1; M[2][col] = (label >> 1) & 1; M[3][col] = label & 1; } // 4. Fill Matrix Payload Block for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { M[i + 4][j + 4] = (payload >> (i * 6 + j)) & 1; } } // Local verification check. // Since Bajtek runs identically on his permuted grid, finding the same K is foolproof. if (decode(M) == K) { for(int i = 0; i < 10; ++i) { for(int j = 0; j < 10; ++j) cout << M[i][j]; cout << "\n"; } cout << flush; break; } } } } else if (role == "Bajtek") { while (t--) { int M[10][10]; for (int i = 0; i < 10; ++i) { string s; cin >> s; for (int j = 0; j < 10; ++j) M[i][j] = s[j] - '0'; } long long max_K = decode(M); cout << (max_K % N_MOD) << "\n"; cout << flush; } } return 0; } |
English