結果

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

ソースコード

diff #
raw source code

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

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<Guess> 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<int> 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<all_g.size(); ++i) 
                if(all_g[i].d[0]==0 && all_g[i].d[1]==1 && all_g[i].d[2]==2 && all_g[i].d[3]==3 && all_g[i].d[4]==4) { best_idx = i; break; }
        } else if (turn == 1) {
            // 二手目: 56789 (全数字をカバー)
            for(int i=0; i<all_g.size(); ++i) 
                if(all_g[i].d[0]==5 && all_g[i].d[1]==6 && all_g[i].d[2]==7 && all_g[i].d[3]==8 && all_g[i].d[4]==9) { best_idx = i; break; }
        } else if (candidates.size() <= (size_t)remaining_targets) {
            // 候補数と残りターゲットが一致したら確定
            best_idx = candidates[0];
        } else {
            // 戦略3: エントロピーによる最適化
            double max_e = -1.0;
            auto now = chrono::steady_clock::now();
            int elapsed = chrono::duration_cast<chrono::milliseconds>(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::milliseconds>(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<pair<int, int>, 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<int> 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;
}
0