#include <bits/stdc++.h>
using namespace std;
const int N = 5000;
const int K = 3157;
const string MOVES = "PKN";
vector<int> conv23(vector<int> bits) {
vector<int> res(K);
auto mul = [&] () {
for (auto& x: res) x *= 2;
for (int i = 0; i < K; i++) {
if (i+1 < K) res[i+1] += res[i] / 3;
res[i] %= 3;
}
};
auto add = [&] (int x) {
res[0] += x;
for (int i = 0; i < K; i++) {
if (res[i] < 3) break;
if (i+1 < K) res[i+1] += res[i] / 3;
res[i] %= 3;
}
};
reverse(bits.begin(), bits.end());
for (int i = 0; i < N; i++) {
mul();
add(bits[i]);
}
return res;
}
vector<int> conv32(vector<int> trits) {
vector<int> res(N);
auto mul = [&] () {
for (auto& x: res) x *= 3;
for (int i = 0; i < N; i++) {
if (i+1 < N) res[i+1] += res[i] / 2;
res[i] %= 2;
}
};
auto add = [&] (int x) {
res[0] += x;
for (int i = 0; i < N; i++) {
if (res[i] < 2) break;
if (i+1 < N) res[i+1] += res[i] / 2;
res[i] %= 2;
}
};
reverse(trits.begin(), trits.end());
for (int i = 0; i < K; i++) {
mul();
add(trits[i]);
}
return res;
};
int64_t R() {
// https://projecteuler.net/problem=803
static int64_t mul = 25214903917;
static int64_t last_ans = 0;
last_ans = (last_ans * mul + 11) % (1LL << 48);
return last_ans;
}
int main () {
ios_base::sync_with_stdio(0); cin.tie(0);
string player;
cin >> player;
const bool A = (player[0] == 'A');
int n, T;
cin >> n >> T;
while (T--) {
string s;
cin >> s;
vector<int> bits(N);
for (int i = 0; i < n; i++) bits[i] = (s[i] == '1');
vector<int> trits = conv23(bits);
vector<int> scrambles(2*K);
for (int i = 0; i < 2*K; i++) scrambles[i] = abs(R()) % 3;
if (A) for (int i = 0; i < K; i++) trits[i] = (trits[i] + scrambles[i]) % 3;
else for (int i = 0; i < K; i++) trits[i] = (trits[i] + scrambles[i+K]) % 3;
int score = 0;
auto play = [&] (int x) {
cout << MOVES[x] << endl;
char c;
cin >> c;
int other = find(MOVES.begin(), MOVES.end(), c) - MOVES.begin();
if (x == other) return other;
if (other == (x+1) % 3) score += (A ? 1 : -1);
else score += (A ? -1 : 1);
return other;
};
vector<int> other_trits(K);
int my_idx = 0, other_idx = 0;
while (my_idx < K || other_idx < K) {
if (score == 0) {
int res = play(my_idx >= K ? 0 : trits[my_idx]);
if (other_idx < K) other_trits[other_idx++] = res;
if (my_idx < K) my_idx++;
}
else {
int base_play = ((score == 1)^A ? 0 : 1);
bool igo = (my_idx < other_idx || (my_idx == other_idx && A));
if (igo) {
if (my_idx+1 < K && trits[my_idx] == 0 && trits[my_idx+1] == 0) {
play(base_play^1);
my_idx += 2;
}
else play(base_play);
}
else {
int res = play(base_play);
if (res == base_play) {
other_trits[other_idx++] = 0;
other_trits[other_idx++] = 0;
}
}
}
}
if (A) for (int i = 0; i < K; i++) other_trits[i] = (other_trits[i] - scrambles[i+K] + 3) % 3;
else for (int i = 0; i < K; i++) other_trits[i] = (other_trits[i] - scrambles[i] + 3) % 3;
vector<int> other_bits = conv32(other_trits);
cout << "! ";
for (int i = 0; i < n; i++) cout << other_bits[i];
cout << endl;
}
}
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 | #include <bits/stdc++.h> using namespace std; const int N = 5000; const int K = 3157; const string MOVES = "PKN"; vector<int> conv23(vector<int> bits) { vector<int> res(K); auto mul = [&] () { for (auto& x: res) x *= 2; for (int i = 0; i < K; i++) { if (i+1 < K) res[i+1] += res[i] / 3; res[i] %= 3; } }; auto add = [&] (int x) { res[0] += x; for (int i = 0; i < K; i++) { if (res[i] < 3) break; if (i+1 < K) res[i+1] += res[i] / 3; res[i] %= 3; } }; reverse(bits.begin(), bits.end()); for (int i = 0; i < N; i++) { mul(); add(bits[i]); } return res; } vector<int> conv32(vector<int> trits) { vector<int> res(N); auto mul = [&] () { for (auto& x: res) x *= 3; for (int i = 0; i < N; i++) { if (i+1 < N) res[i+1] += res[i] / 2; res[i] %= 2; } }; auto add = [&] (int x) { res[0] += x; for (int i = 0; i < N; i++) { if (res[i] < 2) break; if (i+1 < N) res[i+1] += res[i] / 2; res[i] %= 2; } }; reverse(trits.begin(), trits.end()); for (int i = 0; i < K; i++) { mul(); add(trits[i]); } return res; }; int64_t R() { // https://projecteuler.net/problem=803 static int64_t mul = 25214903917; static int64_t last_ans = 0; last_ans = (last_ans * mul + 11) % (1LL << 48); return last_ans; } int main () { ios_base::sync_with_stdio(0); cin.tie(0); string player; cin >> player; const bool A = (player[0] == 'A'); int n, T; cin >> n >> T; while (T--) { string s; cin >> s; vector<int> bits(N); for (int i = 0; i < n; i++) bits[i] = (s[i] == '1'); vector<int> trits = conv23(bits); vector<int> scrambles(2*K); for (int i = 0; i < 2*K; i++) scrambles[i] = abs(R()) % 3; if (A) for (int i = 0; i < K; i++) trits[i] = (trits[i] + scrambles[i]) % 3; else for (int i = 0; i < K; i++) trits[i] = (trits[i] + scrambles[i+K]) % 3; int score = 0; auto play = [&] (int x) { cout << MOVES[x] << endl; char c; cin >> c; int other = find(MOVES.begin(), MOVES.end(), c) - MOVES.begin(); if (x == other) return other; if (other == (x+1) % 3) score += (A ? 1 : -1); else score += (A ? -1 : 1); return other; }; vector<int> other_trits(K); int my_idx = 0, other_idx = 0; while (my_idx < K || other_idx < K) { if (score == 0) { int res = play(my_idx >= K ? 0 : trits[my_idx]); if (other_idx < K) other_trits[other_idx++] = res; if (my_idx < K) my_idx++; } else { int base_play = ((score == 1)^A ? 0 : 1); bool igo = (my_idx < other_idx || (my_idx == other_idx && A)); if (igo) { if (my_idx+1 < K && trits[my_idx] == 0 && trits[my_idx+1] == 0) { play(base_play^1); my_idx += 2; } else play(base_play); } else { int res = play(base_play); if (res == base_play) { other_trits[other_idx++] = 0; other_trits[other_idx++] = 0; } } } } if (A) for (int i = 0; i < K; i++) other_trits[i] = (other_trits[i] - scrambles[i+K] + 3) % 3; else for (int i = 0; i < K; i++) other_trits[i] = (other_trits[i] - scrambles[i] + 3) % 3; vector<int> other_bits = conv32(other_trits); cout << "! "; for (int i = 0; i < n; i++) cout << other_bits[i]; cout << endl; } } |
English