結果
| 問題 | No.3418 【絶望】30個並列ごちゃ混ぜHit&Blowで遊ぼう! |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-12-25 14:53:15 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 297 ms / 5,000 ms |
| コード長 | 5,587 bytes |
| 記録 | |
| コンパイル時間 | 2,179 ms |
| コンパイル使用メモリ | 200,072 KB |
| 実行使用メモリ | 26,356 KB |
| スコア | 9,992,338 |
| 平均クエリ数 | 76.62 |
| 最終ジャッジ日時 | 2025-12-25 14:53:50 |
| 合計ジャッジ時間 | 34,447 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 100 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <random>
#include <chrono>
#include <map>
using namespace std;
// ハッシュ化された結果 (h*6 + b) を保持
typedef uint8_t ResultCode;
struct Guess {
uint8_t d[5]; // メモリ節約のため uint8_t に変更
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]);
// 共通の数字の数は bitmask の AND で popcount
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);
auto start_time = chrono::steady_clock::now();
mt19937 rng(42);
// 全 30240 通りの生成
vector<Guess> all_patterns;
all_patterns.reserve(30240);
for (int i = 0; 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;
candidates.reserve(30240);
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 * log2(n))
double nlogn_table[30241];
for (int i = 0; i <= 30240; i++) nlogn_table[i] = (i <= 1) ? 0 : (double)i * log2(i);
while (solved_count < 30) {
int best_guess_idx = -1;
if (candidates.size() <= (size_t)(30 - solved_count) && !candidates.empty()) {
// 残り候補が少ない場合は、その中から未解決のものを優先
best_guess_idx = candidates[0];
} else 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 {
double max_entropy = -1.0;
// 探索範囲の調整
int turn_limit = (candidates.size() > 1000) ? 500 : 1500;
int sample_size = min((int)candidates.size(), 800);
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++) {
// 候補から選ぶ確率を高めつつ全域探索
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])]++;
}
// エントロピー計算: -sum(p*log(p)) は sum(n*log(n)) の最小化と等価
double current_entropy = 0;
for (int j = 0; j < 36; j++) {
if (counts[j] > 0) current_entropy -= nlogn_table[counts[j]];
}
if (current_entropy > max_entropy) {
max_entropy = current_entropy;
best_guess_idx = q_idx;
}
}
}
if (best_guess_idx == -1) best_guess_idx = candidates[0];
// クエリ出力
for (int i = 0; i < 5; i++) cout << (int)all_patterns[best_guess_idx].d[i];
cout << endl;
// 返答取得
int current_res_freq[36] = {0};
for (int i = 0; i < 30; i++) {
int h, b;
if (!(cin >> h >> b)) return 0;
if (h == -1) return 0;
current_res_freq[h * 6 + b]++;
}
// 5H 0B (code 30) の増加を確認
int new_total_hits = current_res_freq[30];
if (!is_solved[best_guess_idx] && new_total_hits > solved_count) {
is_solved[best_guess_idx] = true;
}
solved_count = new_total_hits;
if (solved_count == 30) break;
// 候補の絞り込み (5H 0Bを除外したリザルトプールとの照合)
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]);
// 5H 0B以外のリザルトがジャッジから返ってきている場合のみ候補として残す
if (code < 30 && current_res_freq[code] > 0) {
next_candidates.push_back(c_idx);
}
}
candidates = move(next_candidates);
}
return 0;
}