#include <algorithm>
#include <cstring>
#include <iostream>
#include <random>
#include <string>
#include <vector>
using namespace std;
typedef uint64_t u64;
typedef unsigned __int128 u128;
static const int MAXW = 100;
static const int D = 20;
struct BigInt {
u64 d[MAXW] = {};
int sz = 0;
void clear() { fill(d, d + MAXW, 0ull); sz = 0; }
bool zero() const {
for (int i = 0; i < sz; i++) if (d[i]) return false;
return true;
}
void load(const string& s, const vector<int>& mask) {
clear();
int n = (int)s.size();
sz = (n + 63) / 64;
for (int i = 0; i < n; i++) {
int b = (s[i] - '0') ^ mask[i];
d[i / 64] |= (u64)b << (i % 64);
}
while (sz > 0 && !d[sz - 1]) sz--;
}
string dump(int n, const vector<int>& mask) const {
string r(n, '0');
for (int i = 0; i < n; i++) {
int b = (i / 64 < sz) ? (d[i / 64] >> (i % 64)) & 1 : 0;
r[i] = '0' + (b ^ mask[i]);
}
return r;
}
int pop_trit() {
u64 rem = 0;
for (int i = sz - 1; i >= 0; i--) {
u128 v = ((u128)rem << 64) | d[i];
d[i] = (u64)(v / 3); rem = (u64)(v % 3);
}
while (sz > 0 && !d[sz - 1]) sz--;
return (int)rem;
}
int pop_bit() {
if (!sz) return 0;
int b = d[0] & 1;
for (int i = 0; i < sz - 1; i++) d[i] = (d[i] >> 1) | (d[i + 1] << 63);
d[sz - 1] >>= 1;
while (sz > 0 && !d[sz - 1]) sz--;
return b;
}
int peek(int pos) const {
int w = pos / 64, b = pos % 64;
return (w < sz) ? (d[w] >> b) & 1 : 0;
}
bool ge_pow2(int n) const {
int w = n / 64, b = n % 64;
if (sz > w + 1) return true;
if (sz == w + 1) return d[w] >= ((u64)1 << b);
return false;
}
void mul_small(int k) {
u128 carry = 0;
for (int i = 0; i < sz; i++) {
u128 v = (u128)d[i] * k + carry;
d[i] = (u64)v; carry = v >> 64;
}
if (carry && sz < MAXW) d[sz++] = (u64)carry;
}
void add_mul(int digit, const BigInt& w) {
if (!digit) return;
u128 carry = 0;
int top = max(sz, w.sz);
for (int i = 0; i < top || carry; i++) {
if (i >= MAXW) break;
u64 av = (i < sz) ? d[i] : 0;
u64 wv = (i < w.sz) ? w.d[i] : 0;
u128 s = (u128)av + (u128)wv * digit + carry;
d[i] = (u64)s;
if (i >= sz) sz = i + 1;
carry = s >> 64;
}
}
};
vector<int> make_mask(int n) {
mt19937 rng(0xDEADBEEFu);
vector<int> mask(n);
for (int& x : mask) x = rng() & 1;
return mask;
}
inline char mv2c(int m) { return "PKN"[m]; }
inline int c2mv(char c) { return c == 'P' ? 0 : c == 'K' ? 1 : 2; }
inline int rps_result(int a, int b) {
if (a == b) return 0;
return ((b - a + 3) % 3 == 1) ? +1 : -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string role; cin >> role;
bool alg = (role == "Algosia");
int n, t; cin >> n >> t;
while (t--) {
string s; cin >> s;
auto mask = make_mask(n);
BigInt M; M.load(s, mask);
BigInt R; R.clear();
BigInt W; W.clear(); W.sz = 1; W.d[0] = 1;
BigInt S; S.clear(); S.sz = 1; S.d[0] = 1;
int diff = 0;
int consec = 0;
bool my_pend = false;
int my_pend_trit = 0;
bool opp_pend = false;
bool i_done = false;
bool opp_done = false;
for (int rnd = 0; rnd < 20000; rnd++) {
bool my_trail = alg ? (diff < 0) : (diff > 0);
bool my_lead = alg ? (diff > 0) : (diff < 0);
int my_move;
if (diff == 0) {
consec = 0;
if (my_pend) {
my_move = my_pend_trit;
my_pend = false;
S.mul_small(4);
i_done = S.ge_pow2(n);
} else if (!i_done) {
int trit = M.pop_trit();
my_move = trit;
S.mul_small(3);
i_done = S.ge_pow2(n);
} else {
my_move = 1;
}
} else {
consec++;
if (my_lead) {
my_move = 1;
} else {
bool forced = (consec >= D) || i_done;
if (forced) {
my_move = 0;
} else {
int b0 = M.peek(0), b1 = M.peek(1);
if (b0 == 1 && b1 == 1) {
my_move = 1;
M.pop_bit(); M.pop_bit();
S.mul_small(4);
i_done = S.ge_pow2(n);
} else {
my_move = 0;
int v0 = M.pop_bit(), v1 = M.pop_bit();
my_pend = true;
my_pend_trit = (v1 << 1) | v0;
}
}
}
}
cout << mv2c(my_move) << '\n' << flush;
char c; cin >> c;
int opp_move = c2mv(c);
int diff_before = diff;
bool opp_trail_before = alg ? (diff > 0) : (diff < 0);
diff += rps_result(alg ? my_move : opp_move,
alg ? opp_move : my_move);
if (!opp_done) {
if (diff_before == 0) {
if (opp_pend) {
int bit0 = opp_move & 1;
int bit1 = (opp_move >> 1) & 1;
R.add_mul(bit0, W); W.mul_small(2);
R.add_mul(bit1, W); W.mul_small(2);
opp_pend = false;
} else {
R.add_mul(opp_move, W); W.mul_small(3);
}
} else {
bool forced = (consec >= D);
if (opp_trail_before && !forced) {
if (opp_move == 1) {
R.add_mul(1, W); W.mul_small(2);
R.add_mul(1, W); W.mul_small(2);
} else {
opp_pend = true;
}
}
}
if (W.ge_pow2(n)) opp_done = true;
}
if (i_done && opp_done) break;
}
cout << "! " << R.dump(n, mask) << '\n' << 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 | #include <algorithm> #include <cstring> #include <iostream> #include <random> #include <string> #include <vector> using namespace std; typedef uint64_t u64; typedef unsigned __int128 u128; static const int MAXW = 100; static const int D = 20; struct BigInt { u64 d[MAXW] = {}; int sz = 0; void clear() { fill(d, d + MAXW, 0ull); sz = 0; } bool zero() const { for (int i = 0; i < sz; i++) if (d[i]) return false; return true; } void load(const string& s, const vector<int>& mask) { clear(); int n = (int)s.size(); sz = (n + 63) / 64; for (int i = 0; i < n; i++) { int b = (s[i] - '0') ^ mask[i]; d[i / 64] |= (u64)b << (i % 64); } while (sz > 0 && !d[sz - 1]) sz--; } string dump(int n, const vector<int>& mask) const { string r(n, '0'); for (int i = 0; i < n; i++) { int b = (i / 64 < sz) ? (d[i / 64] >> (i % 64)) & 1 : 0; r[i] = '0' + (b ^ mask[i]); } return r; } int pop_trit() { u64 rem = 0; for (int i = sz - 1; i >= 0; i--) { u128 v = ((u128)rem << 64) | d[i]; d[i] = (u64)(v / 3); rem = (u64)(v % 3); } while (sz > 0 && !d[sz - 1]) sz--; return (int)rem; } int pop_bit() { if (!sz) return 0; int b = d[0] & 1; for (int i = 0; i < sz - 1; i++) d[i] = (d[i] >> 1) | (d[i + 1] << 63); d[sz - 1] >>= 1; while (sz > 0 && !d[sz - 1]) sz--; return b; } int peek(int pos) const { int w = pos / 64, b = pos % 64; return (w < sz) ? (d[w] >> b) & 1 : 0; } bool ge_pow2(int n) const { int w = n / 64, b = n % 64; if (sz > w + 1) return true; if (sz == w + 1) return d[w] >= ((u64)1 << b); return false; } void mul_small(int k) { u128 carry = 0; for (int i = 0; i < sz; i++) { u128 v = (u128)d[i] * k + carry; d[i] = (u64)v; carry = v >> 64; } if (carry && sz < MAXW) d[sz++] = (u64)carry; } void add_mul(int digit, const BigInt& w) { if (!digit) return; u128 carry = 0; int top = max(sz, w.sz); for (int i = 0; i < top || carry; i++) { if (i >= MAXW) break; u64 av = (i < sz) ? d[i] : 0; u64 wv = (i < w.sz) ? w.d[i] : 0; u128 s = (u128)av + (u128)wv * digit + carry; d[i] = (u64)s; if (i >= sz) sz = i + 1; carry = s >> 64; } } }; vector<int> make_mask(int n) { mt19937 rng(0xDEADBEEFu); vector<int> mask(n); for (int& x : mask) x = rng() & 1; return mask; } inline char mv2c(int m) { return "PKN"[m]; } inline int c2mv(char c) { return c == 'P' ? 0 : c == 'K' ? 1 : 2; } inline int rps_result(int a, int b) { if (a == b) return 0; return ((b - a + 3) % 3 == 1) ? +1 : -1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string role; cin >> role; bool alg = (role == "Algosia"); int n, t; cin >> n >> t; while (t--) { string s; cin >> s; auto mask = make_mask(n); BigInt M; M.load(s, mask); BigInt R; R.clear(); BigInt W; W.clear(); W.sz = 1; W.d[0] = 1; BigInt S; S.clear(); S.sz = 1; S.d[0] = 1; int diff = 0; int consec = 0; bool my_pend = false; int my_pend_trit = 0; bool opp_pend = false; bool i_done = false; bool opp_done = false; for (int rnd = 0; rnd < 20000; rnd++) { bool my_trail = alg ? (diff < 0) : (diff > 0); bool my_lead = alg ? (diff > 0) : (diff < 0); int my_move; if (diff == 0) { consec = 0; if (my_pend) { my_move = my_pend_trit; my_pend = false; S.mul_small(4); i_done = S.ge_pow2(n); } else if (!i_done) { int trit = M.pop_trit(); my_move = trit; S.mul_small(3); i_done = S.ge_pow2(n); } else { my_move = 1; } } else { consec++; if (my_lead) { my_move = 1; } else { bool forced = (consec >= D) || i_done; if (forced) { my_move = 0; } else { int b0 = M.peek(0), b1 = M.peek(1); if (b0 == 1 && b1 == 1) { my_move = 1; M.pop_bit(); M.pop_bit(); S.mul_small(4); i_done = S.ge_pow2(n); } else { my_move = 0; int v0 = M.pop_bit(), v1 = M.pop_bit(); my_pend = true; my_pend_trit = (v1 << 1) | v0; } } } } cout << mv2c(my_move) << '\n' << flush; char c; cin >> c; int opp_move = c2mv(c); int diff_before = diff; bool opp_trail_before = alg ? (diff > 0) : (diff < 0); diff += rps_result(alg ? my_move : opp_move, alg ? opp_move : my_move); if (!opp_done) { if (diff_before == 0) { if (opp_pend) { int bit0 = opp_move & 1; int bit1 = (opp_move >> 1) & 1; R.add_mul(bit0, W); W.mul_small(2); R.add_mul(bit1, W); W.mul_small(2); opp_pend = false; } else { R.add_mul(opp_move, W); W.mul_small(3); } } else { bool forced = (consec >= D); if (opp_trail_before && !forced) { if (opp_move == 1) { R.add_mul(1, W); W.mul_small(2); R.add_mul(1, W); W.mul_small(2); } else { opp_pend = true; } } } if (W.ge_pow2(n)) opp_done = true; } if (i_done && opp_done) break; } cout << "! " << R.dump(n, mask) << '\n' << flush; } return 0; } |
English