//MinMax法の実装を生成AIに依頼しました。 #include #include #include #include #include #include #include using namespace std; struct Result { int h, b; }; struct Guess { int d[5]; int mask; }; // 高速なHit & Blow判定(ループ展開済み) inline Result get_hb_fast(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++; return {h, __builtin_popcount(q.mask & t.mask) - h}; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto g_start = chrono::steady_clock::now(); 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); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int found_total = 0; int turn = 0; while (found_total < 30) { int best_idx = -1; auto now_t = chrono::steady_clock::now(); int elapsed = chrono::duration_cast(now_t - g_start).count(); // 戦略的固定クエリ if (turn == 0) { best_idx = 123; // "01234" に相当するインデックスを検索(全生成順に依存) 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 && found_total < 10) { 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 if (elapsed > 4700) { best_idx = candidates[0]; } else { double max_score = -1.0; // 候補数に応じてサンプリング数を調整 int s_size = (candidates.size() < 800) ? (int)candidates.size() : 800; auto t_limit = (elapsed < 3500) ? 200 : 100; auto t_start = chrono::steady_clock::now(); while (chrono::duration_cast(chrono::steady_clock::now() - t_start).count() < t_limit) { // クエリ候補:候補内から選ぶ確率を高める int t_idx = (rng() % 10 < 8) ? candidates[rng() % candidates.size()] : rng() % all_g.size(); int counts[6][6] = {0}; for (int i = 0; i < s_size; i++) { Result r = get_hb_fast(all_g[t_idx], all_g[candidates[rng() % candidates.size()]]); counts[r.h][r.b]++; } // エントロピー計算 $E = -\sum p \log_2 p$ 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] / s_size; e -= p * log2(p); } } } // 候補内クエリにボーナス(直接正解する可能性があるため) if (candidates.size() < 100) { bool is_cand = false; for(int c : candidates) if(c == t_idx) { is_cand = true; break; } if(is_cand) e += 0.1; } if (e > max_score) { max_score = e; best_idx = t_idx; } if (chrono::duration_cast(chrono::steady_clock::now() - g_start).count() > 4900) break; } } 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_map[6][6] = {0}; for (int i = 0; i < 30; i++) { int h, b; cin >> h >> b; if (h == -1) return 0; res_map[h][b]++; } if (res_map[5][0] == 30) break; found_total = res_map[5][0]; vector next_c; next_c.reserve(candidates.size()); for (int idx : candidates) { Result r = get_hb_fast(all_g[best_idx], all_g[idx]); if (r.h == 5 && r.b == 0) continue; if (res_map[r.h][r.b] > 0) { next_c.push_back(idx); } } if (!next_c.empty()) candidates = move(next_c); turn++; } return 0; }