結果

問題 No.3418 【絶望】30個並列ごちゃ混ぜHit&Blowで遊ぼう!
コンテスト
ユーザー LNG
提出日時 2025-12-25 13:00:49
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 1,988 ms / 5,000 ms
コード長 6,255 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,237 ms
コンパイル使用メモリ 195,604 KB
実行使用メモリ 26,356 KB
スコア 9,987,066
平均クエリ数 129.34
最終ジャッジ日時 2025-12-25 13:03:48
合計ジャッジ時間 177,334 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 100
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <random>
#include <chrono>

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<Guess> 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<int> candidates;
    for (int i = 0; i < (int)all.size(); i++) candidates.push_back(i);

    vector<bool> 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<chrono::milliseconds>(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::milliseconds>(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<int> 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;
}
0