結果

問題 No.1538 引きこもりさんは引き算が得意。
ユーザー 👑 obakyanobakyan
提出日時 2021-12-31 23:34:15
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 2,742 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 6,676 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-21 22:14:02
合計ジャッジ時間 2,446 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 12 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 10 ms
6,676 KB
testcase_06 AC 10 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 10 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 6 ms
6,676 KB
testcase_15 AC 10 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 4 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 1 ms
6,676 KB
testcase_20 AC 1 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 1 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 4 ms
6,676 KB
testcase_29 AC 1 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 1 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 1 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 20 ms
6,676 KB
testcase_38 AC 20 ms
6,676 KB
testcase_39 AC 20 ms
6,676 KB
testcase_40 AC 20 ms
6,676 KB
testcase_41 AC 19 ms
6,676 KB
testcase_42 AC 20 ms
6,676 KB
testcase_43 AC 1 ms
6,676 KB
testcase_44 AC 15 ms
6,676 KB
testcase_45 AC 21 ms
6,676 KB
testcase_46 AC 14 ms
6,676 KB
testcase_47 AC 14 ms
6,676 KB
testcase_48 AC 27 ms
6,676 KB
testcase_49 AC 17 ms
6,676 KB
testcase_50 AC 1 ms
6,676 KB
testcase_51 AC 28 ms
6,676 KB
testcase_52 AC 25 ms
6,676 KB
testcase_53 AC 23 ms
6,676 KB
testcase_54 AC 29 ms
6,676 KB
testcase_55 AC 24 ms
6,676 KB
testcase_56 AC 28 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

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 = {}, {}
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 cnt2 == 0 and 0 < cnt1 then
      tail_allminus[v] = true
    elseif cnt1 == 0 and 0 < cnt2 then
      tail_allplus[v] = true
    else
      if 0 < cnt1 and 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