結果

問題 No.3418 【絶望】30個並列ごちゃ混ぜHit&Blowで遊ぼう!
コンテスト
ユーザー LNG
提出日時 2025-12-25 15:01:15
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
WA  
実行時間 -
コード長 5,227 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,218 ms
コンパイル使用メモリ 197,560 KB
実行使用メモリ 26,368 KB
スコア 299,588
平均クエリ数 76.92
最終ジャッジ日時 2025-12-25 15:01:54
合計ジャッジ時間 38,258 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 3 WA * 97
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using namespace std;

// ヒット・ブローの結果を1バイトで保持 (H*6 + B)
typedef uint8_t ResultCode;

struct Guess {
    uint8_t d[5];
    uint16_t mask;
    int id;
};

// 高速なヒット・ブロー計算
inline ResultCode get_hb_code(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 (ResultCode)(h * 6 + (common - h));
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    mt19937 rng(1337); // 固定シード

    // 全 30240 通りの生成
    vector<Guess> all_patterns;
    all_patterns.reserve(30240);
    for (int i = 1234; i <= 98765; 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 = (uint16_t)m;
            g.id = (int)all_patterns.size();
            for (int j = 0; j < 5; j++) g.d[j] = digs[j];
            all_patterns.push_back(g);
        }
    }

    vector<int> candidates;
    for (int i = 0; i < (int)all_patterns.size(); i++) candidates.push_back(i);

    vector<bool> is_solved(all_patterns.size(), false);
    int solved_count = 0;

    // n*log(n) の事前計算テーブル
    double nlogn_table[30241];
    for (int i = 0; i <= 30240; i++) nlogn_table[i] = (i <= 1) ? 0 : (double)i * log(i);

    while (solved_count < 30) {
        int best_guess_idx = -1;

        if (solved_count == 0 && candidates.size() == 30240) {
            // 初手は情報量の多い 01234 を固定
            for(int i=0; i<(int)all_patterns.size(); ++i) {
                if(all_patterns[i].d[0]==0 && all_patterns[i].d[1]==1 && 
                   all_patterns[i].d[2]==2 && all_patterns[i].d[3]==3 && all_patterns[i].d[4]==4) {
                    best_guess_idx = i; break;
                }
            }
        } else if (candidates.size() <= (size_t)(30 - solved_count)) {
            // 残り候補が少ない場合は、その中から未解決のものを優先(直接当てに行く)
            best_guess_idx = candidates[0];
        } else {
            double min_expected_size = 1e18;
            
            // 探索パラメータの動的調整
            int turn_limit = (candidates.size() > 5000) ? 400 : 1200;
            int sample_size = min((int)candidates.size(), 1000);
            
            vector<int> samples;
            if (candidates.size() <= (size_t)sample_size) {
                samples = candidates;
            } else {
                for (int i = 0; i < sample_size; i++) 
                    samples.push_back(candidates[rng() % candidates.size()]);
            }

            for (int i = 0; i < turn_limit; i++) {
                // 候補から選ぶか、全域から選ぶか(8:2)
                int q_idx = (rng() % 10 < 8) ? candidates[rng() % candidates.size()] : rng() % all_patterns.size();
                if (is_solved[q_idx]) continue;

                int counts[36] = {0};
                for (int s_idx : samples) {
                    counts[get_hb_code(all_patterns[q_idx], all_patterns[s_idx])]++;
                }

                // 情報期待値(Σ n*log(n) を最小化することはエントロピー最大化と等価)
                double score = 0;
                for (int j = 0; j < 36; j++) score += nlogn_table[counts[j]];

                if (score < min_expected_size) {
                    min_expected_size = score;
                    best_guess_idx = q_idx;
                }
            }
        }

        // クエリ出力
        for (int i = 0; i < 5; i++) cout << (int)all_patterns[best_guess_idx].d[i];
        cout << endl;

        // ジャッジからのヒント取得
        int current_res_freq[36] = {0};
        bool current_guess_correct = false;
        for (int i = 0; i < 30; i++) {
            int h, b;
            if (!(cin >> h >> b)) return 0;
            if (h == -1) return 0;
            if (h == 5) current_guess_correct = true;
            current_res_freq[h * 6 + b]++;
        }

        if (current_guess_correct && !is_solved[best_guess_idx]) {
            is_solved[best_guess_idx] = true;
            solved_count++;
        }

        if (solved_count == 30) break;

        // 候補の絞り込み
        vector<int> next_candidates;
        for (int c_idx : candidates) {
            if (is_solved[c_idx]) continue;
            
            ResultCode code = get_hb_code(all_patterns[best_guess_idx], all_patterns[c_idx]);
            // ジャッジが返したヒント集合の中に、その候補が作り得る結果が含まれているか
            if (current_res_freq[code] > 0) {
                next_candidates.push_back(c_idx);
            }
        }
        candidates = move(next_candidates);
    }

    return 0;
}
0