結果

問題 No.2848 Birthday Hit and Blow
ユーザー hos.lyrichos.lyric
提出日時 2024-08-23 21:48:09
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 800 ms / 2,000 ms
コード長 3,610 bytes
コンパイル時間 2,115 ms
コンパイル使用メモリ 162,484 KB
実行使用メモリ 25,196 KB
平均クエリ数 442.50
最終ジャッジ日時 2024-08-23 21:48:15
合計ジャッジ時間 4,933 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 726 ms
25,196 KB
testcase_01 AC 800 ms
24,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }

string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum D = [0, 31,29,31,30,31,30,31,31,30,31,30,31];

int U;
int[5][5][] graph;
string[] query, answer;
int newNode() {
  const u = U++;
  graph.length += 1;
  query ~= "?";
  answer ~= "?";
  return u;
}

int dfs(int dep, string[] ss) {
  const u = newNode;
  if (ss.length <= 1) {
    answer[u] = ss.length ? ss[0] : "empty";
  } else {
    assert(dep < 6);
    int mn = int.max;
    int qm = -1;
    foreach (q; 0 .. 10^^4) {
      const r = format("%04d", q);
      bool ok = true;
      foreach (i; 0 .. 4) foreach (j; i + 1 .. 4) ok = ok && (r[i] != r[j]);
      if (ok) {
        string[][5][5] tss;
        foreach (s; ss) {
          int hit, blow;
          foreach (i; 0 .. 4) foreach (j; 0 .. 4) if (r[i] == s[j]) (i == j) ? ++hit : ++blow;
          tss[hit][blow] ~= s;
        }
        int mx;
        foreach (hit; 0 .. 5) foreach (blow; 0 .. 5) chmax(mx, cast(int)(tss[hit][blow].length));
        if (chmin(mn, mx)) qm = q;
      }
    }
    {
      const q = qm;
      const r = format("%04d", q);
      query[u] = r;
      // stderr.writeln(dep, " ", ss, ": ", r);
      bool ok = true;
      foreach (i; 0 .. 4) foreach (j; i + 1 .. 4) ok = ok && (r[i] != r[j]);
      if (ok) {
        string[][5][5] tss;
        foreach (s; ss) {
          int hit, blow;
          foreach (i; 0 .. 4) foreach (j; 0 .. 4) if (r[i] == s[j]) (i == j) ? ++hit : ++blow;
          tss[hit][blow] ~= s;
        }
        foreach (hit; 0 .. 5) foreach (blow; 0 .. 5) {
          const v = dfs(dep + 1, tss[hit][blow]);
          graph[u][hit][blow] = v;
        }
      }
    }
  }
  return u;
}

void main() {
  string[] ini;
  foreach (m; 1 .. 12 + 1) foreach (d; 1 .. D[m] + 1) {
    const s = format("%02d%02d", m, d);
    bool ok = true;
    foreach (i; 0 .. 4) foreach (j; i + 1 .. 4) ok = ok && (s[i] != s[j]);
    if (ok) {
      // stderr.writeln(s);
      ini ~= s;
    }
  }
  // stderr.writeln("|ini| = ", ini.length);
  const rt = dfs(0, ini);
  
  const numCases = readInt;
  foreach (caseId; 0 .. numCases) {
    int u = rt;
    for (; answer[u] == "?"; ) {
      writefln("? %s", query[u]); stdout.flush;
      const H = readInt;
      const B = readInt;
      if (H == -1 && B == -1) return;
      // if (H == -1 && B == -1) assert(false);
      u = graph[u][H][B];
    }
    writefln("! %s", answer[u]); stdout.flush;
    const A = readInt;
    if (A == -1) return;
    // if (A == -1) assert(false);
  }
}
0