#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 << ")"; }
auto operator<<(auto &o, auto a) -> decltype(all(a), o);
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x)
#else
#define deb(...) 0
#endif
const int BITS = 50, WORDS = 100;
const ll BASE = 1LL << BITS;
struct bigint_t {
array<ll, WORDS> words{};
bigint_t() {}
bigint_t(const string &s) {
rep(i, sz(s)) {
if (s[i] == '0') {}
else if (s[i] == '1') xorbit(i);
else assert(false);
}
}
bool iszero() {
return *max_element(all(words)) == 0;
}
void fillrandom(mt19937_64 &rnd) {
for (auto &a : words) a = rnd() % BASE;
}
bool getbit(int id) {
assert(0 <= id && id < BITS * WORDS);
return words[id/BITS] & (1LL << (id%BITS));
}
void xorbit(int id) {
assert(0 <= id && id < BITS * WORDS);
words[id/BITS] ^= (1LL << (id%BITS));
}
string to_string(int strlen) {
string s(strlen, '0');
rep(i, strlen) if (getbit(i)) s[i] = '1';
return s;
}
void normalize() {
rep(i, WORDS) {
if (words[i] < 0) {
assert(i+1 < WORDS);
words[i] += BASE;
words[i+1]--;
}
assert(words[i] >= 0);
ll carry = words[i] / BASE;
words[i] %= BASE;
if (carry) {
assert(i+1 < WORDS);
words[i+1] += carry;
}
}
}
bigint_t& operator^=(const bigint_t &o) {
rep(i, WORDS) words[i] ^= o.words[i];
return *this;
}
bigint_t& operator+=(int i) {
words[0] += i; normalize();
return *this;
}
bigint_t& operator-=(int i) {
words[0] -= i; normalize();
return *this;
}
bigint_t& operator*=(int i) {
for (ll &a : words) a *= i;
normalize();
return *this;
}
int divrem(int o) {
int rem = 0;
for (int i = WORDS - 1; i >= 0; --i) {
words[i] += rem * BASE;
rem = int(words[i] % o);
words[i] /= o;
}
normalize();
return rem;
}
};
char toPKN(int i) {
if (i == 0) return 'P';
if (i == 1) return 'N';
if (i == 2) return 'K';
assert(false);
}
int fromPKN(char ch) {
if (ch == 'P') return 0;
if (ch == 'N') return 1;
if (ch == 'K') return 2;
assert(false);
}
int gameValue(int a, int b) {
int d = (a-b+3)%3;
if (d == 0) return 0;
if (d == 1) return 1;
if (d == 2) return -1;
assert(false);
}
int gameValue(bool iAmAlgosia, int mine, int other) {
return iAmAlgosia ? gameValue(mine, other) : gameValue(other, mine);
}
// log(3,2) * 10**12
const ll LOG_3 = 1584962500721;
const ll LOG_2 = 1000000000000;
const ll LOG_4 = 2*LOG_2;
const ll LOG_4d3 = LOG_4 - LOG_3;
// const ll LOG_3 = 1;
// const ll LOG_2 = 1;
// const ll LOG_4 = 1;
// const ll LOG_4d3 = 1;
struct solver_t {
bigint_t xorA, xorB;
bigint_t myWord;
int myWordLen;
bool iAmAlgosia;
int bal = 0;
ll transmittedA = 0;
ll transmittedB = 0;
optional<int> lastProduced;
vi otherOps;
solver_t(string whoAmIStr, string myWordStr) {
mt19937_64 gen(606133268284446439+2);
xorA.fillrandom(gen);
xorB.fillrandom(gen);
myWord = bigint_t(myWordStr);
myWordLen = sz(myWordStr);
if (whoAmIStr == "Algosia") {
iAmAlgosia = true;
myWord ^= xorA;
}
else if (whoAmIStr == "Bajtek") {
iAmAlgosia = false;
myWord ^= xorB;
}
else assert(false);
}
ll& transmittedMine() { return iAmAlgosia ? transmittedA : transmittedB; }
ll& transmittedOther() { return iAmAlgosia ? transmittedB : transmittedA; }
int myScore() { return iAmAlgosia ? bal : -bal; }
int produce() {
assert(!lastProduced.has_value());
if (bal == 0) {
lastProduced.emplace(myWord.divrem(3));
}
else if (pair(transmittedMine(), iAmAlgosia) < pair(transmittedOther(), !iAmAlgosia)) {
if (myWord.getbit(0) == 0 && myWord.getbit(1) == 0) {
myWord.divrem(4);
lastProduced.emplace(0);
}
else {
int r = myWord.divrem(4);
myWord *= 3;
myWord += r - 1;
if (myScore() == 1) lastProduced.emplace(2);
else if (myScore() == -1) lastProduced.emplace(1);
else assert(false);
}
}
else {
lastProduced.emplace(0);
}
return *lastProduced;
}
void consume(int other) {
int mine = lastProduced.value();
lastProduced.reset();
if (bal == 0) {
otherOps.pb(other);
transmittedA += LOG_3;
transmittedB += LOG_3;
}
else if (pair(transmittedMine(), iAmAlgosia) < pair(transmittedOther(), !iAmAlgosia)) {
assert(other == 0);
if (mine == 0)
transmittedMine() += LOG_4;
else
transmittedMine() += LOG_4d3;
}
else {
assert(mine == 0);
if (other == 0) {
transmittedOther() += LOG_4;
otherOps.pb(3);
}
else {
transmittedOther() += LOG_4d3;
otherOps.pb(4);
}
}
bal += gameValue(iAmAlgosia, mine, other);
assert(abs(bal) <= 1);
}
string restore() {
assert(myWord.iszero());
bigint_t i;
for (int op : otherOps | views::reverse) {
if (op < 3) {
i *= 3;
i += op;
}
else if (op == 3) {
i *= 4;
}
else if (op == 4) {
int r = i.divrem(3);
i *= 4;
i += r + 1;
}
else assert(false);
}
i ^= iAmAlgosia ? xorB : xorA;
return i.to_string(myWordLen);
}
};
void check(string wa, string wb) {
solver_t sa("Algosia", wa);
solver_t sb("Bajtek", wb);
int rounds = 1e9;
rep(idround, 4999) {
int ta = sa.produce();
int tb = sb.produce();
sa.consume(tb);
sb.consume(ta);
if (sa.myWord.iszero() && sb.myWord.iszero())
rounds = min(rounds, idround + 1);
// deb(_, sa.myWord.words[0], sa.myWord.words[1], sa.myWord.words[2]);
}
string geta = sa.restore();
string getb = sb.restore();
assert(geta == wb);
assert(getb == wa);
if (rounds > 4950)
deb(rounds);
}
void check(int seed) {
mt19937 rnd{(unsigned)seed};
fwd(n, 1, 5001) {
string sa(n, '0'), sb(n, '0');
rep(i, n) {
if (rnd() & 1) sa[i]++;
if (rnd() & 1) sb[i]++;
}
check(sa, sb);
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cout << fixed << setprecision(10);
// rep(i, 1e6) check(i);
string whoami;
int n, t;
cin >> whoami >> n >> t;
rep(_, t) {
string word;
cin >> word;
solver_t solver(whoami, word);
rep(__, 4999) {
int i = solver.produce();
char ch = toPKN(i);
cout << ch << endl;
cin >> ch;
solver.consume(fromPKN(ch));
}
word = solver.restore();
cout << "! " << word << endl;
}
cout << flush;
_Exit(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 | #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 << ")"; } auto operator<<(auto &o, auto a) -> decltype(all(a), o); DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); #define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x) #else #define deb(...) 0 #endif const int BITS = 50, WORDS = 100; const ll BASE = 1LL << BITS; struct bigint_t { array<ll, WORDS> words{}; bigint_t() {} bigint_t(const string &s) { rep(i, sz(s)) { if (s[i] == '0') {} else if (s[i] == '1') xorbit(i); else assert(false); } } bool iszero() { return *max_element(all(words)) == 0; } void fillrandom(mt19937_64 &rnd) { for (auto &a : words) a = rnd() % BASE; } bool getbit(int id) { assert(0 <= id && id < BITS * WORDS); return words[id/BITS] & (1LL << (id%BITS)); } void xorbit(int id) { assert(0 <= id && id < BITS * WORDS); words[id/BITS] ^= (1LL << (id%BITS)); } string to_string(int strlen) { string s(strlen, '0'); rep(i, strlen) if (getbit(i)) s[i] = '1'; return s; } void normalize() { rep(i, WORDS) { if (words[i] < 0) { assert(i+1 < WORDS); words[i] += BASE; words[i+1]--; } assert(words[i] >= 0); ll carry = words[i] / BASE; words[i] %= BASE; if (carry) { assert(i+1 < WORDS); words[i+1] += carry; } } } bigint_t& operator^=(const bigint_t &o) { rep(i, WORDS) words[i] ^= o.words[i]; return *this; } bigint_t& operator+=(int i) { words[0] += i; normalize(); return *this; } bigint_t& operator-=(int i) { words[0] -= i; normalize(); return *this; } bigint_t& operator*=(int i) { for (ll &a : words) a *= i; normalize(); return *this; } int divrem(int o) { int rem = 0; for (int i = WORDS - 1; i >= 0; --i) { words[i] += rem * BASE; rem = int(words[i] % o); words[i] /= o; } normalize(); return rem; } }; char toPKN(int i) { if (i == 0) return 'P'; if (i == 1) return 'N'; if (i == 2) return 'K'; assert(false); } int fromPKN(char ch) { if (ch == 'P') return 0; if (ch == 'N') return 1; if (ch == 'K') return 2; assert(false); } int gameValue(int a, int b) { int d = (a-b+3)%3; if (d == 0) return 0; if (d == 1) return 1; if (d == 2) return -1; assert(false); } int gameValue(bool iAmAlgosia, int mine, int other) { return iAmAlgosia ? gameValue(mine, other) : gameValue(other, mine); } // log(3,2) * 10**12 const ll LOG_3 = 1584962500721; const ll LOG_2 = 1000000000000; const ll LOG_4 = 2*LOG_2; const ll LOG_4d3 = LOG_4 - LOG_3; // const ll LOG_3 = 1; // const ll LOG_2 = 1; // const ll LOG_4 = 1; // const ll LOG_4d3 = 1; struct solver_t { bigint_t xorA, xorB; bigint_t myWord; int myWordLen; bool iAmAlgosia; int bal = 0; ll transmittedA = 0; ll transmittedB = 0; optional<int> lastProduced; vi otherOps; solver_t(string whoAmIStr, string myWordStr) { mt19937_64 gen(606133268284446439+2); xorA.fillrandom(gen); xorB.fillrandom(gen); myWord = bigint_t(myWordStr); myWordLen = sz(myWordStr); if (whoAmIStr == "Algosia") { iAmAlgosia = true; myWord ^= xorA; } else if (whoAmIStr == "Bajtek") { iAmAlgosia = false; myWord ^= xorB; } else assert(false); } ll& transmittedMine() { return iAmAlgosia ? transmittedA : transmittedB; } ll& transmittedOther() { return iAmAlgosia ? transmittedB : transmittedA; } int myScore() { return iAmAlgosia ? bal : -bal; } int produce() { assert(!lastProduced.has_value()); if (bal == 0) { lastProduced.emplace(myWord.divrem(3)); } else if (pair(transmittedMine(), iAmAlgosia) < pair(transmittedOther(), !iAmAlgosia)) { if (myWord.getbit(0) == 0 && myWord.getbit(1) == 0) { myWord.divrem(4); lastProduced.emplace(0); } else { int r = myWord.divrem(4); myWord *= 3; myWord += r - 1; if (myScore() == 1) lastProduced.emplace(2); else if (myScore() == -1) lastProduced.emplace(1); else assert(false); } } else { lastProduced.emplace(0); } return *lastProduced; } void consume(int other) { int mine = lastProduced.value(); lastProduced.reset(); if (bal == 0) { otherOps.pb(other); transmittedA += LOG_3; transmittedB += LOG_3; } else if (pair(transmittedMine(), iAmAlgosia) < pair(transmittedOther(), !iAmAlgosia)) { assert(other == 0); if (mine == 0) transmittedMine() += LOG_4; else transmittedMine() += LOG_4d3; } else { assert(mine == 0); if (other == 0) { transmittedOther() += LOG_4; otherOps.pb(3); } else { transmittedOther() += LOG_4d3; otherOps.pb(4); } } bal += gameValue(iAmAlgosia, mine, other); assert(abs(bal) <= 1); } string restore() { assert(myWord.iszero()); bigint_t i; for (int op : otherOps | views::reverse) { if (op < 3) { i *= 3; i += op; } else if (op == 3) { i *= 4; } else if (op == 4) { int r = i.divrem(3); i *= 4; i += r + 1; } else assert(false); } i ^= iAmAlgosia ? xorB : xorA; return i.to_string(myWordLen); } }; void check(string wa, string wb) { solver_t sa("Algosia", wa); solver_t sb("Bajtek", wb); int rounds = 1e9; rep(idround, 4999) { int ta = sa.produce(); int tb = sb.produce(); sa.consume(tb); sb.consume(ta); if (sa.myWord.iszero() && sb.myWord.iszero()) rounds = min(rounds, idround + 1); // deb(_, sa.myWord.words[0], sa.myWord.words[1], sa.myWord.words[2]); } string geta = sa.restore(); string getb = sb.restore(); assert(geta == wb); assert(getb == wa); if (rounds > 4950) deb(rounds); } void check(int seed) { mt19937 rnd{(unsigned)seed}; fwd(n, 1, 5001) { string sa(n, '0'), sb(n, '0'); rep(i, n) { if (rnd() & 1) sa[i]++; if (rnd() & 1) sb[i]++; } check(sa, sb); } } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); // rep(i, 1e6) check(i); string whoami; int n, t; cin >> whoami >> n >> t; rep(_, t) { string word; cin >> word; solver_t solver(whoami, word); rep(__, 4999) { int i = solver.produce(); char ch = toPKN(i); cout << ch << endl; cin >> ch; solver.consume(fromPKN(ch)); } word = solver.restore(); cout << "! " << word << endl; } cout << flush; _Exit(0); } |
English