結果

問題 No.3418 【絶望】30個並列ごちゃ混ぜHit&Blowで遊ぼう!
コンテスト
ユーザー LNG
提出日時 2025-12-25 13:46:16
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
TLE  
実行時間 -
コード長 6,224 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,265 ms
コンパイル使用メモリ 203,076 KB
実行使用メモリ 42,900 KB
スコア 0
最終ジャッジ日時 2025-12-25 13:46:32
合計ジャッジ時間 15,124 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;

typedef uint8_t ResultCode;

struct Guess {
    int d[5];
    int mask;
};

// ヒット・ブロー判定(最速化)
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));
}

struct QueryHistory {
    int guess_idx;
    int freq[36];
};

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

    auto start_time = chrono::steady_clock::now();
    auto get_elapsed = [&]() {
        return chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start_time).count();
    };

    mt19937 rng(42);
    vector<Guess> all_patterns;
    all_patterns.reserve(30240);
    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) {
            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_solved(all_patterns.size(), false);
    vector<int> solved_indices;
    vector<QueryHistory> history;
    int solved_count = 0;

    double log_table[30241];
    for (int i = 0; i <= 30240; i++) log_table[i] = (i <= 1) ? 0 : log2((double)i);

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

        if (candidates.size() <= (size_t)(30 - solved_count)) {
            for (int c : candidates) if (!is_solved[c]) { best_guess_idx = c; break; }
        } else if (history.empty()) {
            // 初手は 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) {
                    best_guess_idx = i; break;
                }
            }
        } else {
            // --- 時間管理の修正 ---
            long long now = get_elapsed();
            // 残り時間の1/10〜1/15をこのターンの探索に充てる
            long long turn_limit = now + (4900 - now) / 10; 
            
            double max_entropy = -1.0;
            int sample_size = (candidates.size() < 1000) ? (int)candidates.size() : 400;
            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()]);
            }

            int attempts = 0;
            while (get_elapsed() < turn_limit) {
                attempts++;
                int q_idx;
                if (rng() % 10 < 7) q_idx = candidates[rng() % candidates.size()];
                else q_idx = 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])]++;

                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;
                    best_guess_idx = q_idx;
                }
                // 十分に探索したら抜ける(時間を節約)
                if (candidates.size() < 500 && attempts > 1000) break;
                if (attempts > 3000) break; 
            }
        }

        if (best_guess_idx == -1) best_guess_idx = candidates[0];

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

        // レスポンス取得
        QueryHistory cur_h;
        cur_h.guess_idx = best_guess_idx;
        for (int j = 0; j < 36; j++) cur_h.freq[j] = 0;
        for (int i = 0; i < 30; i++) {
            int h, b; 
            if(!(cin >> h >> b)) return 0;
            if (h == -1) return 0;
            cur_h.freq[h * 6 + b]++;
        }
        history.push_back(cur_h);

        // 解決状況更新
        if (!is_solved[best_guess_idx] && history.back().freq[30] > (int)solved_indices.size()) {
            is_solved[best_guess_idx] = true;
            solved_indices.push_back(best_guess_idx);
        }
        solved_count = history.back().freq[30];
        if (solved_count == 30) break;

        // --- フィルタリング ---
        vector<QueryHistory> unknown_history = history;
        for (auto &uh : unknown_history) {
            for (int s_idx : solved_indices) {
                if (uh.guess_idx == s_idx) uh.freq[30]--;
                else uh.freq[get_hb_code(all_patterns[uh.guess_idx], all_patterns[s_idx])]--;
            }
        }

        vector<int> next_candidates;
        next_candidates.reserve(candidates.size());
        for (int c_idx : candidates) {
            if (is_solved[c_idx]) continue;
            bool possible = true;
            for (const auto &uh : unknown_history) {
                ResultCode code = get_hb_code(all_patterns[uh.guess_idx], all_patterns[c_idx]);
                if (uh.freq[code] <= 0) {
                    possible = false;
                    break;
                }
            }
            if (possible) next_candidates.push_back(c_idx);
        }
        candidates = move(next_candidates);
        
        // 候補がいなくなったら、まだ解決していない全パターンから再構成(安全策)
        if (candidates.empty() && solved_count < 30) {
            for(int i=0; i<(int)all_patterns.size(); ++i) {
                if(!is_solved[i]) candidates.push_back(i);
            }
        }
    }

    return 0;
}
0