#include <cassert>
#include <charconv>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
class BigInt
{
const size_t length;
std::vector<int> bity; // np. 10010, big endian
public:
explicit BigInt(int length) : length(length) {
}
explicit BigInt(const std::string& string) : length(string.size()) {
bity.reserve(length);
auto it = string.begin();
while (it != string.end() && *it == '0') {
++it;
}
while (it != string.end()) {
bity.push_back('1' == *it++);
}
}
const std::vector<int>& bits() const
{
return bity;
}
bool nonzero() const
{
return !bity.empty();
}
std::string to_string(bool padding = false) const
{
size_t print_length = std::max<size_t>(padding ? length : 1, bity.size());
std::string result(print_length, '0');
size_t index = print_length - bity.size();
for (int b : bity) {
result[index++] = '0' + b;
}
return result;
}
void div2()
{
if (!bity.empty()) {
bity.pop_back();
}
}
void div3()
{
BigInt temp(1); // tu będzie wynik dzielenia przez trzy
int r = 0; // reszta z dzielenia przez 3
for (int b : bity) {
temp.mul2();
r *= 2;
r += b;
if (r >= 3) {
temp.add1();
r -= 3;
}
}
std::swap(bity, temp.bity);
}
void mul2()
{
if (!bity.empty()) {
bity.push_back(0);
}
}
void mul3()
{
std::vector<int> temp(bity.size() + 2, 0);
temp.reserve(length);
for (int i=bity.size()-1; i>=0; --i) {
temp[i+1] += bity[i];
temp[i+2] += bity[i];
}
int carry = 0;
for (int i=temp.size()-1; i>=0; --i) {
int b = temp[i] + carry;
temp[i] = b % 2;
carry = b / 2;
}
auto it = temp.begin();
while (it != temp.end() && !*it) ++it;
bity = std::vector(it, temp.end());
}
void add1()
{
for (auto it=bity.rbegin(); it!=bity.rend(); ++it) {
if (*it == 0) {
++*it;
return;
} else {
*it = 0;
}
}
bity.insert(bity.begin(), 1);
}
int mod2() const
{
return bity.empty() ? 0 : bity.back();
}
int mod3() const
{
int suma = 0;
int q = 1;
for (auto it=bity.rbegin(); it!=bity.rend(); ++it) {
if (*it) {
suma += q;
}
q = 3 - q;
}
return suma % 3;
}
};
char nextchar()
{
int c = getchar();
if (c == EOF) exit(0);
return c;
}
void nextline()
{
int c = getchar();
assert(c == EOF || c == '\n');
}
bool wygrana(char c_nasz, char c_ich) {
return (c_nasz == 'P' && c_ich == 'K')
|| (c_nasz == 'K' && c_ich == 'N')
|| (c_nasz == 'N' && c_ich == 'P');
}
const int PIERWSZA = 21767;
const int DRUGA = 32749;
int MASKA[5000];
class Kontroler
{
int wynik = 0;
public:
BigInt wczytaj(int N)
{
std::string bits(N, '0');
for (int i=0; i<N; ++i) {
bits[i] = nextchar();
}
nextline();
for (int i=0; i<N; ++i) {
if (MASKA[i]) {
bits[i] = '1'+'0'-bits[i];
}
}
return BigInt(bits);
}
char wyslij(char c_nasz)
{
printf("%c\n", c_nasz);
fflush(stdout);
char c_ich = nextchar();
nextline();
if (wygrana(c_nasz, c_ich)) {
wynik++;
} else if (wygrana(c_ich, c_nasz)) {
wynik--;
}
return c_ich;
}
void zgadnij(const BigInt& x)
{
std::string bits = x.to_string(true);
for (int i=bits.size()-1; i>=0; --i) {
if (MASKA[i]) {
bits[i] = '1'+'0'-bits[i];
}
}
putchar('!'); putchar(' ');
puts(bits.c_str());
fflush(stdout);
}
int przewaga() const
{
return wynik;
}
};
char TRITY[3] = {'P', 'K', 'N'};
void dawaj(int N)
{
Kontroler kontroler;
BigInt bitset_moj = kontroler.wczytaj(N);
std::vector<div_t> elementy;
elementy.reserve(N);
BigInt bitmax_moj = BigInt(std::string(N, '1'));
BigInt bitmax_ich = BigInt(std::string(N, '1'));
int rund = 0;
while (bitmax_moj.nonzero() || bitmax_ich.nonzero()) {
if (kontroler.przewaga() == 0) {
// remis, każdy wysyła swój trit
int t = bitset_moj.mod3();
bitset_moj.div3();
bitmax_moj.div3();
int c_nasz = TRITY[t];
char c_ich = kontroler.wyslij(c_nasz);
if (bitmax_ich.nonzero()) {
elementy.push_back({ 3, (c_ich == 'K') + 2 * (c_ich == 'N') });
bitmax_ich.div3();
}
} else if (kontroler.przewaga() == 1) {
// my mamy przewagę, wysyłamy zawsze papier
int c_nasz = 'P';
char c_ich = kontroler.wyslij(c_nasz);
if (bitmax_ich.nonzero()) {
elementy.push_back({2, c_ich == 'P'});
bitmax_ich.div2();
}
} else if (kontroler.przewaga() == -1) {
// przeciwnik ma przewagę, więc zawsze wyśle papier
int b = bitset_moj.mod2();
bitset_moj.div2();
bitmax_moj.div2();
int c_nasz = b ? 'P' : 'N';
kontroler.wyslij(c_nasz); // odebrany powinien być P
}
++rund;
}
BigInt bitset_ich(N);
for (auto it=elementy.rbegin(); it!=elementy.rend(); ++it) {
if (it->quot == 2) {
bitset_ich.mul2();
} else if (it->quot == 3) {
bitset_ich.mul3();
}
for (int i=0; i<it->rem; ++i) {
bitset_ich.add1();
}
}
//fprintf(stderr, "RUND %d\n", rund);
kontroler.zgadnij(bitset_ich);
}
char X;
int main()
{
X = getchar();
while (getchar() != '\n');
int N, T;
if (scanf("%d %d\n", &N, &T) != 2) {
return 1;
}
srand(N);
for (int i=0; i<N; ++i) {
MASKA[i] = rand() % 2;
}
while (T-- > 0) {
dawaj(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 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 | #include <cassert> #include <charconv> #include <cstdio> #include <cstdlib> #include <string> #include <vector> class BigInt { const size_t length; std::vector<int> bity; // np. 10010, big endian public: explicit BigInt(int length) : length(length) { } explicit BigInt(const std::string& string) : length(string.size()) { bity.reserve(length); auto it = string.begin(); while (it != string.end() && *it == '0') { ++it; } while (it != string.end()) { bity.push_back('1' == *it++); } } const std::vector<int>& bits() const { return bity; } bool nonzero() const { return !bity.empty(); } std::string to_string(bool padding = false) const { size_t print_length = std::max<size_t>(padding ? length : 1, bity.size()); std::string result(print_length, '0'); size_t index = print_length - bity.size(); for (int b : bity) { result[index++] = '0' + b; } return result; } void div2() { if (!bity.empty()) { bity.pop_back(); } } void div3() { BigInt temp(1); // tu będzie wynik dzielenia przez trzy int r = 0; // reszta z dzielenia przez 3 for (int b : bity) { temp.mul2(); r *= 2; r += b; if (r >= 3) { temp.add1(); r -= 3; } } std::swap(bity, temp.bity); } void mul2() { if (!bity.empty()) { bity.push_back(0); } } void mul3() { std::vector<int> temp(bity.size() + 2, 0); temp.reserve(length); for (int i=bity.size()-1; i>=0; --i) { temp[i+1] += bity[i]; temp[i+2] += bity[i]; } int carry = 0; for (int i=temp.size()-1; i>=0; --i) { int b = temp[i] + carry; temp[i] = b % 2; carry = b / 2; } auto it = temp.begin(); while (it != temp.end() && !*it) ++it; bity = std::vector(it, temp.end()); } void add1() { for (auto it=bity.rbegin(); it!=bity.rend(); ++it) { if (*it == 0) { ++*it; return; } else { *it = 0; } } bity.insert(bity.begin(), 1); } int mod2() const { return bity.empty() ? 0 : bity.back(); } int mod3() const { int suma = 0; int q = 1; for (auto it=bity.rbegin(); it!=bity.rend(); ++it) { if (*it) { suma += q; } q = 3 - q; } return suma % 3; } }; char nextchar() { int c = getchar(); if (c == EOF) exit(0); return c; } void nextline() { int c = getchar(); assert(c == EOF || c == '\n'); } bool wygrana(char c_nasz, char c_ich) { return (c_nasz == 'P' && c_ich == 'K') || (c_nasz == 'K' && c_ich == 'N') || (c_nasz == 'N' && c_ich == 'P'); } const int PIERWSZA = 21767; const int DRUGA = 32749; int MASKA[5000]; class Kontroler { int wynik = 0; public: BigInt wczytaj(int N) { std::string bits(N, '0'); for (int i=0; i<N; ++i) { bits[i] = nextchar(); } nextline(); for (int i=0; i<N; ++i) { if (MASKA[i]) { bits[i] = '1'+'0'-bits[i]; } } return BigInt(bits); } char wyslij(char c_nasz) { printf("%c\n", c_nasz); fflush(stdout); char c_ich = nextchar(); nextline(); if (wygrana(c_nasz, c_ich)) { wynik++; } else if (wygrana(c_ich, c_nasz)) { wynik--; } return c_ich; } void zgadnij(const BigInt& x) { std::string bits = x.to_string(true); for (int i=bits.size()-1; i>=0; --i) { if (MASKA[i]) { bits[i] = '1'+'0'-bits[i]; } } putchar('!'); putchar(' '); puts(bits.c_str()); fflush(stdout); } int przewaga() const { return wynik; } }; char TRITY[3] = {'P', 'K', 'N'}; void dawaj(int N) { Kontroler kontroler; BigInt bitset_moj = kontroler.wczytaj(N); std::vector<div_t> elementy; elementy.reserve(N); BigInt bitmax_moj = BigInt(std::string(N, '1')); BigInt bitmax_ich = BigInt(std::string(N, '1')); int rund = 0; while (bitmax_moj.nonzero() || bitmax_ich.nonzero()) { if (kontroler.przewaga() == 0) { // remis, każdy wysyła swój trit int t = bitset_moj.mod3(); bitset_moj.div3(); bitmax_moj.div3(); int c_nasz = TRITY[t]; char c_ich = kontroler.wyslij(c_nasz); if (bitmax_ich.nonzero()) { elementy.push_back({ 3, (c_ich == 'K') + 2 * (c_ich == 'N') }); bitmax_ich.div3(); } } else if (kontroler.przewaga() == 1) { // my mamy przewagę, wysyłamy zawsze papier int c_nasz = 'P'; char c_ich = kontroler.wyslij(c_nasz); if (bitmax_ich.nonzero()) { elementy.push_back({2, c_ich == 'P'}); bitmax_ich.div2(); } } else if (kontroler.przewaga() == -1) { // przeciwnik ma przewagę, więc zawsze wyśle papier int b = bitset_moj.mod2(); bitset_moj.div2(); bitmax_moj.div2(); int c_nasz = b ? 'P' : 'N'; kontroler.wyslij(c_nasz); // odebrany powinien być P } ++rund; } BigInt bitset_ich(N); for (auto it=elementy.rbegin(); it!=elementy.rend(); ++it) { if (it->quot == 2) { bitset_ich.mul2(); } else if (it->quot == 3) { bitset_ich.mul3(); } for (int i=0; i<it->rem; ++i) { bitset_ich.add1(); } } //fprintf(stderr, "RUND %d\n", rund); kontroler.zgadnij(bitset_ich); } char X; int main() { X = getchar(); while (getchar() != '\n'); int N, T; if (scanf("%d %d\n", &N, &T) != 2) { return 1; } srand(N); for (int i=0; i<N; ++i) { MASKA[i] = rand() % 2; } while (T-- > 0) { dawaj(N); } } |
English