結果

問題 No.1267 Stop and Coin Game
ユーザー 👑 obakyan
提出日時 2022-09-30 00:05:19
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 917 ms / 2,000 ms
コード長 1,862 bytes
コンパイル時間 407 ms
コンパイル使用メモリ 6,688 KB
実行使用メモリ 47,016 KB
最終ジャッジ日時 2024-12-22 18:36:39
合計ジャッジ時間 8,770 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

local bls, brs = bit.lshift, bit.rshift
local ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
long long atoll(const char*);
]]

local function lltonumber(str)
  return C.atoll(str)
end

local asum = 0LL

local function bdp_prepare(n, cost, lim)
  local tot = bls(1, n)
  local alltask = {}
  local stagetask = {}
  for i = 1, n do
    stagetask[i] = {}
  end
  for i = 1, tot - 1 do
    local z = asum
    local ti = i
    local cnt = 0
    for j = 1, n do
      if ti % 2 == 1 then
        cnt = cnt + 1
        z = z - cost[j]
      end
      ti = brs(ti, 1)
    end
    table.insert(stagetask[cnt], i)
    if lim < z then
      alltask[i] = 0
    else
      alltask[i] = 1
    end
    -- print(i, z, lim, z <= lim)
  end
  return tot, alltask, stagetask
end

local function bdp_doall(n, alltask, stagetask, cost, lim)
  for stage = 1, n do
    local stlen = #stagetask[stage]
    for i_stagetask = 1, stlen do
      local used = stagetask[stage][i_stagetask]
      -- print(used, alltask[used])
      if alltask[used] == 1 then
        alltask[used] = false
      end
      if not alltask[used] then
        local mul = 1
        local tmp = used
        for j = 1, n do
          if tmp % 2 == 0 then
            alltask[used + mul] = true
          end
          tmp = brs(tmp, 1)
          mul = mul * 2
        end
      end
    end
  end
end

local function bdp_getresult(tot, alltask)
  return alltask[tot - 1]
end

local n, v = io.read():match("(%d+) (%d+)")
n = tonumber(n)
v = lltonumber(v)
local s = io.read()
local a = {}
for w in s:gmatch("%d+") do
  local z = lltonumber(w)
  table.insert(a, z)
end

for i = 1, n do asum = asum + a[i] end
if asum <= v then
  print("Draw")
  os.exit()
end

local tot, alltask, stagetask = bdp_prepare(n, a, v)
bdp_doall(n, alltask, stagetask, a, v)
local ret = alltask[tot - 1]
print(ret and "First" or "Second")
0