#include #include #include #include #include #include #include using namespace std; struct Result { int h, b; bool operator<(const Result& o) const { if (h != o.h) return h < o.h; return b < o.b; } bool operator==(const Result& o) const { return h == o.h && b == o.b; } }; struct Guess { int d[5]; int mask; }; inline Result get_hb(const Guess& q, const Guess& t) { int h = (q.d[0] == t.d[0]) + (q.d[1] == t.d[1]) + (q.d[2] == t.d[2]) + (q.d[3] == t.d[3]) + (q.d[4] == t.d[4]); int common = __builtin_popcount(q.mask & t.mask); return {h, common - h}; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto start_time = chrono::steady_clock::now(); mt19937 rng(1337); vector all_g; all_g.reserve(30240); for (int i = 0; i <= 99999; i++) { int tmp = i, m = 0, digs[5]; bool ok = true; for (int j = 4; j >= 0; j--) { digs[j] = tmp % 10; if (m & (1 << digs[j])) ok = false; m |= (1 << digs[j]); tmp /= 10; } if (ok) { Guess g; g.mask = m; for (int j = 0; j < 5; j++) g.d[j] = digs[j]; all_g.push_back(g); } } vector candidates; candidates.reserve(all_g.size()); for (int i = 0; i < (int)all_g.size(); i++) candidates.push_back(i); int turn = 0; while (true) { int best_idx = -1; if (turn == 0) { for (int i = 0; i < (int)all_g.size(); i++) { if (all_g[i].d[0] == 0 && all_g[i].d[1] == 1 && all_g[i].d[2] == 2 && all_g[i].d[3] == 3 && all_g[i].d[4] == 4) { best_idx = i; break; } } } else if (turn == 1) { for (int i = 0; i < (int)all_g.size(); i++) { if (all_g[i].d[0] == 5 && all_g[i].d[1] == 6 && all_g[i].d[2] == 7 && all_g[i].d[3] == 8 && all_g[i].d[4] == 9) { best_idx = i; break; } } } else { double max_e = -1.0; auto search_start = chrono::steady_clock::now(); int sample_n = min((int)candidates.size(), 600); int attempts = (candidates.size() > 1000) ? 100 : 250; for (int a = 0; a < attempts; a++) { if (a % 10 == 0) { auto now = chrono::steady_clock::now(); if (chrono::duration_cast(now - search_start).count() > 400) break; } int q_idx; if (rng() % 10 < 7 && !candidates.empty()) { q_idx = candidates[rng() % candidates.size()]; } else { q_idx = rng() % all_g.size(); } int counts[6][6] = {0}; for (int i = 0; i < sample_n; i++) { int c_idx = candidates[rng() % candidates.size()]; Result r = get_hb(all_g[q_idx], all_g[c_idx]); counts[r.h][r.b]++; } double e = 0; for (int h = 0; h <= 5; h++) { for (int b = 0; b <= 5; b++) { if (counts[h][b] > 0) { double p = (double)counts[h][b] / sample_n; e -= p * log2(p); } } } if (e > max_e) { max_e = e; best_idx = q_idx; } } } if (best_idx == -1) best_idx = candidates[0]; for (int i = 0; i < 5; i++) cout << all_g[best_idx].d[i]; cout << endl; int res_counts[6][6] = {0}; bool all_cleared = false; int first_h, first_b; for (int i = 0; i < 30; i++) { int h, b; if (!(cin >> h >> b)) return 0; if (h == -1) return 0; if (i == 0) { first_h = h; first_b = b; } res_counts[h][b]++; } if (first_h == 5 && first_b == 0) break; vector next_c; for (int c_idx : candidates) { Result r = get_hb(all_g[best_idx], all_g[c_idx]); if (r.h == 5 && r.b == 0) continue; if (res_counts[r.h][r.b] > 0) { next_c.push_back(c_idx); } } candidates = move(next_c); turn++; } return 0; }