結果

問題 No.355 数当てゲーム(2)
ユーザー 👑 obakyan
提出日時 2020-04-11 22:08:25
言語 Lua
(LuaJit 2.1.1734355927)
結果
RE  
実行時間 -
コード長 2,028 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 6,684 KB
実行使用メモリ 25,464 KB
平均クエリ数 92.81
最終ジャッジ日時 2024-07-17 03:25:42
合計ジャッジ時間 9,132 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 RE * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local function ask(tbl)
  io.write(table.concat(tbl, " "))
  io.write("\n")
  io.flush()
  local ret = io.read()
  local hit, blow = ret:match("(%d) (%d)")
  hit, blow = tonumber(hit), tonumber(blow)
  return hit, blow
end

local function combination_get_map(n)
  local combmap = {}
  for i = 1, n do
    combmap[i] = {}
    local ret = 1
    for j = 1, i do
      ret = mfl(ret * (i + 1 - j) / j)
      combmap[i][j] = ret
    end
  end
  return combmap
end
local function combination_get_count(combmap, n, k)
  if k < 0 then return 0
  elseif n < k then return 0
  elseif k == 0 then return 1
  else return combmap[n][k]
  end
end

local function combination_get_array(combmap, n, k, idx)
  local rem = k
  local retary = {}
  for i = 1, n do
    local useCnt = combination_get_count(combmap, n - i, rem - 1)
    local unuseCnt = combination_get_count(combmap, n - i, rem)
    if idx <= useCnt then
      rem = rem - 1
      table.insert(retary, i)
    else
      idx = idx - useCnt
    end
  end
  return retary
end

local cmap = combination_get_map(10)
local cand = {}
for i = 1, 210 do
  local ary = combination_get_array(cmap, 10, 4, i)
  for j = 1, 4 do
    ary[j] = ary[j] - 1
  end
  local h, b = ask(ary)
  if h + b == 4 then
    for j = 1, 4 do cand[j] = ary[j] end
    break
  end
end

local function getpattern(n, patall, idx)
  local used = {}
  local retary = {}
  local div = patall
  for i = 1, n do used[i] = false end
  for i = n, 1, -1 do
    div = mfl(div / i)
    local v_idx = mfl(idx / div)
    idx = idx % div
    local tmp_idx = 0
    for j = 1, n do
      if not used[j] then
        if tmp_idx == v_idx then
          table.insert(retary, j)
          used[j] = true
          break
        else
          tmp_idx = tmp_idx + 1
        end
      end
    end
  end
  return retary
end

for ip = 0, 23 do
  local idxary = getpattern(4, 24, ip)
  local z = {}
  for j = 1, 4 do
    z[j] = cand[idxary[j]]
  end
  local h, b = ask(z)
  if h == 4 then break end
end
0