//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<class T>
using vec = vector<T>;
template<typename T>
bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef KoRoVa
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
const int max_n = -1, inf = 1000111222;
int win(int a, int b) {
if (a == b) return 0;
if ((a == 0 && b == 2) || (a == 1 && b == 0) || (a == 2 && b == 1)) return 1;
return -1;
}
char to_char(int x) {
if (x == 0) return 'K';
if (x == 1) return 'P';
return 'N';
}
int to_move(char c) {
if (c == 'K') return 0;
if (c == 'P') return 1;
return 2;
}
void norm(vector<int> &a) {
while (len(a) > 1 && a.back() == 0) a.pop_back();
}
void gigacringe(vector<int> &a, int add) {
int carry = add;
for (int i = 0; i < len(a); i++) {
int cur = 2 * a[i] + carry;
a[i] = cur % 3;
carry = cur / 3;
}
while (carry) {
a.pb(carry % 3);
carry /= 3;
}
}
int get_k(int n) {
vector<int> a(1, 0);
for (int i = 0; i < n; i++) {
gigacringe(a, 1);
}
norm(a);
return len(a);
}
vector<int> bin_to_ternary(const string &s, int k) {
vector<int> a(1, 0);
for (auto& c: s) {
gigacringe(a, c - '0');
}
a.resize(k, 0);
return a;
}
string ternary_to_bin(vector<int> a, int n) {
norm(a);
string s(n, '0');
for (int i = n - 1; i >= 0; i--) {
int rem = 0;
for (int j = len(a) - 1; j >= 0; j--) {
int cur = rem * 3 + a[j];
a[j] = cur / 2;
rem = cur % 2;
}
s[i] = char('0' + rem);
norm(a);
}
return s;
}
void test_case(int n, int k) {
string own;
cin >> own;
vector<int> my = bin_to_ternary(own, k);
vector<int> it(k);
int diff = 0;
for (int i = 0; i < k; i++) {
int my_move = my[i];
cout << to_char(my_move) << endl;
char ch;
cin >> ch;
int it_move = to_move(ch);
it[i] = it_move;
diff += win(my_move, it_move);
if (i + 1 == k) break;
int rec_move = (diff == -1 ? 1 : 0);
cout << to_char(rec_move) << endl;
cin >> ch;
it_move = to_move(ch);
diff += win(rec_move, it_move);
}
string ans = ternary_to_bin(it, n);
cout << "! " << ans << endl;
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
string role;
int n, t;
cin >> role;
cin >> n >> t;
int k = get_k(n);
while (t--) test_case(n, k);
return 0;
}
/*
KoRoVa!
*/
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 | //#pragma GCC optimize("Ofast", "unroll-loops") //#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4") #include <bits/stdc++.h> #define all(a) a.begin(),a.end() #define len(a) (int)(a.size()) #define mp make_pair #define pb push_back #define fi first #define se second using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef long double ld; template<class T> using vec = vector<T>; template<typename T> bool umin(T &a, T b) { if (b < a) { a = b; return true; } return false; } template<typename T> bool umax(T &a, T b) { if (a < b) { a = b; return true; } return false; } #ifdef KoRoVa #define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false) #define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); } #else #define DEBUG while (false) #define LOG(...) #endif const int max_n = -1, inf = 1000111222; int win(int a, int b) { if (a == b) return 0; if ((a == 0 && b == 2) || (a == 1 && b == 0) || (a == 2 && b == 1)) return 1; return -1; } char to_char(int x) { if (x == 0) return 'K'; if (x == 1) return 'P'; return 'N'; } int to_move(char c) { if (c == 'K') return 0; if (c == 'P') return 1; return 2; } void norm(vector<int> &a) { while (len(a) > 1 && a.back() == 0) a.pop_back(); } void gigacringe(vector<int> &a, int add) { int carry = add; for (int i = 0; i < len(a); i++) { int cur = 2 * a[i] + carry; a[i] = cur % 3; carry = cur / 3; } while (carry) { a.pb(carry % 3); carry /= 3; } } int get_k(int n) { vector<int> a(1, 0); for (int i = 0; i < n; i++) { gigacringe(a, 1); } norm(a); return len(a); } vector<int> bin_to_ternary(const string &s, int k) { vector<int> a(1, 0); for (auto& c: s) { gigacringe(a, c - '0'); } a.resize(k, 0); return a; } string ternary_to_bin(vector<int> a, int n) { norm(a); string s(n, '0'); for (int i = n - 1; i >= 0; i--) { int rem = 0; for (int j = len(a) - 1; j >= 0; j--) { int cur = rem * 3 + a[j]; a[j] = cur / 2; rem = cur % 2; } s[i] = char('0' + rem); norm(a); } return s; } void test_case(int n, int k) { string own; cin >> own; vector<int> my = bin_to_ternary(own, k); vector<int> it(k); int diff = 0; for (int i = 0; i < k; i++) { int my_move = my[i]; cout << to_char(my_move) << endl; char ch; cin >> ch; int it_move = to_move(ch); it[i] = it_move; diff += win(my_move, it_move); if (i + 1 == k) break; int rec_move = (diff == -1 ? 1 : 0); cout << to_char(rec_move) << endl; cin >> ch; it_move = to_move(ch); diff += win(rec_move, it_move); } string ans = ternary_to_bin(it, n); cout << "! " << ans << endl; } int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); string role; int n, t; cin >> role; cin >> n >> t; int k = get_k(n); while (t--) test_case(n, k); return 0; } /* KoRoVa! */ |
English