結果

問題 No.1701 half price
ユーザー 👑 obakyanobakyan
提出日時 2022-01-09 20:25:11
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 37 ms / 3,000 ms
コード長 1,458 bytes
コンパイル時間 112 ms
コンパイル使用メモリ 5,396 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-09 04:52:45
合計ジャッジ時間 2,336 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 7 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 7 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 37 ms
4,380 KB
testcase_07 AC 37 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 15 ms
4,376 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 37 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local bls, brs = bit.lshift, bit.rshift
local bxor = bit.bxor

local function grayCode(x)
  return bxor(x, brs(x, 1))
end

-- add_func(idx), rm_func(idx), work_func()
local function grayWalk(size, add_func, rm_func, work_func)
  local prv = 0
  local total = bls(1, size) - 1
  local bpos = {}
  for i = 1, size do
    bpos[bls(1, i - 1)] = i
  end
  work_func()
  for i = 1, total do
    local v = grayCode(i)
    if prv < v then
      prv, v = v, v - prv
      add_func(bpos[v])
    else
      prv, v = v, prv - v
      rm_func(bpos[v])
    end
    work_func()
  end
end


local n, w = io.read("*n", "*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end

local ret = 0
local box = {}
local subbox = {}
local subcnt = 0
local subv = 0
local f = false
local function subadd(idx)
  subv = subv - brs(a[subbox[idx]], 1)
end
local function subrm(idx)
  subv = subv + brs(a[subbox[idx]], 1)
end
local function subwork()
  if w == subv then
    f = true
  end
end

for i = 1, n do box[i] = false end
local function add_func(idx)
  box[idx] = true
end
local function rm_func(idx)
  box[idx] = false
end
local function work_func()
  subcnt = 0
  subv = 0
  f = false
  for i = 1, n do
    if box[i] then
      subcnt = subcnt + 1
      subbox[subcnt] = i
      subv = subv + a[i]
    end
  end
  grayWalk(subcnt, subadd, subrm, subwork)
  if f then ret = ret + 1 end
end
grayWalk(n, add_func, rm_func, work_func)
if w == 0 then ret = ret - 1 end
print(ret)
0