#include #include #include #include #include #include using namespace std; // 高速化のため型を小さく typedef uint8_t ResultCode; struct Guess { uint8_t d[5]; // char/uint8_tで十分 int mask; }; // 全パターン保持用 vector all_patterns; // ヒット・ブロー判定 // インライン展開とビット演算で極限まで高速化 inline ResultCode get_hb_code(const Guess& q, const Guess& t) { int h = 0; if (q.d[0] == t.d[0]) h++; if (q.d[1] == t.d[1]) h++; if (q.d[2] == t.d[2]) h++; if (q.d[3] == t.d[3]) h++; if (q.d[4] == t.d[4]) h++; // maskのANDをとってビット数を数える = 共通して含まれる数字の個数 (Hit + Blow) int common = __builtin_popcount(q.mask & t.mask); // return値: h * 6 + b (b = common - h) return (ResultCode)(h * 6 + (common - h)); } int main() { // 入出力高速化 ios_base::sync_with_stdio(false); cin.tie(NULL); // 乱数生成 mt19937 rng(5489); // 固定シードで再現性を確保しつつ適度なランダム性 // 1. 全パターン生成 (約30240通り) all_patterns.reserve(30240); for (int i = 0; i <= 99999; i++) { int tmp = i, m = 0; uint8_t 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_patterns.push_back(g); } } // 候補リスト(インデックスで管理) vector candidates; candidates.reserve(all_patterns.size()); for (int i = 0; i < (int)all_patterns.size(); i++) candidates.push_back(i); // 解決済みフラグ管理 vector is_solved(all_patterns.size(), false); vector solved_indices; // 既に特定した文字列のインデックス int solved_count = 0; // メインループ // 30個すべて見つけるまで while (solved_count < 30) { int best_guess_idx = -1; int cand_size = (int)candidates.size(); // --- クエリ決定フェーズ --- // 戦略: // 1. 候補が極端に多い場合(>2000): 計算コストが高いのでランダムに選ぶ(それでも十分減る) // 2. それ以外: 候補の中から、もっとも「候補集合を均等に分割できる質問」を選ぶ // ※ log計算を避けるため、Σ(count^2) [衝突数の二乗和] を最小化する戦略をとる if (cand_size > 2000) { // ランダムに選ぶ (candidatesの中から選ぶのが重要) best_guess_idx = candidates[rng() % cand_size]; } else { long long min_score = -1; // スコアは小さいほうが良い(衝突が少ない) // 評価に使うサンプルの間引き // 候補が多いときは全探索すると遅いので、質問候補(Q)と評価対象(S)を間引く int check_limit_q = (cand_size > 500) ? 50 : cand_size; // 質問候補数 int check_limit_s = (cand_size > 500) ? 200 : cand_size; // 比較対象数 // ランダムに質問候補を選ぶためのインデックス配列作成 vector q_indices; if (cand_size <= check_limit_q) { q_indices = candidates; } else { q_indices.reserve(check_limit_q); for(int i=0; i s_indices; if (cand_size <= check_limit_s) { s_indices = candidates; } else { s_indices.reserve(check_limit_s); for(int i=0; i 1) { // 0か1なら足す必要なし current_score += (long long)counts[j] * counts[j]; } } if (min_score == -1 || current_score < min_score) { min_score = current_score; best_guess_idx = q_idx; } } } // 万が一決まらなかった場合のフェイルセーフ(通常ここには来ない) if (best_guess_idx == -1) best_guess_idx = candidates[0]; // --- クエリ出力 --- const auto& best_guess = all_patterns[best_guess_idx]; for (int i = 0; i < 5; i++) cout << (int)best_guess.d[i]; cout << endl; // endlでフラッシュされる // --- レスポンス取得 --- // 頻度分布として受け取る int res_freq[36] = {0}; int current_5h_count = 0; for (int i = 0; i < 30; i++) { int h, b; cin >> h >> b; if (h == -1) return 0; // エラー終了 int code = h * 6 + b; res_freq[code]++; if (code == 30) current_5h_count++; // (5,0)はコード30 } // --- 解決判定 --- // 今回のクエリ自体が正解だった場合(ジャッジ側の5Hの数が増えているか確認) // ただし、既に正解済みのものに対する (5,0) レスポンスも含まれるため差分を見る if (!is_solved[best_guess_idx]) { if (current_5h_count > (int)solved_indices.size()) { is_solved[best_guess_idx] = true; solved_indices.push_back(best_guess_idx); } } solved_count = current_5h_count; if (solved_count == 30) break; // 全問正解 // --- 候補フィルタリング --- // まだ見つかっていないターゲットからの返答のみを抽出した分布を作る int unknown_res_freq[36]; for(int j=0; j<36; j++) unknown_res_freq[j] = res_freq[j]; // 既知の正解による (5,0) の分を差し引く unknown_res_freq[30] -= (int)solved_indices.size(); // 次の候補リストを作成 // swap技法を使って再確保コストを減らすことも可能だが、可読性重視で別vector作成 vector next_candidates; next_candidates.reserve(candidates.size()); for (int c_idx : candidates) { // 既に解けたものは候補から外す if (is_solved[c_idx]) continue; // 自分が正解だと仮定したとき、今回の質問に対して返ってくるはずのHB ResultCode code = get_hb_code(best_guess, all_patterns[c_idx]); // そのHBが、未知のターゲットからの返答群に含まれているか? // 含まれていなければ、この候補はあり得ない if (unknown_res_freq[code] > 0) { next_candidates.push_back(c_idx); } } candidates = move(next_candidates); } return 0; }