#include #include #include #include #include #include using namespace std; // 結果を1バイトで保持 (H*6 + B) typedef uint8_t Result; struct Guess { int8_t d[5]; int16_t 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 (Result)(h * 6 + (common - h)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto start_time = chrono::steady_clock::now(); mt19937 rng(42); // 1. 全パターン生成 vector all_patterns; all_patterns.reserve(30240); for (int i = 0; i <= 99999; i++) { int tmp = i, m = 0; int8_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; for (int i = 0; i < 30240; i++) candidates.push_back(i); vector is_solved(30240, false); int solved_count = 0; int next_guess_idx = 0; // "01234" while (solved_count < 30) { // --- クエリ出力 (flush) --- for (int i = 0; i < 5; i++) cout << (int)all_patterns[next_guess_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] == 30) break; // --- ターゲット管理の更新 --- // このクエリ自体が正解だった場合、以降そのスロットは(5,0)を返すようになる if (!is_solved[next_guess_idx] && res_freq[30] > solved_count) { is_solved[next_guess_idx] = true; } solved_count = res_freq[30]; // --- 厳密フィルタリング --- int unknown_target_res[36]; for(int j=0; j<36; j++) unknown_target_res[j] = res_freq[j]; // すでに発見済みの正解数を(5,0)の度数から引く int known_in_res = 0; for(int i=0; i<30240; i++) if(is_solved[i]) known_in_res++; unknown_target_res[30] -= known_in_res; vector next_candidates; for (int c_idx : candidates) { if (is_solved[c_idx]) continue; Result r = get_hb(all_patterns[next_guess_idx], all_patterns[c_idx]); if (unknown_target_res[r] > 0) { next_candidates.push_back(c_idx); } } candidates = move(next_candidates); // --- 次の推論 (エントロピー最大化) --- if (candidates.size() <= (size_t)(30 - solved_count)) { for(int c : candidates) { if(!is_solved[c]) { next_guess_idx = c; break; } } } else { double max_entropy = -1.0; // 時間計測 auto now = chrono::steady_clock::now(); auto elapsed = chrono::duration_cast(now - start_time).count(); // 残り時間に応じた動的パラメータ調整 int num_test_queries = (candidates.size() < 500) ? 500 : 150; int sample_size = (candidates.size() < 1000) ? candidates.size() : 600; // 5秒制限のうち、4.5秒までを探索に使う if (elapsed > 4500) { next_guess_idx = candidates[0]; } else { for (int i = 0; i < num_test_queries; i++) { // 候補から選ぶ(80%)、全体から選ぶ(20%) int q_idx = (rng() % 10 < 8) ? candidates[rng() % candidates.size()] : rng() % 30240; if (is_solved[q_idx]) continue; int counts[36] = {0}; for (int s = 0; s < sample_size; s++) { int s_idx = candidates[rng() % candidates.size()]; counts[get_hb(all_patterns[q_idx], all_patterns[s_idx])]++; } double entropy = 0; for (int j = 0; j < 36; j++) { if (counts[j] > 0) { double p = (double)counts[j] / sample_size; entropy -= p * log(p); } } if (entropy > max_entropy) { max_entropy = entropy; next_guess_idx = q_idx; } } } } } return 0; }