#include #include #include #include #include #include using namespace std; typedef uint8_t ResultCode; struct Guess { int d[5]; int mask; }; inline ResultCode get_hb_code(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 b = __builtin_popcount(q.mask & t.mask) - h; return (ResultCode)(h * 6 + b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto start_time = chrono::steady_clock::now(); auto get_ms = [&]() { return chrono::duration_cast(chrono::steady_clock::now() - start_time).count(); }; vector all; all.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; break; } 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.push_back(g); } } vector candidates; for (int i = 0; i < (int)all.size(); i++) candidates.push_back(i); vector is_target_found(all.size(), false); int found_count = 0; mt19937 rng(42); double log_table[30241]; for (int i = 0; i <= 30240; i++) log_table[i] = (i <= 1) ? 0 : log2(i); while (found_count < 30) { int best_idx = -1; if (found_count == 0 && candidates.size() == 30240) { for(int i=0; i<(int)all.size(); ++i) { if(all[i].d[0]==0 && all[i].d[1]==1 && all[i].d[2]==2 && all[i].d[3]==3 && all[i].d[4]==4) { best_idx = i; break; } } } else if (candidates.size() <= (size_t)(30 - found_count)) { for (int c : candidates) { if (!is_target_found[c]) { best_idx = c; break; } } } else { double max_entropy = -1.0; long long time_limit = 4800 - get_ms(); int turn_limit = (candidates.size() > 1500) ? 500 : 2000; auto search_start = chrono::steady_clock::now(); int sample_n = min((int)candidates.size(), 700); vector sample_c; if (candidates.size() <= (size_t)sample_n) sample_c = candidates; else { for (int i = 0; i < sample_n; i++) sample_c.push_back(candidates[rng() % candidates.size()]); } for (int i = 0; i < turn_limit; i++) { if ((i & 31) == 0) { if (chrono::duration_cast(chrono::steady_clock::now() - search_start).count() > (time_limit / 8)) break; } int q_idx = (rng() % 10 < 8) ? candidates[rng() % candidates.size()] : rng() % all.size(); if (is_target_found[q_idx]) continue; int counts[36] = {0}; for (int c_idx : sample_c) { counts[get_hb_code(all[q_idx], all[c_idx])]++; } double entropy = 0; for (int j = 0; j < 36; j++) { if (counts[j] > 0) { entropy -= (double)counts[j] * log_table[counts[j]]; } } if (entropy > max_entropy) { max_entropy = entropy; best_idx = q_idx; } } } if (best_idx == -1) best_idx = candidates[0]; for (int i = 0; i < 5; i++) cout << all[best_idx].d[i]; cout << endl; int res_freq[36] = {0}; for (int i = 0; i < 30; i++) { int h, b; if (!(cin >> h >> b)) return 0; if (h == -1) return 0; res_freq[h * 6 + b]++; } if (res_freq[30] > found_count) { is_target_found[best_idx] = true; found_count = res_freq[30]; } if (found_count == 30) break; int active_30 = res_freq[30] - (found_count - (is_target_found[best_idx] ? 1 : 0)); res_freq[30] = max(0, active_30); vector next_c; next_c.reserve(candidates.size()); for (int c_idx : candidates) { if (is_target_found[c_idx]) continue; if (res_freq[get_hb_code(all[best_idx], all[c_idx])] > 0) { next_c.push_back(c_idx); } } candidates = move(next_c); } return 0; }