#include #include #include #include #include #include #include #include using namespace std; struct Result { int h, b; bool operator<(const Result& o) const { return h != o.h ? h < o.h : b < o.b; } bool operator==(const Result& o) const { return h == o.h && b == o.b; } }; struct Guess { int d[5]; int 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 b = __builtin_popcount(q.mask & t.mask) - h; return {h, b}; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto g_start = chrono::steady_clock::now(); // 全候補 (30240通り) の生成 vector all_g; 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; for (int i = 0; i < (int)all_g.size(); i++) candidates.push_back(i); mt19937 rng(1337); int found_count = 0; int turn = 0; while (found_count < 30) { int best_idx = -1; int remaining_targets = 30 - found_count; // --- 戦略1: 次のクエリを選択 --- if (turn == 0) { // 初手: 01234 for(int i=0; i(now - g_start).count(); // 候補が絞られてきたらサンプル数を増やして精度を上げる int sample_limit = (candidates.size() < 500) ? candidates.size() : 400; int search_duration = (elapsed < 4000) ? 200 : 100; auto t_search_start = chrono::steady_clock::now(); while (chrono::duration_cast(chrono::steady_clock::now() - t_search_start).count() < search_duration) { // 候補から選ぶか、全集合から選ぶか(通常は候補から選ぶ方が強い) int t_idx = candidates[rng() % candidates.size()]; int counts[6][6] = {0}; for (int i = 0; i < sample_limit; i++) { int c_idx = candidates[rng() % candidates.size()]; Result r = get_hb(all_g[t_idx], all_g[c_idx]); counts[r.h][r.b]++; } 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] / sample_limit; e -= p * log2(p); } } } if (e > max_e) { max_e = e; best_idx = t_idx; } } } if (best_idx == -1) best_idx = candidates[0]; // --- 戦略4: クエリ実行 --- for (int i = 0; i < 5; i++) cout << all_g[best_idx].d[i]; cout << endl; fflush(stdout); // ジャッジからの30個の応答を記録 map, int> res_multiset; for (int i = 0; i < 30; i++) { int h, b; cin >> h >> b; if (h == -1) return 0; res_multiset[{h, b}]++; } found_count = res_multiset[{5, 0}]; if (found_count == 30) break; // --- 戦略2: マルチセットフィルタリング --- vector next_c; for (int idx : candidates) { Result r = get_hb(all_g[best_idx], all_g[idx]); // 今投げたクエリそのものが正解(5,0)だった場合、それは次回の候補からは外す if (r.h == 5) continue; // その候補が生成しうる(h,b)が、ジャッジの返答リストに含まれているか if (res_multiset[{r.h, r.b}] > 0) { next_c.push_back(idx); } } candidates = move(next_c); turn++; } return 0; }