#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
template <class T>
using Ve = vector<T>;
#define ALL(v) (v).begin(), (v).end()
#define pii pair<ll, ll>
#define rep(i, a, b) for(ll i = (a); i <= (b); ++i)
#define per(i, a, b) for(ll i = (a); i >= (b); --i)
#define pb push_back
struct BigInt {
vector<uint32_t> d;
BigInt() {}
BigInt(uint32_t v) { if (v) d.pb(v); }
};
int cmp(const BigInt& a, const BigInt& b) {
if (a.d.size() != b.d.size()) return a.d.size() < b.d.size() ? -1 : 1;
per(i, (int)a.d.size() - 1, 0) {
if (a.d[i] != b.d[i]) return a.d[i] < b.d[i] ? -1 : 1;
}
return 0;
}
BigInt add(const BigInt& a, const BigInt& b) {
BigInt c;
uint64_t carry = 0;
int n = max(a.d.size(), b.d.size());
for (int i = 0; i < n || carry; ++i) {
uint64_t sum = carry;
if (i < a.d.size()) sum += a.d[i];
if (i < b.d.size()) sum += b.d[i];
c.d.pb(sum & 0xFFFFFFFF);
carry = sum >> 32;
}
return c;
}
BigInt sub(const BigInt& a, const BigInt& b) {
BigInt c;
int64_t carry = 0;
rep(i, 0, (int)a.d.size() - 1) {
int64_t diff = a.d[i] - carry - (i < b.d.size() ? b.d[i] : 0);
if (diff < 0) {
diff += 0x100000000LL;
carry = 1;
} else carry = 0;
c.d.pb(diff);
}
while (!c.d.empty() && c.d.back() == 0) c.d.pop_back();
return c;
}
BigInt div_small(const BigInt& a, uint32_t b) {
BigInt c;
c.d.resize(a.d.size());
uint64_t rem = 0;
per(i, (int)a.d.size() - 1, 0) {
rem = (rem << 32) + a.d[i];
c.d[i] = rem / b;
rem %= b;
}
while (!c.d.empty() && c.d.back() == 0) c.d.pop_back();
return c;
}
BigInt shl(const BigInt& a, int shift) {
if (a.d.empty()) return a;
BigInt c;
int words = shift / 32;
int bits = shift % 32;
c.d.assign(words, 0);
uint64_t carry = 0;
rep(i, 0, (int)a.d.size() - 1) {
uint64_t val = ((uint64_t)a.d[i] << bits) + carry;
c.d.pb(val & 0xFFFFFFFF);
carry = val >> 32;
}
if (carry) c.d.pb(carry);
return c;
}
char beats(char m) {
if (m == 'P') return 'N';
if (m == 'K') return 'P';
return 'K';
}
namespace S1 {
string role;
ll n, T;
ll tonum(char c) {
if(c == 'P') return 0;
if(c == 'K') return 1;
return 2;
}
char tochar(ll x) {
if(x == 0) return 'P';
if(x == 1) return 'K';
return 'N';
}
Ve<ll> encode(string s, ll len) {
Ve<ll> res;
while(s.find('1') != string :: npos) {
ll r = 0;
rep(i, 0, (ll)s.size() - 1) {
ll v = r * 2 + (s[i] - '0');
s[i] = v / 3 + '0', r = v % 3;
}
res.pb(r);
}
while(res.size() < len) res.pb(0);
return res;
}
string decode(Ve<ll> s, ll len) {
string res = "0";
per(i, (ll)s.size() - 1, 0) {
ll up = s[i];
per(j, (ll)res.size() - 1, 0) {
ll v = (res[j] - '0') * 3 + up;
res[j] = v % 2 + '0', up = v / 2;
}
while(up) res.insert(res.begin(), up % 2 + '0'), up /= 2;
}
while(res.size() < len) res.insert(res.begin(), '0');
return res;
}
void work(string role, int n, int T) {
ll len = 0;
string s(n, '1');
while(s.find('1') != string :: npos) {
ll r = 0;
rep(i, 0, (ll)s.size() - 1) {
ll v = r * 2 + (s[i] - '0');
s[i] = v / 3 + '0', r = v % 3;
}
++len;
}
auto solve = [&]() {
cin >> s; Ve<ll> a = encode(s, len), b(len);
ll d = 0, qa = 0, qb = 0;
mt19937 rnd(108432);
while(qa < len || qb < len) {
ll o = 0, cur = rnd() % 3;
if(!d) {
if(role[0] == 'A') {
if(qa < len) o = (a[qa] + cur) % 3;
}
else {
if(qb < len) o = (a[qb] + cur) % 3;
}
}
else if(d == 1) {
if(role[0] == 'A') o = 1;
else o = 0;
}
else {
if(role[0] == 'B') o = 1;
else o = 0;
}
cout << tochar(o) << endl;
char oo; cin >> oo;
ll to = tonum(oo);
ll ma = (role[0] == 'A') ? o : to;
ll mb = (role[0] == 'B') ? o : to;
if(!d) {
if(role[0] == 'A' && qb < len) b[qb] = (to - cur + 3) % 3;
else if(role[0] == 'B' && qa < len) b[qa] = (to - cur + 3) % 3;
++qa, ++qb;
}
if((mb - ma + 3) % 3 == 1) ++d;
else if((ma - mb + 3) % 3 == 1) --d;
}
string ans = decode(b, n);
cout << "! " << ans << endl;
};
while(T--) solve();
}
}
void solve() {
string role; cin >> role;
bool isA = (role == "Algosia");
int n, t; cin >> n >> t;
if(n == 5) return S1 :: work(role, n, t), void();
mt19937 rngA(108432), rngB(234801);
BigInt TWO_10000 = shl(BigInt(1), 10000);
while(t--) {
string my_str;
cin >> my_str;
Ve<int> xA(n), xB(n);
rep(i, 0, n - 1) xA[i] = rngA() & 1, xB[i] = rngB() & 1;
rep(i, 0, n - 1) {
if(isA) my_str[i] ^= xA[i];
else my_str[i] ^= xB[i];
}
BigInt V;
rep(i, 0, n - 1) {
if (my_str[i] == '1') {
int pos = 10000 + n - 1 - i;
int word = pos / 32;
int bit = pos % 32;
if (V.d.size() <= word) V.d.resize(word + 1, 0);
V.d[word] |= (1u << bit);
}
}
BigInt LA = 0, RA = shl(BigInt(1), 15000);
BigInt LB = 0, RB = RA;
BigInt VA = isA ? V : BigInt(0);
BigInt VB = !isA ? V : BigInt(0);
int D = 0;
while (true) {
if (cmp(sub(RA, LA), TWO_10000) <= 0 && cmp(sub(RB, LB), TWO_10000) <= 0) break;
char moves[] = {'P', 'K', 'N'};
int pA[3] = {0, 1, 2}, pB[3] = {0, 1, 2};
per(i, 2, 1) swap(pA[i], pA[rngA() % (i + 1)]);
per(i, 2, 1) swap(pB[i], pB[rngB() % (i + 1)]);
char mA = ' ', mB = ' ';
BigInt WA = sub(RA, LA), WB = sub(RB, LB);
int sA = -1, sB = -1;
if (D == 0) {
BigInt WA3 = div_small(WA, 3), P1A = add(LA, WA3), P2A = add(P1A, WA3);
BigInt WB3 = div_small(WB, 3), P1B = add(LB, WB3), P2B = add(P1B, WB3);
if (isA) sA = (cmp(VA, P1A) < 0) ? 0 : ((cmp(VA, P2A) < 0) ? 1 : 2);
if (!isA) sB = (cmp(VB, P1B) < 0) ? 0 : ((cmp(VB, P2B) < 0) ? 1 : 2);
if (isA) mA = moves[pA[sA]];
if (!isA) mB = moves[pB[sB]];
} else if (D == 1) {
mA = moves[pA[0]];
char v[] = {mA, beats(mA)};
BigInt WB4 = div_small(WB, 4), P1B = add(LB, WB4);
if (!isA) {
sB = (cmp(VB, P1B) < 0) ? 0 : 1;
mB = v[sB];
}
} else {
mB = moves[pB[0]];
char v[] = {mB, beats(mB)};
BigInt WA4 = div_small(WA, 4), P1A = add(LA, WA4);
if (isA) {
sA = (cmp(VA, P1A) < 0) ? 0 : 1;
mA = v[sA];
}
}
if (isA) { cout << mA << endl; cin >> mB; }
else { cout << mB << endl; cin >> mA; }
if (D == 0) {
rep(i, 0, 2) if (moves[pA[i]] == mA) sA = i;
rep(i, 0, 2) if (moves[pB[i]] == mB) sB = i;
BigInt WA3 = div_small(WA, 3), P1A = add(LA, WA3), P2A = add(P1A, WA3);
if (sA == 0) RA = P1A;
else if (sA == 1) { LA = P1A; RA = P2A; }
else LA = P2A;
BigInt WB3 = div_small(WB, 3), P1B = add(LB, WB3), P2B = add(P1B, WB3);
if (sB == 0) RB = P1B;
else if (sB == 1) { LB = P1B; RB = P2B; }
else LB = P2B;
} else if (D == 1) {
char v[] = {moves[pA[0]], beats(moves[pA[0]])};
sB = (mB == v[0]) ? 0 : 1;
BigInt WB4 = div_small(WB, 4), P1B = add(LB, WB4);
if (sB == 0) RB = P1B; else LB = P1B;
} else {
char v[] = {moves[pB[0]], beats(moves[pB[0]])};
sA = (mA == v[0]) ? 0 : 1;
BigInt WA4 = div_small(WA, 4), P1A = add(LA, WA4);
if (sA == 0) RA = P1A; else LA = P1A;
}
int delta = 0;
if (mA == 'P' && mB == 'K') delta = 1;
else if (mA == 'K' && mB == 'N') delta = 1;
else if (mA == 'N' && mB == 'P') delta = 1;
else if (mA == 'K' && mB == 'P') delta = -1;
else if (mA == 'N' && mB == 'K') delta = -1;
else if (mA == 'P' && mB == 'N') delta = -1;
D += delta;
}
BigInt SA = div_small(add(LA, sub(TWO_10000, BigInt(1))), 1);
BigInt SB = div_small(add(LB, sub(TWO_10000, BigInt(1))), 1);
auto extract_bits = [&](BigInt target) {
string res = "";
rep(i, 0, n - 1) {
int pos = 10000 + n - 1 - i;
int word = pos / 32, bit = pos % 32;
res += (word < target.d.size() && (target.d[word] & (1u << bit))) ? '1' : '0';
}
return res;
};
string ans;
if (isA) ans = extract_bits(SB);
else ans = extract_bits(SA);
rep(i, 0, n - 1) {
if(isA) ans[i] ^= xB[i];
else ans[i] ^= xA[i];
}
cout << "! " << ans << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; template <class T> using Ve = vector<T>; #define ALL(v) (v).begin(), (v).end() #define pii pair<ll, ll> #define rep(i, a, b) for(ll i = (a); i <= (b); ++i) #define per(i, a, b) for(ll i = (a); i >= (b); --i) #define pb push_back struct BigInt { vector<uint32_t> d; BigInt() {} BigInt(uint32_t v) { if (v) d.pb(v); } }; int cmp(const BigInt& a, const BigInt& b) { if (a.d.size() != b.d.size()) return a.d.size() < b.d.size() ? -1 : 1; per(i, (int)a.d.size() - 1, 0) { if (a.d[i] != b.d[i]) return a.d[i] < b.d[i] ? -1 : 1; } return 0; } BigInt add(const BigInt& a, const BigInt& b) { BigInt c; uint64_t carry = 0; int n = max(a.d.size(), b.d.size()); for (int i = 0; i < n || carry; ++i) { uint64_t sum = carry; if (i < a.d.size()) sum += a.d[i]; if (i < b.d.size()) sum += b.d[i]; c.d.pb(sum & 0xFFFFFFFF); carry = sum >> 32; } return c; } BigInt sub(const BigInt& a, const BigInt& b) { BigInt c; int64_t carry = 0; rep(i, 0, (int)a.d.size() - 1) { int64_t diff = a.d[i] - carry - (i < b.d.size() ? b.d[i] : 0); if (diff < 0) { diff += 0x100000000LL; carry = 1; } else carry = 0; c.d.pb(diff); } while (!c.d.empty() && c.d.back() == 0) c.d.pop_back(); return c; } BigInt div_small(const BigInt& a, uint32_t b) { BigInt c; c.d.resize(a.d.size()); uint64_t rem = 0; per(i, (int)a.d.size() - 1, 0) { rem = (rem << 32) + a.d[i]; c.d[i] = rem / b; rem %= b; } while (!c.d.empty() && c.d.back() == 0) c.d.pop_back(); return c; } BigInt shl(const BigInt& a, int shift) { if (a.d.empty()) return a; BigInt c; int words = shift / 32; int bits = shift % 32; c.d.assign(words, 0); uint64_t carry = 0; rep(i, 0, (int)a.d.size() - 1) { uint64_t val = ((uint64_t)a.d[i] << bits) + carry; c.d.pb(val & 0xFFFFFFFF); carry = val >> 32; } if (carry) c.d.pb(carry); return c; } char beats(char m) { if (m == 'P') return 'N'; if (m == 'K') return 'P'; return 'K'; } namespace S1 { string role; ll n, T; ll tonum(char c) { if(c == 'P') return 0; if(c == 'K') return 1; return 2; } char tochar(ll x) { if(x == 0) return 'P'; if(x == 1) return 'K'; return 'N'; } Ve<ll> encode(string s, ll len) { Ve<ll> res; while(s.find('1') != string :: npos) { ll r = 0; rep(i, 0, (ll)s.size() - 1) { ll v = r * 2 + (s[i] - '0'); s[i] = v / 3 + '0', r = v % 3; } res.pb(r); } while(res.size() < len) res.pb(0); return res; } string decode(Ve<ll> s, ll len) { string res = "0"; per(i, (ll)s.size() - 1, 0) { ll up = s[i]; per(j, (ll)res.size() - 1, 0) { ll v = (res[j] - '0') * 3 + up; res[j] = v % 2 + '0', up = v / 2; } while(up) res.insert(res.begin(), up % 2 + '0'), up /= 2; } while(res.size() < len) res.insert(res.begin(), '0'); return res; } void work(string role, int n, int T) { ll len = 0; string s(n, '1'); while(s.find('1') != string :: npos) { ll r = 0; rep(i, 0, (ll)s.size() - 1) { ll v = r * 2 + (s[i] - '0'); s[i] = v / 3 + '0', r = v % 3; } ++len; } auto solve = [&]() { cin >> s; Ve<ll> a = encode(s, len), b(len); ll d = 0, qa = 0, qb = 0; mt19937 rnd(108432); while(qa < len || qb < len) { ll o = 0, cur = rnd() % 3; if(!d) { if(role[0] == 'A') { if(qa < len) o = (a[qa] + cur) % 3; } else { if(qb < len) o = (a[qb] + cur) % 3; } } else if(d == 1) { if(role[0] == 'A') o = 1; else o = 0; } else { if(role[0] == 'B') o = 1; else o = 0; } cout << tochar(o) << endl; char oo; cin >> oo; ll to = tonum(oo); ll ma = (role[0] == 'A') ? o : to; ll mb = (role[0] == 'B') ? o : to; if(!d) { if(role[0] == 'A' && qb < len) b[qb] = (to - cur + 3) % 3; else if(role[0] == 'B' && qa < len) b[qa] = (to - cur + 3) % 3; ++qa, ++qb; } if((mb - ma + 3) % 3 == 1) ++d; else if((ma - mb + 3) % 3 == 1) --d; } string ans = decode(b, n); cout << "! " << ans << endl; }; while(T--) solve(); } } void solve() { string role; cin >> role; bool isA = (role == "Algosia"); int n, t; cin >> n >> t; if(n == 5) return S1 :: work(role, n, t), void(); mt19937 rngA(108432), rngB(234801); BigInt TWO_10000 = shl(BigInt(1), 10000); while(t--) { string my_str; cin >> my_str; Ve<int> xA(n), xB(n); rep(i, 0, n - 1) xA[i] = rngA() & 1, xB[i] = rngB() & 1; rep(i, 0, n - 1) { if(isA) my_str[i] ^= xA[i]; else my_str[i] ^= xB[i]; } BigInt V; rep(i, 0, n - 1) { if (my_str[i] == '1') { int pos = 10000 + n - 1 - i; int word = pos / 32; int bit = pos % 32; if (V.d.size() <= word) V.d.resize(word + 1, 0); V.d[word] |= (1u << bit); } } BigInt LA = 0, RA = shl(BigInt(1), 15000); BigInt LB = 0, RB = RA; BigInt VA = isA ? V : BigInt(0); BigInt VB = !isA ? V : BigInt(0); int D = 0; while (true) { if (cmp(sub(RA, LA), TWO_10000) <= 0 && cmp(sub(RB, LB), TWO_10000) <= 0) break; char moves[] = {'P', 'K', 'N'}; int pA[3] = {0, 1, 2}, pB[3] = {0, 1, 2}; per(i, 2, 1) swap(pA[i], pA[rngA() % (i + 1)]); per(i, 2, 1) swap(pB[i], pB[rngB() % (i + 1)]); char mA = ' ', mB = ' '; BigInt WA = sub(RA, LA), WB = sub(RB, LB); int sA = -1, sB = -1; if (D == 0) { BigInt WA3 = div_small(WA, 3), P1A = add(LA, WA3), P2A = add(P1A, WA3); BigInt WB3 = div_small(WB, 3), P1B = add(LB, WB3), P2B = add(P1B, WB3); if (isA) sA = (cmp(VA, P1A) < 0) ? 0 : ((cmp(VA, P2A) < 0) ? 1 : 2); if (!isA) sB = (cmp(VB, P1B) < 0) ? 0 : ((cmp(VB, P2B) < 0) ? 1 : 2); if (isA) mA = moves[pA[sA]]; if (!isA) mB = moves[pB[sB]]; } else if (D == 1) { mA = moves[pA[0]]; char v[] = {mA, beats(mA)}; BigInt WB4 = div_small(WB, 4), P1B = add(LB, WB4); if (!isA) { sB = (cmp(VB, P1B) < 0) ? 0 : 1; mB = v[sB]; } } else { mB = moves[pB[0]]; char v[] = {mB, beats(mB)}; BigInt WA4 = div_small(WA, 4), P1A = add(LA, WA4); if (isA) { sA = (cmp(VA, P1A) < 0) ? 0 : 1; mA = v[sA]; } } if (isA) { cout << mA << endl; cin >> mB; } else { cout << mB << endl; cin >> mA; } if (D == 0) { rep(i, 0, 2) if (moves[pA[i]] == mA) sA = i; rep(i, 0, 2) if (moves[pB[i]] == mB) sB = i; BigInt WA3 = div_small(WA, 3), P1A = add(LA, WA3), P2A = add(P1A, WA3); if (sA == 0) RA = P1A; else if (sA == 1) { LA = P1A; RA = P2A; } else LA = P2A; BigInt WB3 = div_small(WB, 3), P1B = add(LB, WB3), P2B = add(P1B, WB3); if (sB == 0) RB = P1B; else if (sB == 1) { LB = P1B; RB = P2B; } else LB = P2B; } else if (D == 1) { char v[] = {moves[pA[0]], beats(moves[pA[0]])}; sB = (mB == v[0]) ? 0 : 1; BigInt WB4 = div_small(WB, 4), P1B = add(LB, WB4); if (sB == 0) RB = P1B; else LB = P1B; } else { char v[] = {moves[pB[0]], beats(moves[pB[0]])}; sA = (mA == v[0]) ? 0 : 1; BigInt WA4 = div_small(WA, 4), P1A = add(LA, WA4); if (sA == 0) RA = P1A; else LA = P1A; } int delta = 0; if (mA == 'P' && mB == 'K') delta = 1; else if (mA == 'K' && mB == 'N') delta = 1; else if (mA == 'N' && mB == 'P') delta = 1; else if (mA == 'K' && mB == 'P') delta = -1; else if (mA == 'N' && mB == 'K') delta = -1; else if (mA == 'P' && mB == 'N') delta = -1; D += delta; } BigInt SA = div_small(add(LA, sub(TWO_10000, BigInt(1))), 1); BigInt SB = div_small(add(LB, sub(TWO_10000, BigInt(1))), 1); auto extract_bits = [&](BigInt target) { string res = ""; rep(i, 0, n - 1) { int pos = 10000 + n - 1 - i; int word = pos / 32, bit = pos % 32; res += (word < target.d.size() && (target.d[word] & (1u << bit))) ? '1' : '0'; } return res; }; string ans; if (isA) ans = extract_bits(SB); else ans = extract_bits(SA); rep(i, 0, n - 1) { if(isA) ans[i] ^= xB[i]; else ans[i] ^= xA[i]; } cout << "! " << ans << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; } |
English