結果

問題 No.1538 引きこもりさんは引き算が得意。
ユーザー 👑 obakyan
提出日時 2021-12-31 23:19:23
言語 Lua
(LuaJit 2.1.1734355927)
結果
WA  
実行時間 -
コード長 2,711 bytes
コンパイル時間 447 ms
コンパイル使用メモリ 7,204 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-09 06:10:29
合計ジャッジ時間 1,996 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local bxor = bit.bxor
local bor, band = bit.bor, bit.band

-- work_func(idx)
-- v = [0, 1, 2]
local function grayWalk3(size, work_func, box)
  assert(size <= 15)
  local prv = 0
  local total = 3^size - 1
  local rep = 3^(size-1)
  local bpos = {}
  for i = 1, size do
    bpos[bls(1, i - 1)] = i
  end
  local v2 = 0
  for i = 1, size do
    box[i] = 0
  end
  local function incOne()
    if box[1] == 0 then
      box[1] = 1
      v2 = v2 + 1
    elseif box[1] == 1 then
      box[1] = 2
    else
      box[1] = 0
      v2 = v2 - 1
    end
    work_func(1)
  end
  local function incX()
    local z = band(v2, -v2) * 2
    local p = bpos[z]
    if box[p] == 0 then
      box[p] = 1
      v2 = v2 + z
    elseif box[p] == 1 then
      box[p] = 2
    else
      box[p] = 0
      v2 = v2 - z
    end
    work_func(p)
  end
  work_func(1)

  incOne()
  incOne()
  for i = 1, rep - 1 do
    incX()
    incOne()
    incOne()
  end
end

local n, k = io.read("*n", "*n")
local a = {}
local v = 0
for i = 1, n do
  a[i] = io.read("*n")
  v = v - a[i]
end
local function finish() print("Yes") os.exit() end
for i = 1, n do if a[i] == k then finish() end end

local large = 10 < n
local tail_allplus, tail_allminus = false, false
local tail = {}
local graybox = {}
if large then
  local tn = n - 10
  local v = 0
  local cnt1 = 0
  local cnt2 = 0
  local function work_func(idx)
    if graybox[idx] == 0 then
      if 0 < cnt2 then
        cnt2 = cnt2 - 1
        v = v - a[10 + idx]
      end
    elseif graybox[idx] == 1 then
      cnt1 = cnt1 + 1
      v = v - a[10 + idx]
    else
      cnt1 = cnt1 - 1
      cnt2 = cnt2 + 1
      v = v + 2 * a[10 + idx]
    end
    if cnt1 == tn then
      tail_allminus = v
    elseif cnt2 == tn then
      tail_allplus = v
    else
      if 0 < cnt1 or 0 < cnt2 then
        tail[v] = true
      end
    end
  end
  grayWalk3(tn, work_func, graybox)
end

if tail[k] then
  finish()
end

do
  local tn = mmi(10, n)
  local v = 0
  local cnt1 = 0
  local cnt2 = 0
  local function work_func(idx)
    if graybox[idx] == 0 then
      if 0 < cnt2 then
        cnt2 = cnt2 - 1
        v = v - a[idx]
      end
    elseif graybox[idx] == 1 then
      cnt1 = cnt1 + 1
      v = v - a[idx]
    else
      cnt1 = cnt1 - 1
      cnt2 = cnt2 + 1
      v = v + 2 * a[idx]
    end
    local z = k - v
    if tail[z] then finish() end
    if 0 < cnt1 then
      if tail_allplus == z then finish() end
    end
    if 0 < cnt2 then
      if tail_allminus == z then finish() end
    end
    if 0 < cnt1 and 0 < cnt2 then
      if z == 0 then finish() end
    end
  end
  grayWalk3(tn, work_func, graybox)
end
print("No")
0