結果

問題 No.1651 Removing Cards
ユーザー 👑 obakyanobakyan
提出日時 2024-11-07 21:31:11
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,958 ms / 2,000 ms
コード長 2,543 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 5,248 KB
実行使用メモリ 53,680 KB
最終ジャッジ日時 2024-11-07 21:31:47
合計ジャッジ時間 32,383 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 326 ms
11,520 KB
testcase_03 AC 3 ms
5,248 KB
testcase_04 AC 3 ms
5,248 KB
testcase_05 AC 3 ms
5,248 KB
testcase_06 AC 3 ms
5,248 KB
testcase_07 AC 3 ms
5,248 KB
testcase_08 AC 3 ms
5,248 KB
testcase_09 AC 216 ms
21,804 KB
testcase_10 AC 212 ms
21,948 KB
testcase_11 AC 221 ms
21,920 KB
testcase_12 AC 229 ms
21,948 KB
testcase_13 AC 214 ms
21,928 KB
testcase_14 AC 222 ms
21,972 KB
testcase_15 AC 1,940 ms
49,276 KB
testcase_16 AC 1,915 ms
48,852 KB
testcase_17 AC 1,882 ms
50,216 KB
testcase_18 AC 1,745 ms
41,800 KB
testcase_19 AC 1,844 ms
43,484 KB
testcase_20 AC 1,568 ms
45,116 KB
testcase_21 AC 1,714 ms
41,800 KB
testcase_22 AC 1,958 ms
42,224 KB
testcase_23 AC 263 ms
22,868 KB
testcase_24 AC 211 ms
19,944 KB
testcase_25 AC 1,752 ms
39,608 KB
testcase_26 AC 1,489 ms
38,452 KB
testcase_27 AC 1,711 ms
53,680 KB
testcase_28 AC 1,837 ms
46,392 KB
testcase_29 AC 1,863 ms
48,684 KB
testcase_30 AC 1,875 ms
48,028 KB
testcase_31 AC 1,911 ms
47,312 KB
testcase_32 AC 124 ms
10,924 KB
testcase_33 AC 128 ms
11,648 KB
testcase_34 AC 131 ms
11,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local pow2 = {1}
for i = 2, 28 do pow2[i] = pow2[i - 1] * 2 end
local function mergesort(ary, buf, sortfunc)
  local n = #ary
  local rep, mul = 0, 1
  while mul < n do
    rep, mul = rep + 1, mul * 2
  end
  for irep = 1, rep do
    local src = irep % 2 == 1 and ary or buf
    local dst = irep % 2 == 1 and buf or ary
    local sz = pow2[irep]
    local boxcnt = pow2[rep - irep + 1]
    for i = 1, boxcnt do
      local il = 1 + sz * 2 * (i - 1)
      local ir = il + sz
      local llim = ir - 1
      local rlim = llim + sz
      local id = il
      local rem = sz * 2
      if n <= llim then
        for k = il, n do dst[k] = src[k] end
        break
      elseif n <= rlim then
        rlim = n
        rem = n - il + 1
      end
      for j = 1, rem do
        if sortfunc(src[ir], src[il]) then
          dst[id] = src[ir] ir = ir + 1
        else
          dst[id] = src[il] il = il + 1
        end
        id = id + 1
        if llim < il then
          for k = 0, rem - j - 1 do dst[id + k] = src[ir + k] end
          break
        elseif rlim < ir then
          for k = 0, rem - j - 1 do dst[id + k] = src[il + k] end
          break
        end
      end
    end
  end
  -- local sorted_tbl_is_written_in_ary1 = rep % 2 == 0
  return rep % 2 == 0
end

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 k, q = io.read("*n", "*n", "*l")
k = 1LL * k
local lim = 1000000007LL * 1000000007LL

local function solve(x)
  local min, max = x - 1LL, x * 2LL + 10
  while 1LL < max - min do
    local mid = (min + max) / 2LL
    local v = (mid + k - 1LL) / k
    if x + 1LL <= mid - v then
      max = mid
    else
      min = mid
    end
  end
  return max - 1LL
end

local t = {}
local sz = 1LL
while sz < lim do
  sz = solve(sz)
  table.insert(t, sz)
end
-- print(t[1])
-- print(#t, t[#t])
-- os.exit()

local a = {}
local aidx = {}
local ans = {}
for iq = 1, q do
  aidx[iq] = iq
  local z = io.read()
  z = string.rep("0", 19 - #z) .. z
  a[iq] = z
  ans[iq] = 0
end
local buf = {}
local f = mergesort(aidx, buf, function(x, y) return a[x] < a[y] end)
if not f then aidx = buf end

local tpos = 1
for iq = 1, q do
  local i = aidx[iq]
  local n = lltonumber(a[i])
  if n == 1LL then
    ans[i] = 1
  else
    while tpos <= #t and t[tpos] < n do
      tpos = tpos + 1
    end
    if tpos == 1 then
      ans[i] = 2
    else
      ans[i] = tostring(t[tpos - 1] + 1LL):gsub("LL", "")
    end
  end
end
print(table.concat(ans, "\n"))

0