結果

問題 No.3418 【絶望】30個並列ごちゃ混ぜHit&Blowで遊ぼう!
コンテスト
ユーザー LNG
提出日時 2025-12-25 13:56:38
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
TLE  
実行時間 -
コード長 5,285 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,151 ms
コンパイル使用メモリ 195,136 KB
実行使用メモリ 34,544 KB
スコア 0
最終ジャッジ日時 2025-12-25 13:56:53
合計ジャッジ時間 14,565 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other TLE * 1 -- * 99
権限があれば一括ダウンロードができます

ソースコード

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 Result;

struct Guess {
    int8_t d[5];
    int16_t mask;
};

// 高速判定関数
inline Result get_hb(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++;
    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. 全パターン生成 (30,240通り)
    vector<Guess> all_patterns;
    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<int> candidates;
    for (int i = 0; i < (int)all_patterns.size(); i++) candidates.push_back(i);
    
    vector<bool> is_target_found(all_patterns.size(), false);
    int found_count = 0;
    int next_guess_idx = 0; // 最初は 01234

    // エントロピー計算用の事前計算テーブル
    double log_table[30241];
    for (int i = 0; i <= 30240; i++) log_table[i] = (i <= 1) ? 0 : log2((double)i);

    while (found_count < 30) {
        // --- クエリフェーズ ---
        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; cin >> h >> b;
            if (h == -1) return 0;
            res_freq[h * 6 + b]++;
        }

        if (res_freq[30] == 30) break;

        // --- フィルタリングとターゲット特定 ---
        // (5,0)が既に特定済みの数より多ければ、今のクエリが正解の一つだったということ
        int total_known_found = 0;
        for(int i=0; i<all_patterns.size(); ++i) if(is_target_found[i]) total_known_found++;

        if (res_freq[30] > total_known_found && !is_target_found[next_guess_idx]) {
            is_target_found[next_guess_idx] = true;
            total_known_found++;
        }
        found_count = res_freq[30];

        // 未知のターゲットが生成したHB結果の分布を作成
        int unknown_res_freq[36];
        for (int j = 0; j < 36; j++) unknown_res_freq[j] = res_freq[j];
        unknown_res_freq[30] -= total_known_found;

        vector<int> next_candidates;
        for (int c_idx : candidates) {
            if (is_target_found[c_idx]) continue;
            Result res = get_hb(all_patterns[next_guess_idx], all_patterns[c_idx]);
            // ヒストグラムに存在する結果を出す候補のみ残す
            if (unknown_res_freq[res] > 0) {
                next_candidates.push_back(c_idx);
            }
        }
        candidates = move(next_candidates);

        // --- 次の推論フェーズ ---
        if (candidates.size() <= (size_t)(30 - found_count)) {
            // 残り少なければ直接候補を試す
            for(int c : candidates) {
                if(!is_target_found[c]) { next_guess_idx = c; break; }
            }
        } else {
            // エントロピー最大化による探索
            double max_entropy = -1.0;
            auto now = chrono::steady_clock::now();
            auto elapsed = chrono::duration_cast<chrono::milliseconds>(now - start_time).count();
            
            // 実行時間に応じてサンプル数を調整(後半ほど精密に)
            int sample_size = (candidates.size() < 800) ? candidates.size() : 800;
            int attempt_limit = (elapsed < 4000) ? 500 : 100; // 時間が厳しくなれば試行回数を減らす

            for (int t = 0; t < attempt_limit; t++) {
                // 候補内から選ぶか全体から選ぶか
                int q_idx = (rng() % 10 < 7) ? candidates[rng() % candidates.size()] : rng() % all_patterns.size();
                if (is_target_found[q_idx]) continue;

                int counts[36] = {0};
                // サンプリングによる分布推定
                for (int i = 0; i < sample_size; i++) {
                    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) entropy -= (double)counts[j] * log_table[counts[j]];
                }

                if (entropy > max_entropy) {
                    max_entropy = entropy;
                    next_guess_idx = q_idx;
                }
            }
        }
    }

    return 0;
}
0