#include <bitset>
#include <iostream>
#include <vector>
// #include "testerka_pkm.h"
#define lol long long
using namespace std;
int main() {
auto exchange_trits = [&](char trit) -> char {
char received;
bool won;
bool lost;
switch (trit) {
case 0:
cout << "P" << flush;
cin >> received;
won = (received == 'K');
lost = (received == 'N');
break;
case 1:
cout << "K" << flush;
cin >> received;
won = (received == 'N');
lost = (received == 'P');
break;
case 2:
cout << "N" << flush;
cin >> received;
won = (received == 'P');
lost = (received == 'K');
break;
}
if (won) {
char tmp;
cout << "K" << flush;
cin >> tmp;
}
if (lost) {
char tmp;
cout << "P" << flush;
cin >> tmp;
}
switch (received) {
case 'P':
return 0;
case 'K':
return 1;
case 'N':
return 2;
default:
throw(invalid_argument("Niepoprawne wejście!!!!"));
}
};
string imie;
cin >> imie;
int n, t;
cin >> n >> t;
for (int proba = 0; proba < t; proba++) {
string bits;
cin >> bits;
string bits_recived(n, '\0');
for (int i = bits.size() - 19; i >= 0; i -= 19) {
int val_in = stol(bits.substr(i, 19), 0, 2);
int val_out = 0;
int mult = 1;
for (int j = 0; j < 12; j++) {
char trit_recived = exchange_trits(val_in % 3);
val_in /= 3;
val_out += trit_recived * mult;
mult *= 3;
}
bits_recived.replace(i, 19, bitset<19>(val_out).to_string());
}
{
int log = 0, tmp = 1;
while (tmp < (1 << (bits.size() % 12))) {
tmp *= 3;
log += 1;
}
int val_in = stoll(bits.substr(0, bits.size() % 12), 0, 2);
int val_out = 0;
int mult = 1;
for (int j = 0; j < log; j++) {
char trit_recived = exchange_trits(val_in % 3);
val_in /= 3;
val_out += trit_recived * mult;
mult *= 3;
}
bits_recived.replace(0, bits.size() % 12, bitset<19>(val_out).to_string().substr(19 - bits.size() % 12, bits.size() % 12));
}
cout << "! " << bits_recived << flush;
}
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 | #include <bitset> #include <iostream> #include <vector> // #include "testerka_pkm.h" #define lol long long using namespace std; int main() { auto exchange_trits = [&](char trit) -> char { char received; bool won; bool lost; switch (trit) { case 0: cout << "P" << flush; cin >> received; won = (received == 'K'); lost = (received == 'N'); break; case 1: cout << "K" << flush; cin >> received; won = (received == 'N'); lost = (received == 'P'); break; case 2: cout << "N" << flush; cin >> received; won = (received == 'P'); lost = (received == 'K'); break; } if (won) { char tmp; cout << "K" << flush; cin >> tmp; } if (lost) { char tmp; cout << "P" << flush; cin >> tmp; } switch (received) { case 'P': return 0; case 'K': return 1; case 'N': return 2; default: throw(invalid_argument("Niepoprawne wejście!!!!")); } }; string imie; cin >> imie; int n, t; cin >> n >> t; for (int proba = 0; proba < t; proba++) { string bits; cin >> bits; string bits_recived(n, '\0'); for (int i = bits.size() - 19; i >= 0; i -= 19) { int val_in = stol(bits.substr(i, 19), 0, 2); int val_out = 0; int mult = 1; for (int j = 0; j < 12; j++) { char trit_recived = exchange_trits(val_in % 3); val_in /= 3; val_out += trit_recived * mult; mult *= 3; } bits_recived.replace(i, 19, bitset<19>(val_out).to_string()); } { int log = 0, tmp = 1; while (tmp < (1 << (bits.size() % 12))) { tmp *= 3; log += 1; } int val_in = stoll(bits.substr(0, bits.size() % 12), 0, 2); int val_out = 0; int mult = 1; for (int j = 0; j < log; j++) { char trit_recived = exchange_trits(val_in % 3); val_in /= 3; val_out += trit_recived * mult; mult *= 3; } bits_recived.replace(0, bits.size() % 12, bitset<19>(val_out).to_string().substr(19 - bits.size() % 12, bits.size() % 12)); } cout << "! " << bits_recived << flush; } return 0; } |
English