結果

問題 No.3418 【絶望】30個並列ごちゃ混ぜHit&Blowで遊ぼう!
コンテスト
ユーザー LNG
提出日時 2025-12-25 12:17:14
言語 C++17
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 4,569 ms / 5,000 ms
コード長 4,220 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,178 ms
コンパイル使用メモリ 113,068 KB
実行使用メモリ 26,356 KB
スコア 9,991,903
平均クエリ数 80.97
最終ジャッジ日時 2025-12-25 12:25:00
合計ジャッジ時間 465,805 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;

struct Result {
    int h, b;
};

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

inline Result get_hb_fast(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]);
    return {h, __builtin_popcount(q.mask & t.mask) - h};
}

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

    auto global_start = chrono::steady_clock::now();
    
    vector<Guess> all_g;
    vector<int> candidates;
    for (int i = 0; i <= 99999; i++) {
        int temp = i, m = 0, digs[5];
        bool ok = true;
        for (int j = 4; j >= 0; j--) {
            digs[j] = temp % 10;
            if (m & (1 << digs[j])) ok = false;
            m |= (1 << digs[j]);
            temp /= 10;
        }
        if (ok) {
            candidates.push_back(all_g.size());
            Guess g;
            g.mask = m;
            for(int j=0; j<5; j++) g.d[j] = digs[j];
            all_g.push_back(g);
        }
    }

    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    int found_total = 0;

    while (found_total < 30) {
        int best_idx = -1;
        auto current_now = chrono::steady_clock::now();
        double elapsed = chrono::duration_cast<chrono::milliseconds>(current_now - global_start).count();

        if (candidates.size() == all_g.size()) {
            for (int i = 0; i < (int)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 (candidates.size() <= (30 - found_total) || elapsed > 4500) {
            best_idx = candidates[0];
        } else {
            double max_e = -1.0;
            int s_size = (candidates.size() < 400) ? (int)candidates.size() : 400;
            auto turn_start = chrono::steady_clock::now();
            
            // 1ターンあたりの思考時間を残り時間に応じて調整(最大150ms)
            while (chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - turn_start).count() < 150) {
                int test_idx = candidates[rng() % candidates.size()];
                if (rng() % 4 == 0) test_idx = rng() % all_g.size();

                int counts[6][6] = {0};
                for (int i = 0; i < s_size; i++) {
                    Result r = get_hb_fast(all_g[test_idx], all_g[candidates[rng() % candidates.size()]]);
                    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] / s_size;
                            e -= p * log2(p);
                        }
                    }
                }
                if (e > max_e) { max_e = e; best_idx = test_idx; }
                
                // 緊急避難:全体で4.8秒超えたら即中断
                if (chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - global_start).count() > 4800) break;
            }
        }

        if (best_idx == -1) best_idx = candidates[0];
        for (int i = 0; i < 5; i++) cout << all_g[best_idx].d[i];
        cout << endl;

        int res_map[6][6] = {0};
        for (int i = 0; i < 30; i++) {
            int h, b;
            if (!(cin >> h >> b)) return 0;
            if (h == -1) return 0;
            res_map[h][b]++;
        }

        if (res_map[5][0] == 30) break;
        found_total = res_map[5][0];

        vector<int> next_c;
        next_c.reserve(candidates.size());
        for (int idx : candidates) {
            Result r = get_hb_fast(all_g[best_idx], all_g[idx]);
            if (r.h == 5 && r.b == 0) continue;
            if (res_map[r.h][r.b] > 0) {
                next_c.push_back(idx);
            }
        }
        if(!next_c.empty()) candidates = move(next_c);
    }

    return 0;
}
0