// algo v5 *
// dynamic nonce + 5x5 anchors + tie-breaker hashing
// limit ~ 1.5e16
// a) kotwica w naroznikuezerwacja sztywnych wartości bitowych w narożniku, umożliwiająca
// b) kopanie "wolnego miejsca" max space / limit N i unikanie false anchorchs
// c) deterministic hash tie-breaker = macierz o max hashu
// d) TLE limis 9.5sec
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
std::chrono::time_point<std::chrono::steady_clock> global_start_time;
namespace common {
uint64_t C[40][40];
bool inited = false;
void precompute() {
if (inited) {
return;
}
memset(C, 0, sizeof(C));
for (int i = 0; i <= 35; ++i) {
C[i][0] = 1;
for (int j = 1; j <= i; ++j) {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
inited = true;
}
vector<int> unrank_comb(int n, int k, uint64_t r) {
vector<int> comb;
int pos = 0;
int remaining = k;
while (remaining > 0) {
for (int x = pos; x <= n - remaining; ++x) {
uint64_t count = C[n - x - 1][remaining - 1];
if (r < count) {
comb.push_back(x);
pos = x + 1;
--remaining;
break;
}
r -= count;
}
}
return comb;
}
uint64_t rank_comb(int n, int k, const vector<int>& comb) {
uint64_t r = 0;
int prev = 0;
int remaining = k;
for (int idx : comb) {
for (int x = prev; x < idx; ++x) {
r += C[n - x - 1][remaining - 1];
}
prev = idx + 1;
--remaining;
}
return r;
}
uint64_t hash_nonce(uint64_t nonce) {
uint64_t z = nonce + 0x9E3779B97F4A7C15ULL;
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
return z ^ (z >> 31);
}
bool is_zero_matrix(const vector<vector<int>>& matrix) {
for (const auto& row : matrix) {
for (int bit : row) {
if (bit != 0) {
return false;
}
}
}
return true;
}
} // namespace common
namespace scheme5 {
constexpr int SIG_SPACE = 27;
constexpr int SELECTED = 5;
constexpr uint64_t SIG_COMB = 80730ULL;
constexpr int PAYLOAD_BITS = 25;
constexpr uint64_t PAYLOAD_MASK = (1ULL << PAYLOAD_BITS) - 1;
uint64_t get_nonce_count(uint64_t limit_n) {
uint64_t max_data_val = (1ULL << PAYLOAD_BITS) * SIG_COMB * SIG_COMB;
uint64_t nc = max_data_val / (limit_n + 1);
if (nc > 250) nc = 250;
if (nc < 1) nc = 1;
return nc;
}
int allowed[27] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 29};
struct CandidateVotes {
int witness_count = 0;
uint64_t nonce_mask = 0;
};
uint64_t pack_state(uint64_t data_val) {
uint64_t payload = data_val & PAYLOAD_MASK;
uint64_t rem = data_val >> PAYLOAD_BITS;
uint64_t r_rank = rem % SIG_COMB;
uint64_t c_rank = rem / SIG_COMB;
return payload + (1ULL << PAYLOAD_BITS) * (r_rank + SIG_COMB * c_rank);
}
bool unpack_state(uint64_t state, uint64_t& data_val) {
uint64_t payload = state & PAYLOAD_MASK;
uint64_t rem = state >> PAYLOAD_BITS;
uint64_t r_rank = rem % SIG_COMB;
uint64_t c_rank = rem / SIG_COMB;
data_val = payload + (1ULL << PAYLOAD_BITS) * (r_rank + SIG_COMB * c_rank);
return true;
}
uint64_t obfuscate(uint64_t raw_val, uint64_t nonce) {
uint64_t h = common::hash_nonce(nonce);
uint64_t payload = raw_val & PAYLOAD_MASK;
uint64_t rem = raw_val >> PAYLOAD_BITS;
uint64_t r_rank = rem % SIG_COMB;
uint64_t c_rank = rem / SIG_COMB;
payload ^= (h & PAYLOAD_MASK);
h >>= PAYLOAD_BITS;
r_rank = (r_rank + h) % SIG_COMB;
h >>= 18;
c_rank = (c_rank + h) % SIG_COMB;
return payload + (1ULL << PAYLOAD_BITS) * (r_rank + SIG_COMB * c_rank);
}
uint64_t deobfuscate(uint64_t attempt, uint64_t nonce) {
uint64_t h = common::hash_nonce(nonce);
uint64_t payload = attempt & PAYLOAD_MASK;
uint64_t rem = attempt >> PAYLOAD_BITS;
uint64_t r_rank = rem % SIG_COMB;
uint64_t c_rank = rem / SIG_COMB;
payload ^= (h & PAYLOAD_MASK);
h >>= PAYLOAD_BITS;
r_rank = (r_rank + SIG_COMB - (h % SIG_COMB)) % SIG_COMB;
h >>= 18;
c_rank = (c_rank + SIG_COMB - (h % SIG_COMB)) % SIG_COMB;
return payload + (1ULL << PAYLOAD_BITS) * (r_rank + SIG_COMB * c_rank);
}
vector<vector<int>> build_matrix(uint64_t attempt) {
uint64_t payload = attempt & PAYLOAD_MASK;
uint64_t rem = attempt >> PAYLOAD_BITS;
uint64_t r_rank = rem % SIG_COMB;
uint64_t c_rank = rem / SIG_COMB;
vector<int> r_idx = common::unrank_comb(SIG_SPACE, SELECTED, r_rank);
vector<int> c_idx = common::unrank_comb(SIG_SPACE, SELECTED, c_rank);
vector<vector<int>> matrix(10, vector<int>(10, 0));
matrix[0][0]=1; matrix[0][1]=1; matrix[0][2]=1; matrix[0][3]=1; matrix[0][4]=1;
matrix[1][0]=1; matrix[1][1]=1; matrix[1][2]=1; matrix[1][3]=1; matrix[1][4]=0;
matrix[2][0]=1; matrix[2][1]=1; matrix[2][2]=1; matrix[2][3]=0; matrix[2][4]=0;
matrix[3][0]=1; matrix[3][1]=1; matrix[3][2]=0; matrix[3][3]=0; matrix[3][4]=0;
matrix[4][0]=1; matrix[4][1]=0; matrix[4][2]=0; matrix[4][3]=0; matrix[4][4]=0;
for (int i = 0; i < 5; ++i) {
int sig = allowed[r_idx[i]];
for (int bit = 0; bit < 5; ++bit) {
matrix[5 + i][4 - bit] = (sig >> bit) & 1;
}
}
for (int j = 0; j < 5; ++j) {
int sig = allowed[c_idx[j]];
for (int bit = 0; bit < 5; ++bit) {
matrix[4 - bit][5 + j] = (sig >> bit) & 1;
}
}
for (int i = 0; i < 25; ++i) {
int bit = (payload >> i) & 1;
matrix[5 + (i / 5)][5 + (i % 5)] = bit;
}
return matrix;
}
uint64_t decode(const vector<vector<int>>& matrix, uint64_t limit_n) {
common::precompute();
if (common::is_zero_matrix(matrix)) return 0;
int rev[32];
for (int i = 0; i < 32; ++i) rev[i] = -1;
for (int i = 0; i < 27; ++i) rev[allowed[i]] = i;
map<uint64_t, CandidateVotes> candidate_votes;
for (int r0 = 0; r0 < 10; ++r0) {
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration<double>(now - global_start_time).count() > 9.5) break;
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;
for (int r4 = 0; r4 < 10; ++r4) { if (r4 == r0 || r4 == r1 || r4 == r2 || r4 == r3) continue;
vector<int> col31, col30, col28, col24, col16;
for (int col = 0; col < 10; ++col) {
int sig = (matrix[r0][col] << 4) | (matrix[r1][col] << 3) | (matrix[r2][col] << 2) |
(matrix[r3][col] << 1) | matrix[r4][col];
if (sig == 31) col31.push_back(col);
else if (sig == 30) col30.push_back(col);
else if (sig == 28) col28.push_back(col);
else if (sig == 24) col24.push_back(col);
else if (sig == 16) col16.push_back(col);
}
if (col31.empty() || col30.empty() || col28.empty() || col24.empty() || col16.empty()) {
continue;
}
for (int c0 : col31) {
for (int c1 : col30) {
for (int c2 : col28) {
for (int c3 : col24) {
for (int c4 : col16) {
int anchor_rows[5] = {r0, r1, r2, r3, r4};
int anchor_cols[5] = {c0, c1, c2, c3, c4};
bool exact_anchor = true;
for (int row_idx = 0; row_idx < 5 && exact_anchor; ++row_idx) {
for (int col_idx = 0; col_idx < 5; ++col_idx) {
int expected = (col_idx <= 4 - row_idx) ? 1 : 0;
if (matrix[anchor_rows[row_idx]][anchor_cols[col_idx]] != expected) {
exact_anchor = false;
break;
}
}
}
if (!exact_anchor) continue;
vector<int> rem_rows;
vector<int> rem_cols;
for (int i = 0; i < 10; ++i) {
if (i != r0 && i != r1 && i != r2 && i != r3 && i != r4) rem_rows.push_back(i);
if (i != c0 && i != c1 && i != c2 && i != c3 && i != c4) rem_cols.push_back(i);
}
bool ok = true;
int seen_rs[27] = {0};
int seen_cs[27] = {0};
vector<pair<int, int>> r_pairs;
vector<pair<int, int>> c_pairs;
for (int row : rem_rows) {
int sig = (matrix[row][c0] << 4) | (matrix[row][c1] << 3) | (matrix[row][c2] << 2) |
(matrix[row][c3] << 1) | matrix[row][c4];
int idx = rev[sig];
if (idx == -1 || seen_rs[idx]) { ok = false; break; }
seen_rs[idx] = 1;
r_pairs.push_back({idx, row});
}
if (!ok) continue;
for (int col : rem_cols) {
int sig = (matrix[r0][col] << 4) | (matrix[r1][col] << 3) | (matrix[r2][col] << 2) |
(matrix[r3][col] << 1) | matrix[r4][col];
int idx = rev[sig];
if (idx == -1 || seen_cs[idx]) { ok = false; break; }
seen_cs[idx] = 1;
c_pairs.push_back({idx, col});
}
if (!ok) continue;
sort(r_pairs.begin(), r_pairs.end());
sort(c_pairs.begin(), c_pairs.end());
vector<int> r_idx_list;
vector<int> c_idx_list;
for (const auto& pair_value : r_pairs) r_idx_list.push_back(pair_value.first);
for (const auto& pair_value : c_pairs) c_idx_list.push_back(pair_value.first);
uint64_t r_rank = common::rank_comb(SIG_SPACE, SELECTED, r_idx_list);
uint64_t c_rank = common::rank_comb(SIG_SPACE, SELECTED, c_idx_list);
uint64_t payload = 0;
for (int i = 0; i < 25; ++i) {
int row = r_pairs[i / 5].second;
int col = c_pairs[i % 5].second;
if (matrix[row][col]) {
payload |= (1ULL << i);
}
}
uint64_t encoded = payload + (1ULL << PAYLOAD_BITS) * (r_rank + SIG_COMB * c_rank);
uint64_t nonce_count = get_nonce_count(limit_n);
for (uint64_t nonce = 0; nonce < nonce_count; ++nonce) {
uint64_t state = deobfuscate(encoded, nonce);
uint64_t data_val = 0;
if (!unpack_state(state, data_val)) continue;
if ((data_val % nonce_count) != nonce) continue;
uint64_t x = data_val / nonce_count;
if (x == 0 || x > limit_n) continue;
CandidateVotes& vote = candidate_votes[x];
++vote.witness_count;
vote.nonce_mask |= (1ULL << nonce);
}
}}}}}
}}}}}
if (candidate_votes.empty()) return 0;
uint64_t best_x = 0;
int best_distinct_nonces = -1;
int best_witness_count = -1;
uint64_t best_data_val = 0;
uint64_t nonce_count = get_nonce_count(limit_n);
for (const auto& [x, vote] : candidate_votes) {
int distinct_nonces = __builtin_popcountll(vote.nonce_mask);
uint64_t current_data_val = x * nonce_count + __builtin_ctzll(vote.nonce_mask);
if (distinct_nonces > best_distinct_nonces ||
(distinct_nonces == best_distinct_nonces && vote.witness_count > best_witness_count)) {
best_x = x;
best_distinct_nonces = distinct_nonces;
best_witness_count = vote.witness_count;
best_data_val = current_data_val;
continue;
}
else if (distinct_nonces == best_distinct_nonces && vote.witness_count == best_witness_count) {
if (common::hash_nonce(current_data_val) > common::hash_nonce(best_data_val)) {
best_x = x;
best_data_val = current_data_val;
}
}
}
return best_x;
}
vector<vector<int>> encode(uint64_t x, uint64_t limit_n) {
common::precompute();
// Usunięte sztywne odpowiedzi dla kod0b
uint64_t nonce_count = get_nonce_count(limit_n);
vector<vector<int>> first_matrix(10, vector<int>(10, 0));
for (uint64_t nonce = 0; nonce < nonce_count; ++nonce) {
uint64_t data_val = x * nonce_count + nonce;
uint64_t state = pack_state(data_val);
uint64_t attempt = obfuscate(state, nonce);
vector<vector<int>> matrix = build_matrix(attempt);
if (nonce == 0) first_matrix = matrix;
if (decode(matrix, limit_n) == x) {
return matrix;
}
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration<double>(now - global_start_time).count() > 9.5) {
return first_matrix;
}
}
return first_matrix;
}
} // namespace scheme5
namespace hybrid {
uint64_t decode(const vector<vector<int>>& matrix, uint64_t limit_n) {
return scheme5::decode(matrix, limit_n);
}
vector<vector<int>> encode(uint64_t x, uint64_t limit_n) {
return scheme5::encode(x, limit_n);
}
} // namespace hybrid
int main() {
global_start_time = std::chrono::steady_clock::now();
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
string role;
if (!(cin >> role)) return 0;
uint64_t n;
int t;
cin >> n >> t;
common::precompute();
if (role == "Algosia") {
while (t--) {
uint64_t x;
cin >> x;
vector<vector<int>> matrix = hybrid::encode(x, n);
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
cout << matrix[i][j];
}
cout << endl;
}
cout.flush();
}
} else if (role == "Bajtek") {
while (t--) {
vector<vector<int>> matrix(10, vector<int>(10));
for (int i = 0; i < 10; ++i) {
string row;
cin >> row;
for (int j = 0; j < 10; ++j) {
matrix[i][j] = row[j] - '0';
}
}
cout << hybrid::decode(matrix, n) << endl;
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 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 | // algo v5 * // dynamic nonce + 5x5 anchors + tie-breaker hashing // limit ~ 1.5e16 // a) kotwica w naroznikuezerwacja sztywnych wartości bitowych w narożniku, umożliwiająca // b) kopanie "wolnego miejsca" max space / limit N i unikanie false anchorchs // c) deterministic hash tie-breaker = macierz o max hashu // d) TLE limis 9.5sec #include <algorithm> #include <chrono> #include <cstdint> #include <cstring> #include <iostream> #include <map> #include <vector> using namespace std; std::chrono::time_point<std::chrono::steady_clock> global_start_time; namespace common { uint64_t C[40][40]; bool inited = false; void precompute() { if (inited) { return; } memset(C, 0, sizeof(C)); for (int i = 0; i <= 35; ++i) { C[i][0] = 1; for (int j = 1; j <= i; ++j) { C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } inited = true; } vector<int> unrank_comb(int n, int k, uint64_t r) { vector<int> comb; int pos = 0; int remaining = k; while (remaining > 0) { for (int x = pos; x <= n - remaining; ++x) { uint64_t count = C[n - x - 1][remaining - 1]; if (r < count) { comb.push_back(x); pos = x + 1; --remaining; break; } r -= count; } } return comb; } uint64_t rank_comb(int n, int k, const vector<int>& comb) { uint64_t r = 0; int prev = 0; int remaining = k; for (int idx : comb) { for (int x = prev; x < idx; ++x) { r += C[n - x - 1][remaining - 1]; } prev = idx + 1; --remaining; } return r; } uint64_t hash_nonce(uint64_t nonce) { uint64_t z = nonce + 0x9E3779B97F4A7C15ULL; z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL; z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL; return z ^ (z >> 31); } bool is_zero_matrix(const vector<vector<int>>& matrix) { for (const auto& row : matrix) { for (int bit : row) { if (bit != 0) { return false; } } } return true; } } // namespace common namespace scheme5 { constexpr int SIG_SPACE = 27; constexpr int SELECTED = 5; constexpr uint64_t SIG_COMB = 80730ULL; constexpr int PAYLOAD_BITS = 25; constexpr uint64_t PAYLOAD_MASK = (1ULL << PAYLOAD_BITS) - 1; uint64_t get_nonce_count(uint64_t limit_n) { uint64_t max_data_val = (1ULL << PAYLOAD_BITS) * SIG_COMB * SIG_COMB; uint64_t nc = max_data_val / (limit_n + 1); if (nc > 250) nc = 250; if (nc < 1) nc = 1; return nc; } int allowed[27] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 29}; struct CandidateVotes { int witness_count = 0; uint64_t nonce_mask = 0; }; uint64_t pack_state(uint64_t data_val) { uint64_t payload = data_val & PAYLOAD_MASK; uint64_t rem = data_val >> PAYLOAD_BITS; uint64_t r_rank = rem % SIG_COMB; uint64_t c_rank = rem / SIG_COMB; return payload + (1ULL << PAYLOAD_BITS) * (r_rank + SIG_COMB * c_rank); } bool unpack_state(uint64_t state, uint64_t& data_val) { uint64_t payload = state & PAYLOAD_MASK; uint64_t rem = state >> PAYLOAD_BITS; uint64_t r_rank = rem % SIG_COMB; uint64_t c_rank = rem / SIG_COMB; data_val = payload + (1ULL << PAYLOAD_BITS) * (r_rank + SIG_COMB * c_rank); return true; } uint64_t obfuscate(uint64_t raw_val, uint64_t nonce) { uint64_t h = common::hash_nonce(nonce); uint64_t payload = raw_val & PAYLOAD_MASK; uint64_t rem = raw_val >> PAYLOAD_BITS; uint64_t r_rank = rem % SIG_COMB; uint64_t c_rank = rem / SIG_COMB; payload ^= (h & PAYLOAD_MASK); h >>= PAYLOAD_BITS; r_rank = (r_rank + h) % SIG_COMB; h >>= 18; c_rank = (c_rank + h) % SIG_COMB; return payload + (1ULL << PAYLOAD_BITS) * (r_rank + SIG_COMB * c_rank); } uint64_t deobfuscate(uint64_t attempt, uint64_t nonce) { uint64_t h = common::hash_nonce(nonce); uint64_t payload = attempt & PAYLOAD_MASK; uint64_t rem = attempt >> PAYLOAD_BITS; uint64_t r_rank = rem % SIG_COMB; uint64_t c_rank = rem / SIG_COMB; payload ^= (h & PAYLOAD_MASK); h >>= PAYLOAD_BITS; r_rank = (r_rank + SIG_COMB - (h % SIG_COMB)) % SIG_COMB; h >>= 18; c_rank = (c_rank + SIG_COMB - (h % SIG_COMB)) % SIG_COMB; return payload + (1ULL << PAYLOAD_BITS) * (r_rank + SIG_COMB * c_rank); } vector<vector<int>> build_matrix(uint64_t attempt) { uint64_t payload = attempt & PAYLOAD_MASK; uint64_t rem = attempt >> PAYLOAD_BITS; uint64_t r_rank = rem % SIG_COMB; uint64_t c_rank = rem / SIG_COMB; vector<int> r_idx = common::unrank_comb(SIG_SPACE, SELECTED, r_rank); vector<int> c_idx = common::unrank_comb(SIG_SPACE, SELECTED, c_rank); vector<vector<int>> matrix(10, vector<int>(10, 0)); matrix[0][0]=1; matrix[0][1]=1; matrix[0][2]=1; matrix[0][3]=1; matrix[0][4]=1; matrix[1][0]=1; matrix[1][1]=1; matrix[1][2]=1; matrix[1][3]=1; matrix[1][4]=0; matrix[2][0]=1; matrix[2][1]=1; matrix[2][2]=1; matrix[2][3]=0; matrix[2][4]=0; matrix[3][0]=1; matrix[3][1]=1; matrix[3][2]=0; matrix[3][3]=0; matrix[3][4]=0; matrix[4][0]=1; matrix[4][1]=0; matrix[4][2]=0; matrix[4][3]=0; matrix[4][4]=0; for (int i = 0; i < 5; ++i) { int sig = allowed[r_idx[i]]; for (int bit = 0; bit < 5; ++bit) { matrix[5 + i][4 - bit] = (sig >> bit) & 1; } } for (int j = 0; j < 5; ++j) { int sig = allowed[c_idx[j]]; for (int bit = 0; bit < 5; ++bit) { matrix[4 - bit][5 + j] = (sig >> bit) & 1; } } for (int i = 0; i < 25; ++i) { int bit = (payload >> i) & 1; matrix[5 + (i / 5)][5 + (i % 5)] = bit; } return matrix; } uint64_t decode(const vector<vector<int>>& matrix, uint64_t limit_n) { common::precompute(); if (common::is_zero_matrix(matrix)) return 0; int rev[32]; for (int i = 0; i < 32; ++i) rev[i] = -1; for (int i = 0; i < 27; ++i) rev[allowed[i]] = i; map<uint64_t, CandidateVotes> candidate_votes; for (int r0 = 0; r0 < 10; ++r0) { auto now = std::chrono::steady_clock::now(); if (std::chrono::duration<double>(now - global_start_time).count() > 9.5) break; 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; for (int r4 = 0; r4 < 10; ++r4) { if (r4 == r0 || r4 == r1 || r4 == r2 || r4 == r3) continue; vector<int> col31, col30, col28, col24, col16; for (int col = 0; col < 10; ++col) { int sig = (matrix[r0][col] << 4) | (matrix[r1][col] << 3) | (matrix[r2][col] << 2) | (matrix[r3][col] << 1) | matrix[r4][col]; if (sig == 31) col31.push_back(col); else if (sig == 30) col30.push_back(col); else if (sig == 28) col28.push_back(col); else if (sig == 24) col24.push_back(col); else if (sig == 16) col16.push_back(col); } if (col31.empty() || col30.empty() || col28.empty() || col24.empty() || col16.empty()) { continue; } for (int c0 : col31) { for (int c1 : col30) { for (int c2 : col28) { for (int c3 : col24) { for (int c4 : col16) { int anchor_rows[5] = {r0, r1, r2, r3, r4}; int anchor_cols[5] = {c0, c1, c2, c3, c4}; bool exact_anchor = true; for (int row_idx = 0; row_idx < 5 && exact_anchor; ++row_idx) { for (int col_idx = 0; col_idx < 5; ++col_idx) { int expected = (col_idx <= 4 - row_idx) ? 1 : 0; if (matrix[anchor_rows[row_idx]][anchor_cols[col_idx]] != expected) { exact_anchor = false; break; } } } if (!exact_anchor) continue; vector<int> rem_rows; vector<int> rem_cols; for (int i = 0; i < 10; ++i) { if (i != r0 && i != r1 && i != r2 && i != r3 && i != r4) rem_rows.push_back(i); if (i != c0 && i != c1 && i != c2 && i != c3 && i != c4) rem_cols.push_back(i); } bool ok = true; int seen_rs[27] = {0}; int seen_cs[27] = {0}; vector<pair<int, int>> r_pairs; vector<pair<int, int>> c_pairs; for (int row : rem_rows) { int sig = (matrix[row][c0] << 4) | (matrix[row][c1] << 3) | (matrix[row][c2] << 2) | (matrix[row][c3] << 1) | matrix[row][c4]; int idx = rev[sig]; if (idx == -1 || seen_rs[idx]) { ok = false; break; } seen_rs[idx] = 1; r_pairs.push_back({idx, row}); } if (!ok) continue; for (int col : rem_cols) { int sig = (matrix[r0][col] << 4) | (matrix[r1][col] << 3) | (matrix[r2][col] << 2) | (matrix[r3][col] << 1) | matrix[r4][col]; int idx = rev[sig]; if (idx == -1 || seen_cs[idx]) { ok = false; break; } seen_cs[idx] = 1; c_pairs.push_back({idx, col}); } if (!ok) continue; sort(r_pairs.begin(), r_pairs.end()); sort(c_pairs.begin(), c_pairs.end()); vector<int> r_idx_list; vector<int> c_idx_list; for (const auto& pair_value : r_pairs) r_idx_list.push_back(pair_value.first); for (const auto& pair_value : c_pairs) c_idx_list.push_back(pair_value.first); uint64_t r_rank = common::rank_comb(SIG_SPACE, SELECTED, r_idx_list); uint64_t c_rank = common::rank_comb(SIG_SPACE, SELECTED, c_idx_list); uint64_t payload = 0; for (int i = 0; i < 25; ++i) { int row = r_pairs[i / 5].second; int col = c_pairs[i % 5].second; if (matrix[row][col]) { payload |= (1ULL << i); } } uint64_t encoded = payload + (1ULL << PAYLOAD_BITS) * (r_rank + SIG_COMB * c_rank); uint64_t nonce_count = get_nonce_count(limit_n); for (uint64_t nonce = 0; nonce < nonce_count; ++nonce) { uint64_t state = deobfuscate(encoded, nonce); uint64_t data_val = 0; if (!unpack_state(state, data_val)) continue; if ((data_val % nonce_count) != nonce) continue; uint64_t x = data_val / nonce_count; if (x == 0 || x > limit_n) continue; CandidateVotes& vote = candidate_votes[x]; ++vote.witness_count; vote.nonce_mask |= (1ULL << nonce); } }}}}} }}}}} if (candidate_votes.empty()) return 0; uint64_t best_x = 0; int best_distinct_nonces = -1; int best_witness_count = -1; uint64_t best_data_val = 0; uint64_t nonce_count = get_nonce_count(limit_n); for (const auto& [x, vote] : candidate_votes) { int distinct_nonces = __builtin_popcountll(vote.nonce_mask); uint64_t current_data_val = x * nonce_count + __builtin_ctzll(vote.nonce_mask); if (distinct_nonces > best_distinct_nonces || (distinct_nonces == best_distinct_nonces && vote.witness_count > best_witness_count)) { best_x = x; best_distinct_nonces = distinct_nonces; best_witness_count = vote.witness_count; best_data_val = current_data_val; continue; } else if (distinct_nonces == best_distinct_nonces && vote.witness_count == best_witness_count) { if (common::hash_nonce(current_data_val) > common::hash_nonce(best_data_val)) { best_x = x; best_data_val = current_data_val; } } } return best_x; } vector<vector<int>> encode(uint64_t x, uint64_t limit_n) { common::precompute(); // Usunięte sztywne odpowiedzi dla kod0b uint64_t nonce_count = get_nonce_count(limit_n); vector<vector<int>> first_matrix(10, vector<int>(10, 0)); for (uint64_t nonce = 0; nonce < nonce_count; ++nonce) { uint64_t data_val = x * nonce_count + nonce; uint64_t state = pack_state(data_val); uint64_t attempt = obfuscate(state, nonce); vector<vector<int>> matrix = build_matrix(attempt); if (nonce == 0) first_matrix = matrix; if (decode(matrix, limit_n) == x) { return matrix; } auto now = std::chrono::steady_clock::now(); if (std::chrono::duration<double>(now - global_start_time).count() > 9.5) { return first_matrix; } } return first_matrix; } } // namespace scheme5 namespace hybrid { uint64_t decode(const vector<vector<int>>& matrix, uint64_t limit_n) { return scheme5::decode(matrix, limit_n); } vector<vector<int>> encode(uint64_t x, uint64_t limit_n) { return scheme5::encode(x, limit_n); } } // namespace hybrid int main() { global_start_time = std::chrono::steady_clock::now(); ios_base::sync_with_stdio(false); cin.tie(nullptr); string role; if (!(cin >> role)) return 0; uint64_t n; int t; cin >> n >> t; common::precompute(); if (role == "Algosia") { while (t--) { uint64_t x; cin >> x; vector<vector<int>> matrix = hybrid::encode(x, n); for (int i = 0; i < 10; ++i) { for (int j = 0; j < 10; ++j) { cout << matrix[i][j]; } cout << endl; } cout.flush(); } } else if (role == "Bajtek") { while (t--) { vector<vector<int>> matrix(10, vector<int>(10)); for (int i = 0; i < 10; ++i) { string row; cin >> row; for (int j = 0; j < 10; ++j) { matrix[i][j] = row[j] - '0'; } } cout << hybrid::decode(matrix, n) << endl; cout.flush(); } } return 0; } |
English