結果

問題 No.1651 Removing Cards
ユーザー 👑 obakyan
提出日時 2021-08-20 23:33:22
言語 Lua
(LuaJit 2.1.1734355927)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,431 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 6,692 KB
実行使用メモリ 35,372 KB
最終ジャッジ日時 2024-10-14 07:28:14
合計ジャッジ時間 32,565 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

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 mfl = math.floor

local function comp(a, b)
  return a < b
end

local function lower_bound(ary, x)
  local num = #ary
  if num == 0 then return 1 end
  if not comp(ary[1], x) then return 1 end
  if comp(ary[num], x) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if comp(ary[mid], x) then
      min = mid
    else
      max = mid
    end
  end
  return max
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
-- for i = 1, #t do
  -- print(i, t[i], ret[i])
-- end
-- print(t[1])
-- print(#t, t[#t])
-- os.exit()

for iq = 1, q do
  local n = io.read()
  n = lltonumber(n)
  if n == 1LL then
    print(1)
  else
    local pos = lower_bound(t, n)
    if pos == 1 then
      print(2)
    else
      local ans = tostring(t[pos - 1] + 1LL):gsub("LL", "")
      print(ans)
    end
  end
end
-- print(os.clock())
0