#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define RFOR(i,p,n) for(int i=(p);i>=(n);--i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define ssize(x) int((x).size())
#define fi first
#define se second
#define V vector
#define pb push_back
#define eb emplace_back
#define C const
#define gc getchar_unlocked
#define pc putchar_unlocked
#define pn putchar_unlocked('\n')
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128_t lll;
typedef V<int> vi;
typedef V<ll> vll;
typedef const int ci;
typedef const ll cll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
void chmin(auto &a, auto b){a=min(a,b);}
void chmax(auto &a, auto b){a=max(a,b);}
ci inf = 2.1e9;
cll infll = 4.5e18;
int I(){
int z;
int c = gc();
while(c < '0' || c > '9') c = gc();
for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0';
return z;
}
ci grupa = 50;
cll podstawa = 1LL<<grupa;
struct liczba{
vll t;
liczba(ll x){
t.eb(x);
}
void wez(ll razy, ll dod){
for(ll &x : t) x *= razy;
t[0] += dod;
REP(i, ssize(t)){
cll xd = t[i]/podstawa;
t[i] %= podstawa;
if(xd){
if(i+1 < ssize(t)) t[i+1] += xd;
else t.eb(xd);
}
}
}
ll daj(ll dziel){
ll resz = 0;
RFOR(i, ssize(t)-1, 0){
t[i] += resz*podstawa;
resz = t[i]%dziel;
t[i] /= dziel;
}
return resz;
}
ll mod(ll dziel){
ll resz = 0;
RFOR(i, ssize(t)-1, 0){
resz = (resz*podstawa+t[i])%dziel;
}
return resz;
}
int ile_bitow(){
RFOR(i, ssize(t)-1, 0) if(t[i]) return grupa*i + bit_width(ull(t[i]))-1;
return -1;
}
};
C vi kodowanie = {'P', 'K', 'N'};
int przewaga;
int wyslij_i_odbierz(ci ja){
pc(kodowanie[ja]);
pn;
fflush(stdout);
ci c = gc();
assert(gc() == '\n');
int oni = -1;
REP(i, 3) if(kodowanie[i] == c) oni = i;
if(oni == (ja+1)%3) ++przewaga;
if(ja == (oni+1)%3) --przewaga;
return oni;
}
mt19937 mt(694202137^233665);
int los(int a, int b){
uniform_int_distribution<int> dist(a, b);
return dist(mt);
}
void answer(ci n){
vi ja(n);
REP(i, n) ja[i] = gc()-'0';
assert(gc() == '\n');
vi hasz(n);
REP(i, n) hasz[i] = los(0, 1);
REP(i, n) ja[i] ^= hasz[i];
liczba ja_licz(0);
RFOR(i, n-1, 0) ja_licz.wez(2, ja[i]);
liczba mozliwosci_ja(1);
auto pobierz = [&](int dziel){
mozliwosci_ja.wez(dziel, 0);
return int(ja_licz.daj(dziel));
};
V<pii> vec;
liczba mozliwosci_oni(1);
auto odbierz = [&](int razy, int dod){
mozliwosci_oni.wez(razy, 0);
vec.eb(razy, dod);
};
bool jeszcze_odbieram = false;
bool jeszcze_wysylam = false;
while(mozliwosci_ja.ile_bitow() < n || mozliwosci_oni.ile_bitow() < n){
if(przewaga == 0){
odbierz(3+jeszcze_odbieram, wyslij_i_odbierz(pobierz(3+jeszcze_wysylam)));
jeszcze_odbieram = false;
jeszcze_wysylam = false;
}
else if(przewaga == 1){
if(wyslij_i_odbierz(1) == 1){
odbierz(4, 3);
}
else{
jeszcze_odbieram = true;
}
}
else{
if(ja_licz.mod(4) == 3){
pobierz(4);
wyslij_i_odbierz(1);
}
else{
wyslij_i_odbierz(0);
jeszcze_wysylam = true;
}
}
}
liczba oni_licz(0);
reverse(all(vec));
for(auto [mod, resz] : vec) oni_licz.wez(mod, resz);
vi oni(n);
REP(i, n) oni[i] = int(oni_licz.daj(2));
REP(i, n) oni[i] ^= hasz[i];
pc('!');
pc(' ');
REP(i, n) pc('0'+oni[i]);
pn;
fflush(stdout);
}
int main(){
[[maybe_unused]] ci kim_jestem = gc()-'A';
ci n = I();
ci t = I();
REP(_, t){
przewaga = 0;
answer(n);
}
}
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 | #include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define RFOR(i,p,n) for(int i=(p);i>=(n);--i) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define ssize(x) int((x).size()) #define fi first #define se second #define V vector #define pb push_back #define eb emplace_back #define C const #define gc getchar_unlocked #define pc putchar_unlocked #define pn putchar_unlocked('\n') using namespace std; typedef long long ll; typedef unsigned long long ull; typedef __int128_t lll; typedef V<int> vi; typedef V<ll> vll; typedef const int ci; typedef const ll cll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; void chmin(auto &a, auto b){a=min(a,b);} void chmax(auto &a, auto b){a=max(a,b);} ci inf = 2.1e9; cll infll = 4.5e18; int I(){ int z; int c = gc(); while(c < '0' || c > '9') c = gc(); for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0'; return z; } ci grupa = 50; cll podstawa = 1LL<<grupa; struct liczba{ vll t; liczba(ll x){ t.eb(x); } void wez(ll razy, ll dod){ for(ll &x : t) x *= razy; t[0] += dod; REP(i, ssize(t)){ cll xd = t[i]/podstawa; t[i] %= podstawa; if(xd){ if(i+1 < ssize(t)) t[i+1] += xd; else t.eb(xd); } } } ll daj(ll dziel){ ll resz = 0; RFOR(i, ssize(t)-1, 0){ t[i] += resz*podstawa; resz = t[i]%dziel; t[i] /= dziel; } return resz; } ll mod(ll dziel){ ll resz = 0; RFOR(i, ssize(t)-1, 0){ resz = (resz*podstawa+t[i])%dziel; } return resz; } int ile_bitow(){ RFOR(i, ssize(t)-1, 0) if(t[i]) return grupa*i + bit_width(ull(t[i]))-1; return -1; } }; C vi kodowanie = {'P', 'K', 'N'}; int przewaga; int wyslij_i_odbierz(ci ja){ pc(kodowanie[ja]); pn; fflush(stdout); ci c = gc(); assert(gc() == '\n'); int oni = -1; REP(i, 3) if(kodowanie[i] == c) oni = i; if(oni == (ja+1)%3) ++przewaga; if(ja == (oni+1)%3) --przewaga; return oni; } mt19937 mt(694202137^233665); int los(int a, int b){ uniform_int_distribution<int> dist(a, b); return dist(mt); } void answer(ci n){ vi ja(n); REP(i, n) ja[i] = gc()-'0'; assert(gc() == '\n'); vi hasz(n); REP(i, n) hasz[i] = los(0, 1); REP(i, n) ja[i] ^= hasz[i]; liczba ja_licz(0); RFOR(i, n-1, 0) ja_licz.wez(2, ja[i]); liczba mozliwosci_ja(1); auto pobierz = [&](int dziel){ mozliwosci_ja.wez(dziel, 0); return int(ja_licz.daj(dziel)); }; V<pii> vec; liczba mozliwosci_oni(1); auto odbierz = [&](int razy, int dod){ mozliwosci_oni.wez(razy, 0); vec.eb(razy, dod); }; bool jeszcze_odbieram = false; bool jeszcze_wysylam = false; while(mozliwosci_ja.ile_bitow() < n || mozliwosci_oni.ile_bitow() < n){ if(przewaga == 0){ odbierz(3+jeszcze_odbieram, wyslij_i_odbierz(pobierz(3+jeszcze_wysylam))); jeszcze_odbieram = false; jeszcze_wysylam = false; } else if(przewaga == 1){ if(wyslij_i_odbierz(1) == 1){ odbierz(4, 3); } else{ jeszcze_odbieram = true; } } else{ if(ja_licz.mod(4) == 3){ pobierz(4); wyslij_i_odbierz(1); } else{ wyslij_i_odbierz(0); jeszcze_wysylam = true; } } } liczba oni_licz(0); reverse(all(vec)); for(auto [mod, resz] : vec) oni_licz.wez(mod, resz); vi oni(n); REP(i, n) oni[i] = int(oni_licz.daj(2)); REP(i, n) oni[i] ^= hasz[i]; pc('!'); pc(' '); REP(i, n) pc('0'+oni[i]); pn; fflush(stdout); } int main(){ [[maybe_unused]] ci kim_jestem = gc()-'A'; ci n = I(); ci t = I(); REP(_, t){ przewaga = 0; answer(n); } } |
English