#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <optional>
#include <string>
#include <vector>
using namespace std;
using u64 = uint64_t;
constexpr int K = 10;
constexpr int H = 3;
constexpr int D = 7;
constexpr int SK = 8;
constexpr int MASK_ID_BITS = 5;
constexpr int SECRET_TRIES = 1 << MASK_ID_BITS;
constexpr int MESSAGE_BITS = 55 - MASK_ID_BITS;
constexpr u64 MESSAGE_MASK = (1ULL << MESSAGE_BITS) - 1;
using HeaderRow = array<int, H>;
using Row = array<int, K>;
using Matrix = array<Row, K>;
constexpr array<HeaderRow, H> HEADER = {{
{0, 1, 0},
{0, 0, 0},
{0, 1, 1},
}};
constexpr array<HeaderRow, SK> BITS3 = {{
{0, 0, 0},
{0, 0, 1},
{0, 1, 0},
{0, 1, 1},
{1, 0, 0},
{1, 0, 1},
{1, 1, 0},
{1, 1, 1},
}};
vector<HeaderRow> perms;
u64 splitmix64(u64 x) {
x += 0x9E3779B97F4A7C15ULL;
x = (x ^ (x >> 30)) * 0xBF58476D1CE4E5B9ULL;
x = (x ^ (x >> 27)) * 0x94D049BB133111EBULL;
return x ^ (x >> 31);
}
array<u64, SECRET_TRIES> buildSecrets() {
array<u64, SECRET_TRIES> res{};
u64 seed = 0xD1B54A32D192ED03ULL;
for (int i = 0; i < SECRET_TRIES; ++i) {
seed = splitmix64(seed + static_cast<u64>(i));
res[i] = seed;
}
return res;
}
const array<u64, SECRET_TRIES> SECRETS = buildSecrets();
u64 maskFromSecret(u64 secret) {
return secret & MESSAGE_MASK;
}
Matrix expectedHeader(int skippedRow, int skippedCol) {
Matrix res{};
for (int i = 0; i < H; ++i) {
for (int j = 0; j < H; ++j) {
res[i][j] = HEADER[i][j];
}
}
for (int i = 0; i < D; ++i) {
const int rowCode = i + (i >= skippedRow);
for (int j = 0; j < H; ++j) {
res[H + i][j] = BITS3[rowCode][j];
}
const int colCode = i + (i >= skippedCol);
for (int j = 0; j < H; ++j) {
res[j][H + i] = BITS3[colCode][j];
}
}
return res;
}
u64 buildRawWord(u64 x, int secretIdx) {
const u64 mixedPayload = x ^ maskFromSecret(SECRETS[secretIdx]);
return (mixedPayload << MASK_ID_BITS) | static_cast<u64>(secretIdx);
}
Matrix encodeWithSecretIdx(u64 x, int secretIdx) {
const u64 raw = buildRawWord(x, secretIdx);
const int skippedRow = static_cast<int>(raw & 7ULL);
const int skippedCol = static_cast<int>((raw >> 3) & 7ULL);
const u64 payload49 = raw >> 6;
Matrix res = expectedHeader(skippedRow, skippedCol);
for (int i = 0; i < D; ++i) {
for (int j = 0; j < D; ++j) {
const int bit = i * D + j;
res[H + i][H + j] = (payload49 >> bit) & 1ULL;
}
}
return res;
}
pair<Row, Row> rowSumAndColSum(const Matrix& m) {
Row rows{}, cols{};
for (int i = 0; i < K; ++i) {
for (int j = 0; j < K; ++j) {
rows[i] += m[i][j];
cols[j] += m[i][j];
}
}
return {rows, cols};
}
bool inHeader(int id, const HeaderRow& ids) {
for (int x : ids) {
if (x == id) {
return true;
}
}
return false;
}
bool isValidCandidate(const Matrix& m, const HeaderRow& rows, const HeaderRow& cols,
int skippedRow, int skippedCol, Row& rowOrder, Row& colOrder) {
for (int i = 0; i < H; ++i) {
for (int j = 0; j < H; ++j) {
if (m[rows[i]][cols[j]] != HEADER[i][j]) {
return false;
}
}
}
for (int i = 0; i < H; ++i) {
rowOrder[i] = rows[i];
colOrder[i] = cols[i];
}
array<int, D> usedRow{}, usedCol{};
for (int i = 0; i < K; ++i) {
if (!inHeader(i, rows)) {
int rowNo = (m[i][cols[0]] << 2) + (m[i][cols[1]] << 1) + m[i][cols[2]];
if (rowNo == skippedRow) {
return false;
}
if (rowNo > skippedRow) {
--rowNo;
}
if (usedRow[rowNo]) {
return false;
}
usedRow[rowNo] = 1;
rowOrder[rowNo + H] = i;
}
if (!inHeader(i, cols)) {
int colNo = (m[rows[0]][i] << 2) + (m[rows[1]][i] << 1) + m[rows[2]][i];
if (colNo == skippedCol) {
return false;
}
if (colNo > skippedCol) {
--colNo;
}
if (usedCol[colNo]) {
return false;
}
usedCol[colNo] = 1;
colOrder[colNo + H] = i;
}
}
return true;
}
bool isValid(const Matrix& m, int skippedRow, int skippedCol, Row& rowOrder, Row& colOrder) {
const auto [mRowSum, mColSum] = rowSumAndColSum(m);
const Matrix expected = expectedHeader(skippedRow, skippedCol);
const auto [eRowSum, eColSum] = rowSumAndColSum(expected);
bool found = false;
for (const auto& rows : perms) {
bool ok = true;
for (int i = 0; i < H; ++i) {
if (mRowSum[rows[i]] != eRowSum[i]) {
ok = false;
}
}
if (!ok) {
continue;
}
for (const auto& cols : perms) {
ok = true;
for (int i = 0; i < H; ++i) {
if (mColSum[cols[i]] != eColSum[i]) {
ok = false;
}
}
if (!ok) {
continue;
}
Row tmpRowOrder{}, tmpColOrder{};
if (!isValidCandidate(m, rows, cols, skippedRow, skippedCol, tmpRowOrder, tmpColOrder)) {
continue;
}
if (found) {
return false;
}
found = true;
rowOrder = tmpRowOrder;
colOrder = tmpColOrder;
}
}
return found;
}
u64 computeRaw(const Matrix& m, int skippedRow, int skippedCol,
const Row& rowOrder, const Row& colOrder) {
u64 payload49 = 0;
for (int i = 0; i < D; ++i) {
for (int j = 0; j < D; ++j) {
if (m[rowOrder[i + H]][colOrder[j + H]]) {
payload49 |= (1ULL << (D * i + j));
}
}
}
return (payload49 << 6) | (static_cast<u64>(skippedCol) << 3) | static_cast<u64>(skippedRow);
}
struct DecodeResult {
int count = 0;
u64 raw = 0;
};
DecodeResult decodeRaw(const Matrix& m) {
DecodeResult res;
for (int skippedRow = 0; skippedRow < SK; ++skippedRow) {
for (int skippedCol = 0; skippedCol < SK; ++skippedCol) {
Row rowOrder{}, colOrder{};
if (!isValid(m, skippedRow, skippedCol, rowOrder, colOrder)) {
continue;
}
++res.count;
res.raw = computeRaw(m, skippedRow, skippedCol, rowOrder, colOrder);
}
}
return res;
}
struct DecodedMessage {
u64 x = 0;
int secretIdx = -1;
};
optional<DecodedMessage> decodeExplicitSecretIdx(const Matrix& m) {
const DecodeResult raw = decodeRaw(m);
if (raw.count != 1) {
return nullopt;
}
const int secretIdx = static_cast<int>(raw.raw & ((1ULL << MASK_ID_BITS) - 1));
if (secretIdx < 0 || secretIdx >= SECRET_TRIES) {
return nullopt;
}
const u64 maskedPayload = raw.raw >> MASK_ID_BITS;
const u64 x = maskedPayload ^ maskFromSecret(SECRETS[secretIdx]);
return DecodedMessage{x, secretIdx};
}
optional<Matrix> encodeFirstWorking(u64 x) {
for (int secretIdx = 0; secretIdx < SECRET_TRIES; ++secretIdx) {
Matrix candidate = encodeWithSecretIdx(x, secretIdx);
const auto decoded = decodeExplicitSecretIdx(candidate);
if (decoded && decoded->x == x && decoded->secretIdx == secretIdx) {
return candidate;
}
}
return nullopt;
}
void printMatrix(const Matrix& m) {
for (int i = 0; i < K; ++i) {
for (int j = 0; j < K; ++j) {
cout << m[i][j];
}
cout << '\n';
}
}
Matrix readMatrix() {
Matrix m{};
for (int i = 0; i < K; ++i) {
string s;
cin >> s;
for (int j = 0; j < K; ++j) {
m[i][j] = s[j] - '0';
}
}
return m;
}
void solveAlgosia(int t) {
for (int tc = 0; tc < t; ++tc) {
u64 x;
cin >> x;
auto encoded = encodeFirstWorking(x);
if (!encoded) {
encoded = encodeWithSecretIdx(min<u64>(x, MESSAGE_MASK), 0);
}
printMatrix(*encoded);
cout.flush();
}
}
void solveBajtek(int t) {
for (int tc = 0; tc < t; ++tc) {
const Matrix m = readMatrix();
const auto decoded = decodeExplicitSecretIdx(m);
cout << (decoded ? decoded->x : 1ULL) << '\n';
cout.flush();
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
for (int a = 0; a < K; ++a) {
for (int b = 0; b < K; ++b) {
if (b == a) {
continue;
}
for (int c = 0; c < K; ++c) {
if (c == a || c == b) {
continue;
}
perms.push_back({a, b, c});
}
}
}
string who;
u64 n;
int t;
cin >> who >> n >> t;
if (who == "Algosia") {
solveAlgosia(t);
} else {
solveBajtek(t);
}
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 | #include <algorithm> #include <array> #include <cstdint> #include <iostream> #include <numeric> #include <optional> #include <string> #include <vector> using namespace std; using u64 = uint64_t; constexpr int K = 10; constexpr int H = 3; constexpr int D = 7; constexpr int SK = 8; constexpr int MASK_ID_BITS = 5; constexpr int SECRET_TRIES = 1 << MASK_ID_BITS; constexpr int MESSAGE_BITS = 55 - MASK_ID_BITS; constexpr u64 MESSAGE_MASK = (1ULL << MESSAGE_BITS) - 1; using HeaderRow = array<int, H>; using Row = array<int, K>; using Matrix = array<Row, K>; constexpr array<HeaderRow, H> HEADER = {{ {0, 1, 0}, {0, 0, 0}, {0, 1, 1}, }}; constexpr array<HeaderRow, SK> BITS3 = {{ {0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}, }}; vector<HeaderRow> perms; u64 splitmix64(u64 x) { x += 0x9E3779B97F4A7C15ULL; x = (x ^ (x >> 30)) * 0xBF58476D1CE4E5B9ULL; x = (x ^ (x >> 27)) * 0x94D049BB133111EBULL; return x ^ (x >> 31); } array<u64, SECRET_TRIES> buildSecrets() { array<u64, SECRET_TRIES> res{}; u64 seed = 0xD1B54A32D192ED03ULL; for (int i = 0; i < SECRET_TRIES; ++i) { seed = splitmix64(seed + static_cast<u64>(i)); res[i] = seed; } return res; } const array<u64, SECRET_TRIES> SECRETS = buildSecrets(); u64 maskFromSecret(u64 secret) { return secret & MESSAGE_MASK; } Matrix expectedHeader(int skippedRow, int skippedCol) { Matrix res{}; for (int i = 0; i < H; ++i) { for (int j = 0; j < H; ++j) { res[i][j] = HEADER[i][j]; } } for (int i = 0; i < D; ++i) { const int rowCode = i + (i >= skippedRow); for (int j = 0; j < H; ++j) { res[H + i][j] = BITS3[rowCode][j]; } const int colCode = i + (i >= skippedCol); for (int j = 0; j < H; ++j) { res[j][H + i] = BITS3[colCode][j]; } } return res; } u64 buildRawWord(u64 x, int secretIdx) { const u64 mixedPayload = x ^ maskFromSecret(SECRETS[secretIdx]); return (mixedPayload << MASK_ID_BITS) | static_cast<u64>(secretIdx); } Matrix encodeWithSecretIdx(u64 x, int secretIdx) { const u64 raw = buildRawWord(x, secretIdx); const int skippedRow = static_cast<int>(raw & 7ULL); const int skippedCol = static_cast<int>((raw >> 3) & 7ULL); const u64 payload49 = raw >> 6; Matrix res = expectedHeader(skippedRow, skippedCol); for (int i = 0; i < D; ++i) { for (int j = 0; j < D; ++j) { const int bit = i * D + j; res[H + i][H + j] = (payload49 >> bit) & 1ULL; } } return res; } pair<Row, Row> rowSumAndColSum(const Matrix& m) { Row rows{}, cols{}; for (int i = 0; i < K; ++i) { for (int j = 0; j < K; ++j) { rows[i] += m[i][j]; cols[j] += m[i][j]; } } return {rows, cols}; } bool inHeader(int id, const HeaderRow& ids) { for (int x : ids) { if (x == id) { return true; } } return false; } bool isValidCandidate(const Matrix& m, const HeaderRow& rows, const HeaderRow& cols, int skippedRow, int skippedCol, Row& rowOrder, Row& colOrder) { for (int i = 0; i < H; ++i) { for (int j = 0; j < H; ++j) { if (m[rows[i]][cols[j]] != HEADER[i][j]) { return false; } } } for (int i = 0; i < H; ++i) { rowOrder[i] = rows[i]; colOrder[i] = cols[i]; } array<int, D> usedRow{}, usedCol{}; for (int i = 0; i < K; ++i) { if (!inHeader(i, rows)) { int rowNo = (m[i][cols[0]] << 2) + (m[i][cols[1]] << 1) + m[i][cols[2]]; if (rowNo == skippedRow) { return false; } if (rowNo > skippedRow) { --rowNo; } if (usedRow[rowNo]) { return false; } usedRow[rowNo] = 1; rowOrder[rowNo + H] = i; } if (!inHeader(i, cols)) { int colNo = (m[rows[0]][i] << 2) + (m[rows[1]][i] << 1) + m[rows[2]][i]; if (colNo == skippedCol) { return false; } if (colNo > skippedCol) { --colNo; } if (usedCol[colNo]) { return false; } usedCol[colNo] = 1; colOrder[colNo + H] = i; } } return true; } bool isValid(const Matrix& m, int skippedRow, int skippedCol, Row& rowOrder, Row& colOrder) { const auto [mRowSum, mColSum] = rowSumAndColSum(m); const Matrix expected = expectedHeader(skippedRow, skippedCol); const auto [eRowSum, eColSum] = rowSumAndColSum(expected); bool found = false; for (const auto& rows : perms) { bool ok = true; for (int i = 0; i < H; ++i) { if (mRowSum[rows[i]] != eRowSum[i]) { ok = false; } } if (!ok) { continue; } for (const auto& cols : perms) { ok = true; for (int i = 0; i < H; ++i) { if (mColSum[cols[i]] != eColSum[i]) { ok = false; } } if (!ok) { continue; } Row tmpRowOrder{}, tmpColOrder{}; if (!isValidCandidate(m, rows, cols, skippedRow, skippedCol, tmpRowOrder, tmpColOrder)) { continue; } if (found) { return false; } found = true; rowOrder = tmpRowOrder; colOrder = tmpColOrder; } } return found; } u64 computeRaw(const Matrix& m, int skippedRow, int skippedCol, const Row& rowOrder, const Row& colOrder) { u64 payload49 = 0; for (int i = 0; i < D; ++i) { for (int j = 0; j < D; ++j) { if (m[rowOrder[i + H]][colOrder[j + H]]) { payload49 |= (1ULL << (D * i + j)); } } } return (payload49 << 6) | (static_cast<u64>(skippedCol) << 3) | static_cast<u64>(skippedRow); } struct DecodeResult { int count = 0; u64 raw = 0; }; DecodeResult decodeRaw(const Matrix& m) { DecodeResult res; for (int skippedRow = 0; skippedRow < SK; ++skippedRow) { for (int skippedCol = 0; skippedCol < SK; ++skippedCol) { Row rowOrder{}, colOrder{}; if (!isValid(m, skippedRow, skippedCol, rowOrder, colOrder)) { continue; } ++res.count; res.raw = computeRaw(m, skippedRow, skippedCol, rowOrder, colOrder); } } return res; } struct DecodedMessage { u64 x = 0; int secretIdx = -1; }; optional<DecodedMessage> decodeExplicitSecretIdx(const Matrix& m) { const DecodeResult raw = decodeRaw(m); if (raw.count != 1) { return nullopt; } const int secretIdx = static_cast<int>(raw.raw & ((1ULL << MASK_ID_BITS) - 1)); if (secretIdx < 0 || secretIdx >= SECRET_TRIES) { return nullopt; } const u64 maskedPayload = raw.raw >> MASK_ID_BITS; const u64 x = maskedPayload ^ maskFromSecret(SECRETS[secretIdx]); return DecodedMessage{x, secretIdx}; } optional<Matrix> encodeFirstWorking(u64 x) { for (int secretIdx = 0; secretIdx < SECRET_TRIES; ++secretIdx) { Matrix candidate = encodeWithSecretIdx(x, secretIdx); const auto decoded = decodeExplicitSecretIdx(candidate); if (decoded && decoded->x == x && decoded->secretIdx == secretIdx) { return candidate; } } return nullopt; } void printMatrix(const Matrix& m) { for (int i = 0; i < K; ++i) { for (int j = 0; j < K; ++j) { cout << m[i][j]; } cout << '\n'; } } Matrix readMatrix() { Matrix m{}; for (int i = 0; i < K; ++i) { string s; cin >> s; for (int j = 0; j < K; ++j) { m[i][j] = s[j] - '0'; } } return m; } void solveAlgosia(int t) { for (int tc = 0; tc < t; ++tc) { u64 x; cin >> x; auto encoded = encodeFirstWorking(x); if (!encoded) { encoded = encodeWithSecretIdx(min<u64>(x, MESSAGE_MASK), 0); } printMatrix(*encoded); cout.flush(); } } void solveBajtek(int t) { for (int tc = 0; tc < t; ++tc) { const Matrix m = readMatrix(); const auto decoded = decodeExplicitSecretIdx(m); cout << (decoded ? decoded->x : 1ULL) << '\n'; cout.flush(); } } int main() { ios::sync_with_stdio(false); cin.tie(0); for (int a = 0; a < K; ++a) { for (int b = 0; b < K; ++b) { if (b == a) { continue; } for (int c = 0; c < K; ++c) { if (c == a || c == b) { continue; } perms.push_back({a, b, c}); } } } string who; u64 n; int t; cin >> who >> n >> t; if (who == "Algosia") { solveAlgosia(t); } else { solveBajtek(t); } return 0; } |
English