// {{{
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define each(...) for (auto& __VA_ARGS__)
#define rep(i, b, e) for (int i = (b); i <= (e); i++)
#define rev(i, b, e) for (int i = (e); i >= (b); i--)
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define tC template<class
#define $(x) #x<<'='<<(x)<<' '
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vb = vector<bool>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vpi = vector<pii>;
using vpl = vector<pll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
tC T, class C=greater<T>> using min_priority_queue = priority_queue<T,vector<T>,C>;
tC T> int sz(const T& a) { return (int)a.size(); }
tC T> bool amin(T& a, T b) { return b < a ? a = b, 1 : 0; }
tC T> bool amax(T& a, T b) { return b > a ? a = b, 1 : 0; }
const int oo = 1e9+1;
const ll OO = ll(1e18)+1;
auto now() { return chrono::high_resolution_clock::now().time_since_epoch().count(); }
mt19937 rnd(4488);
tC T> T rand(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); }
struct Debug {
#ifdef SPONGE
tC T>Debug operator<<(const T& x) { cerr<<"\033[1;33m"<<x<<"\033[0m"; return *this; }
#else
tC T>Debug operator<<(const T&) { return *this; }
#endif
} dbg;
namespace { void solve(int n,char imie); }
// }}}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.precision(15);
cout.setf(ios::fixed,ios::floatfield);
cerr.precision(15);
cerr.setf(ios::fixed,ios::floatfield);
int n,t;
string imie;
cin>>imie;
cin>>n>>t;
rep(i,1,t) solve(n,imie[0]);
}
namespace {
const int NUM_SIZE=100;
const int NUM_BITS=55;
const ll NUM_BASE=1ll<<NUM_BITS;
using num=array<ll,NUM_SIZE>;
enum { LT, EQ, GT };
num num_from_int(int x)
{
num a{};
a[0]=x;
return a;
}
const num ZERO=num_from_int(0);
const num ONE=num_from_int(1);
void add(num& a,const num& b)
{
rep(i,0,NUM_SIZE-1){
a[i]+=b[i];
if(a[i]>=NUM_BASE){
a[i]-=NUM_BASE;
a[i+1]++;
}
}
}
void sub(num& a,const num& b)
{
rep(i,0,NUM_SIZE-1){
if(a[i]>=b[i]){
a[i]-=b[i];
}
else{
a[i+1]--;
a[i]=NUM_BASE+a[i]-b[i];
}
}
}
void div(num& a,int x)
{
ll c=0;
rev(i,0,NUM_SIZE-1){
c=a[i]+(c%x)*NUM_BASE;
a[i]=c/x;
}
}
int cmp(const num& a,const num& b)
{
rev(i,0,sz(a)-1){
if(a[i]<b[i]) return LT;
else if(a[i]>b[i]) return GT;
}
return EQ;
}
num num_from_str(string s)
{
num a{};
rep(i,0,sz(s)-1) if(s[i]=='1') a[i/NUM_BITS]|=(1ll<<(i%NUM_BITS));
return a;
}
string str_from_num(num a,int n=-1)
{
string s(NUM_SIZE*NUM_BITS,'0');
rep(i,0,sz(s)-1) if(a[i/NUM_BITS] & (1ll<<(i%NUM_BITS))) s[i]='1';
if(n!=-1) s.resize(n);
return s;
}
const int K=4;
int zakoduj(num& poc,num& kon,num& wiad,int deg,bool& done)
{
num dlug=kon;
sub(dlug,poc);
if(cmp(dlug,num_from_int(K+1))==LT){
if(cmp(poc,wiad)==EQ){
done=true;
return 0;
}
else{
add(poc,ONE);
return 1;
}
}
num srod0=dlug;
div(srod0,K);
add(srod0,poc);
num xd=dlug;
div(xd,3);
num srod1=poc;
add(srod1,xd);
num srod2=srod1;
add(srod2,xd);
if(deg==2){
if(cmp(wiad,srod0)==LT){
kon=srod0;
return 0;
}
else{
poc=srod0;
return 1;
}
}
else if(deg==3){
if(cmp(wiad,srod1)==LT){
kon=srod1;
return 0;
}
else if(cmp(wiad,srod2)==LT){
poc=srod1;
kon=srod2;
return 1;
}
else{
poc=srod2;
return 2;
}
}
assert(false);
}
void odkoduj(num& poc,num& kon,int znak,int deg,bool& done)
{
num dlug=kon;
sub(dlug,poc);
if(cmp(dlug,num_from_int(K+1))==LT){
if(znak==0) done=true;
else add(poc,ONE);
return;
}
num srod0=dlug;
div(srod0,K);
add(srod0,poc);
num xd=dlug;
div(xd,3);
num srod1=poc;
add(srod1,xd);
num srod2=srod1;
add(srod2,xd);
if(deg==2){
if(znak==0) kon=srod0;
else poc=srod0;
}
else if(deg==3){
if(znak==0) kon=srod1;
else if(znak==1) { poc=srod1; kon=srod2; }
else poc=srod2;
}
}
bool czy_wygrywa(char x,char y)
{
return (x=='P' && y=='K') || (x=='K' && y=='N') || (x=='N' && y=='P');
}
int rundy=0;
int komunikacja(int moj_znak,string moja_paleta,string twoja_paleta,int& wynik_gry)
{
rundy++;
char moj_ruch=moja_paleta[moj_znak];
cout<<moj_ruch<<endl;
cout.flush();
string odebrane;
cin>>odebrane;
char twoj_ruch=odebrane[0];
int twoj_znak=-1;
rep(i,0,sz(twoja_paleta)-1) if(twoja_paleta[i]==twoj_ruch) twoj_znak=i;
if(czy_wygrywa(moj_ruch,twoj_ruch)) wynik_gry++;
else if(czy_wygrywa(twoj_ruch,moj_ruch)) wynik_gry--;
return twoj_znak;
}
const ll MOD=1'000'000'007;
void haszuj(string& wiad,char kto)
{
ll x=1,baza;
if(kto=='A') baza=37;
else baza=19;
rep(i,0,sz(wiad)-1){
x=baza*x%MOD;
if(x%2==1){
if(wiad[i]=='0') wiad[i]='1';
else wiad[i]='0';
}
}
}
void solve(int n,char kto)
{
string moj_sekret;
cin>>moj_sekret;
haszuj(moj_sekret,kto);
num moj_wiad=num_from_str(moj_sekret);
num moj_pocz{},moj_kon{};
num twoj_pocz{},twoj_kon{};
moj_kon[n/NUM_BITS]|=(1ll<<(n%NUM_BITS));
twoj_kon[n/NUM_BITS]|=(1ll<<(n%NUM_BITS));
bool moj_done=false,twoj_done=false;
int wynik_gry=0; // dla mnie
char kolejka='A';
while(!moj_done || !twoj_done){
if(wynik_gry==0){
int moj_znak=zakoduj(moj_pocz,moj_kon,moj_wiad,3,moj_done);
int twoj_znak=komunikacja(moj_znak,"PKN","PKN",wynik_gry);
odkoduj(twoj_pocz,twoj_kon,twoj_znak,3,twoj_done);
}
else{
if(kolejka==kto){
int moj_znak=zakoduj(moj_pocz,moj_kon,moj_wiad,2,moj_done);
komunikacja(moj_znak,(wynik_gry==1 ? "PK" : "PN"),"P",wynik_gry);
}
else{
int twoj_znak=komunikacja(0,"P",(wynik_gry==-1 ? "PK" : "PN"),wynik_gry);
odkoduj(twoj_pocz,twoj_kon,twoj_znak,2,twoj_done);
}
kolejka=(kolejka=='A' ? 'B' : 'A');
}
}
string twoj_sekret=str_from_num(twoj_pocz,n);
haszuj(twoj_sekret,kto == 'A' ? 'B' : 'A');
cout<<"! "<<twoj_sekret<<endl;
cout.flush();
/*
ofstream plik("out"s+kto,ios::app);
plik<<rundy<<endl;
plik.close();
rundy=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 324 325 326 327 328 329 330 331 332 333 334 335 | // {{{ #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define each(...) for (auto& __VA_ARGS__) #define rep(i, b, e) for (int i = (b); i <= (e); i++) #define rev(i, b, e) for (int i = (e); i >= (b); i--) #define mp make_pair #define mt make_tuple #define x first #define y second #define pb push_back #define all(x) (x).begin(), (x).end() #define endl '\n' #define tC template<class #define $(x) #x<<'='<<(x)<<' ' using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vb = vector<bool>; using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>; using vpi = vector<pii>; using vpl = vector<pll>; using vvi = vector<vi>; using vvl = vector<vl>; tC T, class C=greater<T>> using min_priority_queue = priority_queue<T,vector<T>,C>; tC T> int sz(const T& a) { return (int)a.size(); } tC T> bool amin(T& a, T b) { return b < a ? a = b, 1 : 0; } tC T> bool amax(T& a, T b) { return b > a ? a = b, 1 : 0; } const int oo = 1e9+1; const ll OO = ll(1e18)+1; auto now() { return chrono::high_resolution_clock::now().time_since_epoch().count(); } mt19937 rnd(4488); tC T> T rand(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); } struct Debug { #ifdef SPONGE tC T>Debug operator<<(const T& x) { cerr<<"\033[1;33m"<<x<<"\033[0m"; return *this; } #else tC T>Debug operator<<(const T&) { return *this; } #endif } dbg; namespace { void solve(int n,char imie); } // }}} int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(15); cout.setf(ios::fixed,ios::floatfield); cerr.precision(15); cerr.setf(ios::fixed,ios::floatfield); int n,t; string imie; cin>>imie; cin>>n>>t; rep(i,1,t) solve(n,imie[0]); } namespace { const int NUM_SIZE=100; const int NUM_BITS=55; const ll NUM_BASE=1ll<<NUM_BITS; using num=array<ll,NUM_SIZE>; enum { LT, EQ, GT }; num num_from_int(int x) { num a{}; a[0]=x; return a; } const num ZERO=num_from_int(0); const num ONE=num_from_int(1); void add(num& a,const num& b) { rep(i,0,NUM_SIZE-1){ a[i]+=b[i]; if(a[i]>=NUM_BASE){ a[i]-=NUM_BASE; a[i+1]++; } } } void sub(num& a,const num& b) { rep(i,0,NUM_SIZE-1){ if(a[i]>=b[i]){ a[i]-=b[i]; } else{ a[i+1]--; a[i]=NUM_BASE+a[i]-b[i]; } } } void div(num& a,int x) { ll c=0; rev(i,0,NUM_SIZE-1){ c=a[i]+(c%x)*NUM_BASE; a[i]=c/x; } } int cmp(const num& a,const num& b) { rev(i,0,sz(a)-1){ if(a[i]<b[i]) return LT; else if(a[i]>b[i]) return GT; } return EQ; } num num_from_str(string s) { num a{}; rep(i,0,sz(s)-1) if(s[i]=='1') a[i/NUM_BITS]|=(1ll<<(i%NUM_BITS)); return a; } string str_from_num(num a,int n=-1) { string s(NUM_SIZE*NUM_BITS,'0'); rep(i,0,sz(s)-1) if(a[i/NUM_BITS] & (1ll<<(i%NUM_BITS))) s[i]='1'; if(n!=-1) s.resize(n); return s; } const int K=4; int zakoduj(num& poc,num& kon,num& wiad,int deg,bool& done) { num dlug=kon; sub(dlug,poc); if(cmp(dlug,num_from_int(K+1))==LT){ if(cmp(poc,wiad)==EQ){ done=true; return 0; } else{ add(poc,ONE); return 1; } } num srod0=dlug; div(srod0,K); add(srod0,poc); num xd=dlug; div(xd,3); num srod1=poc; add(srod1,xd); num srod2=srod1; add(srod2,xd); if(deg==2){ if(cmp(wiad,srod0)==LT){ kon=srod0; return 0; } else{ poc=srod0; return 1; } } else if(deg==3){ if(cmp(wiad,srod1)==LT){ kon=srod1; return 0; } else if(cmp(wiad,srod2)==LT){ poc=srod1; kon=srod2; return 1; } else{ poc=srod2; return 2; } } assert(false); } void odkoduj(num& poc,num& kon,int znak,int deg,bool& done) { num dlug=kon; sub(dlug,poc); if(cmp(dlug,num_from_int(K+1))==LT){ if(znak==0) done=true; else add(poc,ONE); return; } num srod0=dlug; div(srod0,K); add(srod0,poc); num xd=dlug; div(xd,3); num srod1=poc; add(srod1,xd); num srod2=srod1; add(srod2,xd); if(deg==2){ if(znak==0) kon=srod0; else poc=srod0; } else if(deg==3){ if(znak==0) kon=srod1; else if(znak==1) { poc=srod1; kon=srod2; } else poc=srod2; } } bool czy_wygrywa(char x,char y) { return (x=='P' && y=='K') || (x=='K' && y=='N') || (x=='N' && y=='P'); } int rundy=0; int komunikacja(int moj_znak,string moja_paleta,string twoja_paleta,int& wynik_gry) { rundy++; char moj_ruch=moja_paleta[moj_znak]; cout<<moj_ruch<<endl; cout.flush(); string odebrane; cin>>odebrane; char twoj_ruch=odebrane[0]; int twoj_znak=-1; rep(i,0,sz(twoja_paleta)-1) if(twoja_paleta[i]==twoj_ruch) twoj_znak=i; if(czy_wygrywa(moj_ruch,twoj_ruch)) wynik_gry++; else if(czy_wygrywa(twoj_ruch,moj_ruch)) wynik_gry--; return twoj_znak; } const ll MOD=1'000'000'007; void haszuj(string& wiad,char kto) { ll x=1,baza; if(kto=='A') baza=37; else baza=19; rep(i,0,sz(wiad)-1){ x=baza*x%MOD; if(x%2==1){ if(wiad[i]=='0') wiad[i]='1'; else wiad[i]='0'; } } } void solve(int n,char kto) { string moj_sekret; cin>>moj_sekret; haszuj(moj_sekret,kto); num moj_wiad=num_from_str(moj_sekret); num moj_pocz{},moj_kon{}; num twoj_pocz{},twoj_kon{}; moj_kon[n/NUM_BITS]|=(1ll<<(n%NUM_BITS)); twoj_kon[n/NUM_BITS]|=(1ll<<(n%NUM_BITS)); bool moj_done=false,twoj_done=false; int wynik_gry=0; // dla mnie char kolejka='A'; while(!moj_done || !twoj_done){ if(wynik_gry==0){ int moj_znak=zakoduj(moj_pocz,moj_kon,moj_wiad,3,moj_done); int twoj_znak=komunikacja(moj_znak,"PKN","PKN",wynik_gry); odkoduj(twoj_pocz,twoj_kon,twoj_znak,3,twoj_done); } else{ if(kolejka==kto){ int moj_znak=zakoduj(moj_pocz,moj_kon,moj_wiad,2,moj_done); komunikacja(moj_znak,(wynik_gry==1 ? "PK" : "PN"),"P",wynik_gry); } else{ int twoj_znak=komunikacja(0,"P",(wynik_gry==-1 ? "PK" : "PN"),wynik_gry); odkoduj(twoj_pocz,twoj_kon,twoj_znak,2,twoj_done); } kolejka=(kolejka=='A' ? 'B' : 'A'); } } string twoj_sekret=str_from_num(twoj_pocz,n); haszuj(twoj_sekret,kto == 'A' ? 'B' : 'A'); cout<<"! "<<twoj_sekret<<endl; cout.flush(); /* ofstream plik("out"s+kto,ios::app); plik<<rundy<<endl; plik.close(); rundy=0; */ } } |
English