#include #include #include #include #include #include #include using namespace std; // 高速化のためHit&Blowの結果を1つの整数(0-35)で管理 // code = h * 6 + b; 5H 0B は 30 inline int get_hb_code(const int* q_d, int q_mask, const int* t_d, int t_mask) { 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 h * 6 + b; } struct Guess { int d[5]; int mask; int id; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto start_time = chrono::steady_clock::now(); // 1. 全通りの生成 vector all; 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) { all.push_back({{digs[0], digs[1], digs[2], digs[3], digs[4]}, m, (int)all.size()}); } } vector candidates; for (int i = 0; i < (int)all.size(); i++) candidates.push_back(i); vector used_query(all.size(), false); int found_count = 0; int turn = 0; mt19937 rng(1337); while (found_count < 30) { int best_idx = -1; auto now = chrono::steady_clock::now(); double elapsed = chrono::duration_cast(now - start_time).count(); // クエリ戦略 if (turn == 0) { // 固定初手: 01234 best_idx = 1234; // all[1234]周辺が01234になるよう調整(実際は探索でOK) 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 (!used_query[c]) { best_idx = c; break; } } else { // エントロピーによる最適解探索 double max_entropy = -1.0; // 候補が少なくなれば全候補をサンプルに使う int sample_n = min((int)candidates.size(), (elapsed < 3000 ? 1000 : 500)); // 探索時間制限(後半になるほど短くして手数を稼ぐ) int time_limit = (elapsed < 4000) ? 400 : 150; auto search_start = chrono::steady_clock::now(); for (int a = 0; a < 2000; a++) { if (a % 50 == 0) { if (chrono::duration_cast(chrono::steady_clock::now() - search_start).count() > time_limit) break; } // クエリ候補の選択:候補集合から70%、全集合から30% int q_idx = (rng() % 10 < 7) ? candidates[rng() % candidates.size()] : rng() % all.size(); if (used_query[q_idx]) continue; int counts[36] = {0}; if (candidates.size() < 1000) { // 候補が少なければ全候補に対してシミュレーション for (int c_idx : candidates) { counts[get_hb_code(all[q_idx].d, all[q_idx].mask, all[c_idx].d, all[c_idx].mask)]++; } } else { // 多ければサンプリング for (int i = 0; i < sample_n; i++) { int c_idx = candidates[rng() % candidates.size()]; counts[get_hb_code(all[q_idx].d, all[q_idx].mask, all[c_idx].d, all[c_idx].mask)]++; } } double entropy = 0; double denom = (candidates.size() < 1000) ? candidates.size() : sample_n; for (int i = 0; i < 36; i++) { if (counts[i] > 0) { double p = (double)counts[i] / denom; entropy -= p * log2(p); } } // 候補内から選ぶ場合はわずかに優先度を上げる(正解する可能性があるため) if (all[q_idx].id == q_idx) { // ダミーチェック bool is_candidate = false; // 実際はフラグ管理すると速い } if (entropy > max_entropy) { max_entropy = entropy; best_idx = q_idx; } } } if (best_idx == -1) best_idx = candidates[0]; // クエリ実行 used_query[best_idx] = true; 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]++; } found_count = res_freq[30]; if (found_count == 30) break; // 候補の絞り込み vector next_c; next_c.reserve(candidates.size()); for (int c_idx : candidates) { // 投げたクエリそのものは、もう(5,0)でカウントされているので候補から除外 if (c_idx == best_idx) continue; int code = get_hb_code(all[best_idx].d, all[best_idx].mask, all[c_idx].d, all[c_idx].mask); // ターゲットが1つ見つかるとジャッジは必ず(5,0)を1つ返す // もしこの候補 c_idx が正解だとしたら、得られた結果リスト res_freq の中に // その結果 (code) が「まだ残っている」必要がある if (code != 30 && res_freq[code] > 0) { next_c.push_back(c_idx); } } candidates = move(next_c); turn++; } return 0; }