#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i=(a); i<(b); i++)
#define FORD(i, a, b) for (auto i=(a); i>(b); i--)
#define SZ(x) ((int)(x).size())
#define ITH_BIT(m, i) ((m)>>(i) & 1)
#define PPC(x) __builtin_popcount(x)
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = std::min(a, (T1)b); }
template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = std::max(a, (T1)b); }
const int maxN = 5028, N3 = 3158;
template <int B>
struct BigNum
{
std::vector <int> T;
void normalise()
{
while (T.size() != 1 and T.back() == 0)
T.pop_back();
}
int operator[] (int i) const
{ return T[i]; }
void operator+= (BigNum& other)
{
T.resize(std::max(T.size(), other.T.size()));
int carry = 0;
FOR(i, 0, SZ(T))
{
int b = T[i] + carry + (i < SZ(other.T) ? other[i] : 0);
T[i] = b % B;
carry = b >= B;
}
if (carry == 1)
T.push_back(1);
}
long long operator/= (int m) //|m|<B
{
long long mod = 0;
for (int i=T.size()-1; i>=0; i--)
{
mod = mod * B + T[i];
T[i] = mod / m;
mod %= m;
}
normalise();
return mod;
}
BigNum operator+ (BigNum& other)
{
BigNum res(*this);
res += other;
return res;
}
bool isZero()
{
return T.empty() or T.back() == 0;
}
void fromStr(const char* s = "0")
{
int m = strlen(s);
T.resize(m);
FOR(i, 0, m)
T[i] = s[i] - '0';
std::ranges::reverse(T);
normalise();
}
};
BigNum<3> two2three(BigNum<2>& x)
{
std::vector <int> temp[3];
int ind = 0;
x.normalise();
for (int dig : x.T)
{
temp[ind].push_back(dig);
ind ^= 1;
}
BigNum<4> A { temp[0] };
BigNum<4> B { temp[1] };
A.normalise(), B.normalise();
A += B;
A += B;
while (not A.isZero())
temp[2].push_back(A /= 3);
return BigNum<3> { temp[2] };
}
BigNum<2> three2two(BigNum<3>& x)
{
x.normalise();
std::vector <int> digs;
while (not x.isZero())
digs.push_back(x /= 2);
return BigNum<2> { digs };
}
char id[28], input[maxN];
int n;
std::vector <int> myOrd, hisOrd, myDigs, hisDigs;
int myIt, hisIt;
int play(int x)
{
static const std::string str = "PKN";
static const std::map <char, int> M = {{'P', 0}, {'K', 1}, {'N', 2}};
printf("%c\n", str[x]);
fflush(stdout);
char buf[7];
scanf ("%s", buf);
return M.at(buf[0]);
}
void ans(BigNum<2>& x)
{
int xDigs = SZ(x.T);
int lZeros = n - xDigs;
printf("! ");
while (lZeros--)
printf("0");
FORD(i, xDigs - 1, -1)
printf("%d", x.T[i]);
printf("\n");
fflush(stdout);
}
void ans()
{
BigNum<3> as3 { hisDigs };
as3.normalise();
BigNum<2> as2 = three2two(as3);
as2.normalise();
ans(as2);
}
int digMy()
{
return myDigs[myOrd[myIt++]];
}
void digHis(int d)
{
hisDigs[hisOrd[hisIt++]] = d;
}
void enrand(int& d, int x)
{
d += (id[0] == 'A' ? x : 3 - x);
d %= 3;
}
void derand(int& d, int x)
{
d += (id[0] == 'A' ? x : 3 - x);
d %= 3;
}
void shuffleOrder(int seed)
{
myIt = hisIt = 0;
auto rng = std::default_random_engine {seed};
if (id[0] == 'A')
{
std::ranges::shuffle(myOrd, rng);
std::ranges::shuffle(hisOrd, rng);
}
else
{
std::ranges::shuffle(hisOrd, rng);
std::ranges::shuffle(myOrd, rng);
}
}
void setupOrder()
{
myOrd.resize(N3);
hisOrd.resize(N3);
std::iota(myOrd.begin(), myOrd.end(), 0);
std::iota(hisOrd.begin(), hisOrd.end(), 0);
}
int setupSeed()
{
srand(clock() ^ id[0]);
int myPart = rand() % 3;
int hisPart = play(myPart);
if (myPart != hisPart)
play(hisPart);
int ret = N3 * (myPart + 17) + maxN * (hisPart + 2) * (hisPart + 3);
return ret;
}
void load()
{
scanf ("%s", input);
BigNum<2> myBin;
myBin.fromStr(input);
BigNum<3> tern = two2three(myBin);
tern.normalise();
myDigs = tern.T;
myDigs.resize(N3, 0);
hisDigs.resize(N3, 0);
}
void solve()
{
load();
int seed = setupSeed();
shuffleOrder(seed);
FOR(_, 0, N3)
{
int x = rand() % 3;
int my = digMy(), myOrig = my;
enrand(my, x);
int his = play(my);
derand(his, x);
int hisOrig = his;
digHis(his);
if (myOrig != hisOrig)
play(hisOrig);
}
ans();
}
int main()
{
int tc = 1;
scanf ("%s%d%d", id, &n, &tc);
setupOrder();
FOR(cid, 1, tc+1)
{
// printf("Case #%d: ", cid);
solve();
}
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 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 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (auto i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ITH_BIT(m, i) ((m)>>(i) & 1) #define PPC(x) __builtin_popcount(x) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = std::min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = std::max(a, (T1)b); } const int maxN = 5028, N3 = 3158; template <int B> struct BigNum { std::vector <int> T; void normalise() { while (T.size() != 1 and T.back() == 0) T.pop_back(); } int operator[] (int i) const { return T[i]; } void operator+= (BigNum& other) { T.resize(std::max(T.size(), other.T.size())); int carry = 0; FOR(i, 0, SZ(T)) { int b = T[i] + carry + (i < SZ(other.T) ? other[i] : 0); T[i] = b % B; carry = b >= B; } if (carry == 1) T.push_back(1); } long long operator/= (int m) //|m|<B { long long mod = 0; for (int i=T.size()-1; i>=0; i--) { mod = mod * B + T[i]; T[i] = mod / m; mod %= m; } normalise(); return mod; } BigNum operator+ (BigNum& other) { BigNum res(*this); res += other; return res; } bool isZero() { return T.empty() or T.back() == 0; } void fromStr(const char* s = "0") { int m = strlen(s); T.resize(m); FOR(i, 0, m) T[i] = s[i] - '0'; std::ranges::reverse(T); normalise(); } }; BigNum<3> two2three(BigNum<2>& x) { std::vector <int> temp[3]; int ind = 0; x.normalise(); for (int dig : x.T) { temp[ind].push_back(dig); ind ^= 1; } BigNum<4> A { temp[0] }; BigNum<4> B { temp[1] }; A.normalise(), B.normalise(); A += B; A += B; while (not A.isZero()) temp[2].push_back(A /= 3); return BigNum<3> { temp[2] }; } BigNum<2> three2two(BigNum<3>& x) { x.normalise(); std::vector <int> digs; while (not x.isZero()) digs.push_back(x /= 2); return BigNum<2> { digs }; } char id[28], input[maxN]; int n; std::vector <int> myOrd, hisOrd, myDigs, hisDigs; int myIt, hisIt; int play(int x) { static const std::string str = "PKN"; static const std::map <char, int> M = {{'P', 0}, {'K', 1}, {'N', 2}}; printf("%c\n", str[x]); fflush(stdout); char buf[7]; scanf ("%s", buf); return M.at(buf[0]); } void ans(BigNum<2>& x) { int xDigs = SZ(x.T); int lZeros = n - xDigs; printf("! "); while (lZeros--) printf("0"); FORD(i, xDigs - 1, -1) printf("%d", x.T[i]); printf("\n"); fflush(stdout); } void ans() { BigNum<3> as3 { hisDigs }; as3.normalise(); BigNum<2> as2 = three2two(as3); as2.normalise(); ans(as2); } int digMy() { return myDigs[myOrd[myIt++]]; } void digHis(int d) { hisDigs[hisOrd[hisIt++]] = d; } void enrand(int& d, int x) { d += (id[0] == 'A' ? x : 3 - x); d %= 3; } void derand(int& d, int x) { d += (id[0] == 'A' ? x : 3 - x); d %= 3; } void shuffleOrder(int seed) { myIt = hisIt = 0; auto rng = std::default_random_engine {seed}; if (id[0] == 'A') { std::ranges::shuffle(myOrd, rng); std::ranges::shuffle(hisOrd, rng); } else { std::ranges::shuffle(hisOrd, rng); std::ranges::shuffle(myOrd, rng); } } void setupOrder() { myOrd.resize(N3); hisOrd.resize(N3); std::iota(myOrd.begin(), myOrd.end(), 0); std::iota(hisOrd.begin(), hisOrd.end(), 0); } int setupSeed() { srand(clock() ^ id[0]); int myPart = rand() % 3; int hisPart = play(myPart); if (myPart != hisPart) play(hisPart); int ret = N3 * (myPart + 17) + maxN * (hisPart + 2) * (hisPart + 3); return ret; } void load() { scanf ("%s", input); BigNum<2> myBin; myBin.fromStr(input); BigNum<3> tern = two2three(myBin); tern.normalise(); myDigs = tern.T; myDigs.resize(N3, 0); hisDigs.resize(N3, 0); } void solve() { load(); int seed = setupSeed(); shuffleOrder(seed); FOR(_, 0, N3) { int x = rand() % 3; int my = digMy(), myOrig = my; enrand(my, x); int his = play(my); derand(his, x); int hisOrig = his; digHis(his); if (myOrig != hisOrig) play(hisOrig); } ans(); } int main() { int tc = 1; scanf ("%s%d%d", id, &n, &tc); setupOrder(); FOR(cid, 1, tc+1) { // printf("Case #%d: ", cid); solve(); } return 0; } |
English