#include "bits/stdc++.h" using namespace std; using ll = long long; #define OVERLOAD3(_1, _2, _3, call,...) call #define REP1(i, n) for (ll i = 0; i < ll(n); ++i) #define REP2(i, a, b) for (ll i = ll(a); i < ll(b); ++i) #define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__) #define RREP1(i, n) for (ll i = ll(n) - 1; i >= 0; --i) #define RREP2(i, a, b) for (ll i = ll(a) - 1; i >= b; --i) #define RREP(...) OVERLOAD3(__VA_ARGS__, RREP2, RREP1)(__VA_ARGS__) #define REPE(i, c) for (auto&& i : c) #define TCASE() ll _ttt; cin >> _ttt; while(_ttt--) ll dp[12][19]; void init_dp(int n, int m) { memset(dp, 0, sizeof(dp)); dp[0][0] = 1; REP(i, 1, m + 1) { REP(j, n + 1) { REP(k, min(i, j) + 1) { dp[i][j] += dp[i - 1][j - k]; } } } } ll get_rk(const vector& hand_counts, int n, int m) { ll rk = 0; int rem = n; for (int i = m; i >= 1; --i) { int c = hand_counts[i]; for (int k = 0; k < c; ++k) { rk += dp[i-1][rem - k]; } rem -= c; } return rk; } vector rank_to_hand(ll rank, int n, int m) { vector counts(m + 1, 0); int rem_n = n; for (int i = m; i >= 1; --i) { for (int k = 0; k <= min(i, rem_n); ++k) { ll ways = dp[i - 1][rem_n - k]; if (rank >= ways) { rank -= ways; } else { counts[i] = k; rem_n -= k; break; } } } return counts; } int main() { int id, n, m; cin >> id >> n >> m; init_dp(n, m); vector hand_counts(m + 1, 0); for (int i = 0; i < n; ++i) { int c; cin >> c; hand_counts[c]++; } ll my_rk = get_rk(hand_counts, n, m); ll max_rk = dp[m][n]; ll q = 0; ll qc = 1; while (qc < max_rk) { qc *= m; q++; } vector query(q, 0); ll tmp = my_rk; REP(i, q) { query[i] = tmp % m; tmp /= m; } vector other_query; vector total_counts(m + 1, -1); vector other_counts(m + 1, 0); int turn_idx = 0; bool decode_other = false; bool guessed = false; while (true) { string action; if (!(cin >> action)) break; if (action == "END") { int final_q; cin >> final_q; break; } if (action == "TURN") { if (other_query.size() == q && !decode_other) { ll other_rank = 0; ll mult = 1; for (int i = 0; i < q; ++i) { other_rank += (ll)other_query[i] * mult; mult *= m; } other_counts = rank_to_hand(other_rank, n, m); decode_other = true; } int next_ask = -1; if (turn_idx < q) { next_ask = query[turn_idx] + 1; } else { ll sum = 0; for (int i = 1; i < m; ++i) { if (total_counts[i] != -1) { sum += total_counts[i]; } } if (sum == 3 * n) { for (int i = 1; i < m; ++i) { if (total_counts[i] == -1) total_counts[i] = 0; } } else { for (int i = 1; i < m; ++i) { if (total_counts[i] == -1) { next_ask = i; break; } } } } if (next_ask != -1) { cout << "ASK " << next_ask << endl; } else { if (!guessed) { vector c_hand; for (int i = 1; i < m; ++i) { int c = total_counts[i] - hand_counts[i] - other_counts[i]; for (int k = 0; k < c; ++k) c_hand.push_back(i); } while (c_hand.size() < n) c_hand.push_back(m); cout << "GUESS"; for (int v : c_hand) cout << " " << v; cout << endl; guessed = true; } } } if (action == "TURN" || action == "WAIT") { string res; cin >> res; if (res == "COUNT") { int x, k; cin >> x >> k; total_counts[x] = k; if (action == "TURN") { turn_idx++; } else if (action == "WAIT") { if (other_query.size() < q) { other_query.push_back(x - 1); } } } else if (res == "GUESSED") { int id, ok; cin >> id >> ok; } } } return 0; }