#include <bits/stdc++.h>
#include <ostream>
#include <random>
using namespace std;
#ifdef DEBUG
auto &operator<<(auto &o, pair<auto, auto> p) {
return o << "(" << p.first << ", " << p.second <<")";
}
auto operator<<(auto &o, auto x)-> decltype(x.end(), o) {
o << "{";int i = 0;
for(auto e : x) o << ", "+!i++<<e;
return o <<"}";
}
#define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x)
#else
#define debug(...) {}
#endif
#define int long long
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define fi first
#define se second
typedef pair <int, int> pii;
#define TWO_BLOCK_SIZE 84
int T[128] = {0,1,2,2,3,4,4,5,6,6,7,7,8,9,9,10,11,11,12,12,13,14,14,15,16,16,17,18,18,19,19,20,21,21,22,23,23,24,24,25,26,26,27,28,28,29,30,30,31,31,32,33,33,34,35,35,36,36,37,38,38,39,40,40,41,42,42,43,43,44,45,45,46,47,47,48,48,49,50,50,51,52,52,53,53,54,55,55,56,57,57,58,59,59,60,60,61,62,62,63,64,64,65,65,66,67,67,68,69,69,70,71,71,72,72,73,74,74,75,76,76,77,77,78,79,79,80,80};
#define MAGIC 3
__int128 string2int(string s, int l) {
for (int i=sz(s); i<sz(s); ++i) {
s += '0';
}
__int128 ret = 0;
for (int i=0; i<l; ++i) {
int val = s[i] - '0';
ret <<= 1;
ret += val;
}
return ret;
}
string int2string(__int128 x, int l) {
string ret = "";
for (int i=0; i<l; ++i) {
int val = x & 1;
x >>= 1;
char z = val + '0';
ret += z;
}
reverse(all(ret));
return ret;
}
__int128 string3int(string s, int l) {
__int128 ret = 0;
for (int i=0; i<l; ++i) {
int val = s[i] - '0';
ret = (ret*3);
ret += val;
}
return ret;
}
string int3string(__int128 x, int l) {
string ret = "";
for (int i=0; i<l; ++i) {
int val = x % 3;
x /= 3;
char z = val + '0';
ret += z;
}
reverse(all(ret));
return ret;
}
string xor_string(string a, string b) {
string c = "";
for (int i=0; i<sz(a); ++i) {
if (a[i] == b[i]) {
c += '0';
}
else {
c += '1';
}
}
return c;
}
char trl(char c) {
switch (c) {
case '0':
return 'P';
case '1':
return 'K';
case '2':
return 'N';
case 'P':
return '0';
case 'K':
return '1';
}
return '2';
}
bool won(char m, char y) {
if (m == 'P' && y == 'K') return true;
if (m == 'N' && y == 'P') return true;
if (m == 'K' && y == 'N') return true;
return false;
}
pair<string, int> send_c(char c, int magic) {
char mt = trl(c);
cout<<mt<<endl;
char yt; cin>>yt;
if (mt == yt) {
string s = "a";
s[0] = trl(yt);
return {s, 1};
}
if (won(mt, yt)) {
if (magic) {
cout<<'N'<<endl;
}
else {
cout<<'P'<<endl;
}
}
else {
cout<<'N'<<endl;
}
char x; cin>>x;
if (won(mt, yt)) {
if (magic) {
cout<<'P'<<endl;
cin>>x;
string s = "a";
s[0] = trl(yt);
return {s, MAGIC};
}
else {
string s = "a";
s[0] = trl(yt);
return {s, 1};
}
}
else {
if (x == 'N') { // magic
cout<<'N'<<endl;
cin>>x;
string s = "";
for (int i=0; i<MAGIC; ++i) {
s += trl(yt);
}
return {s, 1};
}
else { // not magic
string s = "a";
s[0] = trl(yt);
return {s, 1};
}
}
}
void test(string rola, int n) {
debug("TC______________________");
string s; cin>>s;
debug(s);
// mt19937 rng(534992827);
mt19937 rng(2004012821);
string x = "";
map <int, int> bin_len;
for (int i=0; i<n; ++i) {
int a = rng() & 1;
if (a) {
x += '0';
}
else {
x += '1';
}
}
debug(x);
s = xor_string(x, s);
debug(s);
string tern = "";
for (int i=0; i<n; i+=TWO_BLOCK_SIZE) {
int rem = min(n - i, (int)TWO_BLOCK_SIZE);
bin_len[sz(tern)] = rem;
string block = s.substr(i, rem);
__int128 val = string2int(block, rem);
debug(block);
string ts = int3string(val, T[rem]);
tern += ts;
}
int org_len = sz(tern);
vector <int> M(sz(tern));
for (int i=0; i<sz(tern)-MAGIC; i++) {
int same = 1;
for (int j=1; j<MAGIC; ++j) {
if (tern[i] != tern[i+j]) {
same = 0;
}
}
M[i] = same;
}
for (int i=0; i<org_len; ++i) {
char x = rand()%3;
tern += (x + '0');
}
string ytern = "";
// tu przesylam
int i=0, j=0;
while (i<org_len || j < org_len) {
auto [s, ip] = send_c(tern[i], M[i]);
ytern += s;
i += ip;
j += sz(s);
}
ytern = ytern.substr(0, org_len);
string res = "";
// tu odkodowuje blokami
for (int i=0; i<org_len; i+=T[TWO_BLOCK_SIZE]) {
int rem = min(org_len - i, T[TWO_BLOCK_SIZE]);
string block = ytern.substr(i, rem);
unsigned __int128 tval = string3int(block, rem);
debug(i, bin_len[i]);
string tbin = int2string(tval, bin_len[i]);
debug(tbin);
res += tbin;
}
res = res.substr(0, n);
res = xor_string(x, res);
cout<<"! "<<res<<endl;
}
signed main() {
// ios_base::sync_with_stdio(0); cin.tie(0);
string rola; cin>>rola;
int t = 1, n; cin>>n>>t;
while (t--) {
test(rola, 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 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 | #include <bits/stdc++.h> #include <ostream> #include <random> using namespace std; #ifdef DEBUG auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif #define int long long #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define pb push_back #define fi first #define se second typedef pair <int, int> pii; #define TWO_BLOCK_SIZE 84 int T[128] = {0,1,2,2,3,4,4,5,6,6,7,7,8,9,9,10,11,11,12,12,13,14,14,15,16,16,17,18,18,19,19,20,21,21,22,23,23,24,24,25,26,26,27,28,28,29,30,30,31,31,32,33,33,34,35,35,36,36,37,38,38,39,40,40,41,42,42,43,43,44,45,45,46,47,47,48,48,49,50,50,51,52,52,53,53,54,55,55,56,57,57,58,59,59,60,60,61,62,62,63,64,64,65,65,66,67,67,68,69,69,70,71,71,72,72,73,74,74,75,76,76,77,77,78,79,79,80,80}; #define MAGIC 3 __int128 string2int(string s, int l) { for (int i=sz(s); i<sz(s); ++i) { s += '0'; } __int128 ret = 0; for (int i=0; i<l; ++i) { int val = s[i] - '0'; ret <<= 1; ret += val; } return ret; } string int2string(__int128 x, int l) { string ret = ""; for (int i=0; i<l; ++i) { int val = x & 1; x >>= 1; char z = val + '0'; ret += z; } reverse(all(ret)); return ret; } __int128 string3int(string s, int l) { __int128 ret = 0; for (int i=0; i<l; ++i) { int val = s[i] - '0'; ret = (ret*3); ret += val; } return ret; } string int3string(__int128 x, int l) { string ret = ""; for (int i=0; i<l; ++i) { int val = x % 3; x /= 3; char z = val + '0'; ret += z; } reverse(all(ret)); return ret; } string xor_string(string a, string b) { string c = ""; for (int i=0; i<sz(a); ++i) { if (a[i] == b[i]) { c += '0'; } else { c += '1'; } } return c; } char trl(char c) { switch (c) { case '0': return 'P'; case '1': return 'K'; case '2': return 'N'; case 'P': return '0'; case 'K': return '1'; } return '2'; } bool won(char m, char y) { if (m == 'P' && y == 'K') return true; if (m == 'N' && y == 'P') return true; if (m == 'K' && y == 'N') return true; return false; } pair<string, int> send_c(char c, int magic) { char mt = trl(c); cout<<mt<<endl; char yt; cin>>yt; if (mt == yt) { string s = "a"; s[0] = trl(yt); return {s, 1}; } if (won(mt, yt)) { if (magic) { cout<<'N'<<endl; } else { cout<<'P'<<endl; } } else { cout<<'N'<<endl; } char x; cin>>x; if (won(mt, yt)) { if (magic) { cout<<'P'<<endl; cin>>x; string s = "a"; s[0] = trl(yt); return {s, MAGIC}; } else { string s = "a"; s[0] = trl(yt); return {s, 1}; } } else { if (x == 'N') { // magic cout<<'N'<<endl; cin>>x; string s = ""; for (int i=0; i<MAGIC; ++i) { s += trl(yt); } return {s, 1}; } else { // not magic string s = "a"; s[0] = trl(yt); return {s, 1}; } } } void test(string rola, int n) { debug("TC______________________"); string s; cin>>s; debug(s); // mt19937 rng(534992827); mt19937 rng(2004012821); string x = ""; map <int, int> bin_len; for (int i=0; i<n; ++i) { int a = rng() & 1; if (a) { x += '0'; } else { x += '1'; } } debug(x); s = xor_string(x, s); debug(s); string tern = ""; for (int i=0; i<n; i+=TWO_BLOCK_SIZE) { int rem = min(n - i, (int)TWO_BLOCK_SIZE); bin_len[sz(tern)] = rem; string block = s.substr(i, rem); __int128 val = string2int(block, rem); debug(block); string ts = int3string(val, T[rem]); tern += ts; } int org_len = sz(tern); vector <int> M(sz(tern)); for (int i=0; i<sz(tern)-MAGIC; i++) { int same = 1; for (int j=1; j<MAGIC; ++j) { if (tern[i] != tern[i+j]) { same = 0; } } M[i] = same; } for (int i=0; i<org_len; ++i) { char x = rand()%3; tern += (x + '0'); } string ytern = ""; // tu przesylam int i=0, j=0; while (i<org_len || j < org_len) { auto [s, ip] = send_c(tern[i], M[i]); ytern += s; i += ip; j += sz(s); } ytern = ytern.substr(0, org_len); string res = ""; // tu odkodowuje blokami for (int i=0; i<org_len; i+=T[TWO_BLOCK_SIZE]) { int rem = min(org_len - i, T[TWO_BLOCK_SIZE]); string block = ytern.substr(i, rem); unsigned __int128 tval = string3int(block, rem); debug(i, bin_len[i]); string tbin = int2string(tval, bin_len[i]); debug(tbin); res += tbin; } res = res.substr(0, n); res = xor_string(x, res); cout<<"! "<<res<<endl; } signed main() { // ios_base::sync_with_stdio(0); cin.tie(0); string rola; cin>>rola; int t = 1, n; cin>>n>>t; while (t--) { test(rola, n); } } |
English