結果

問題 No.911 ラッキーソート
ユーザー 👑 obakyanobakyan
提出日時 2020-04-25 10:16:49
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,762 ms / 2,000 ms
コード長 3,944 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 146,280 KB
最終ジャッジ日時 2024-11-06 18:55:07
合計ジャッジ時間 33,771 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,248 KB
testcase_02 AC 2 ms
5,248 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 3 ms
5,248 KB
testcase_10 AC 3 ms
5,248 KB
testcase_11 AC 3 ms
5,248 KB
testcase_12 AC 3 ms
5,248 KB
testcase_13 AC 1,599 ms
145,888 KB
testcase_14 AC 1,529 ms
145,888 KB
testcase_15 AC 1,487 ms
145,528 KB
testcase_16 AC 1,450 ms
141,644 KB
testcase_17 AC 1,461 ms
142,000 KB
testcase_18 AC 1,436 ms
141,436 KB
testcase_19 AC 1,488 ms
141,792 KB
testcase_20 AC 1,438 ms
140,548 KB
testcase_21 AC 1,252 ms
137,360 KB
testcase_22 AC 1,363 ms
133,464 KB
testcase_23 AC 1,202 ms
125,828 KB
testcase_24 AC 1,054 ms
113,452 KB
testcase_25 AC 567 ms
73,300 KB
testcase_26 AC 467 ms
55,452 KB
testcase_27 AC 183 ms
27,624 KB
testcase_28 AC 91 ms
14,656 KB
testcase_29 AC 30 ms
6,144 KB
testcase_30 AC 7 ms
5,248 KB
testcase_31 AC 3 ms
5,248 KB
testcase_32 AC 3 ms
5,248 KB
testcase_33 AC 3 ms
5,248 KB
testcase_34 AC 3 ms
5,248 KB
testcase_35 AC 3 ms
5,248 KB
testcase_36 AC 3 ms
5,248 KB
testcase_37 AC 4 ms
5,248 KB
testcase_38 AC 1,762 ms
146,280 KB
testcase_39 AC 1,173 ms
128,240 KB
testcase_40 AC 26 ms
5,888 KB
testcase_41 AC 1,437 ms
140,500 KB
testcase_42 AC 778 ms
86,828 KB
testcase_43 AC 1,561 ms
145,344 KB
testcase_44 AC 994 ms
145,432 KB
testcase_45 AC 1,045 ms
142,252 KB
testcase_46 AC 1,115 ms
141,892 KB
testcase_47 AC 939 ms
144,920 KB
testcase_48 AC 791 ms
133,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local bls, brs = bit.lshift, bit.rshift
local mfl, mce = math.floor, math.ceil
local band = bit.band

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 n, l, r = io.read():match("(%d+) (%d+) (%d+)")
n = tonumber(n)
l = lltonumber(l)
r = lltonumber(r)
local t = {}
local countlim = 64
-- 1 or 3: can use "0", 2 or 3: can use "1"
for i = 1, countlim do
  t[i] = 3
end
local astr_all = io.read()
local allbits = {}
for astr in astr_all:gmatch("%d+") do
  allbits[#allbits + 1] = {}
  local i = #allbits
  if #astr < 11 then
    local a = tonumber(astr)
    for j = 1, countlim do
      allbits[i][j] = a % 2
      a = mfl(a / 2)
    end
  else
    local top = astr:sub(1, 9)
    local z = 10^(#astr - 9)
    top = tonumber(top)
    local bottom = astr:sub(10, #astr)
    bottom = tonumber(bottom)
    for j = 1, countlim do
      allbits[i][j] = bottom % 2
      if top % 2 == 1 then
        top = mfl(top / 2)
        bottom = bottom + z
        bottom = mfl(bottom / 2)
      else
        top = mfl(top / 2)
        bottom = mfl(bottom / 2)
      end
    end
  end
  -- local a = lltonumber(astr)
  -- allbits[#allbits + 1] = {}
  -- local i = #allbits
  -- for j = 1, countlim do
  --   allbits[i][j] = a % 2LL == 1LL and 1 or 0
  --   a = a / 2LL
  -- end
end
local valid = true
local t1, t2, t3 = {countlim}, {1}, {n}
while 0 < #t1 do
  local pos, left, right = t1[#t1], t2[#t2], t3[#t3]
  table.remove(t1) table.remove(t2) table.remove(t3)
  if left == right then
    if 1 < pos then
      table.insert(t1, pos - 1) table.insert(t2, left) table.insert(t3, right)
    end
  else
    local lval = allbits[left][pos]
    local rval = allbits[right][pos]
    if lval == rval then
      for i = left + 1, right - 1 do
        if allbits[i][pos] ~= lval then
          valid = false break
        end
      end
      if not valid then break end
      if 1 < pos then
        table.insert(t1, pos - 1) table.insert(t2, left) table.insert(t3, right)
      end
    else
      local turnpos = nil
      for i = left + 1, right do
        if allbits[i][pos] ~= allbits[i - 1][pos] then
          if turnpos then valid = false break
          else
            turnpos = i
          end
        end
      end
      if not valid then break end
      if lval == 0 then
        t[pos] = band(t[pos], 1)
      else
        t[pos] = band(t[pos], 2)
      end
      if t[pos] == 0 then valid = false break end
      if pos ~= 1 then
        table.insert(t1, pos - 1) table.insert(t2, left) table.insert(t3, turnpos - 1)
        table.insert(t1, pos - 1) table.insert(t2, turnpos) table.insert(t3, right)
      end
    end
  end
end
-- print(valid)
-- print(table.concat(t, " "))
local function getCount(max)
  local maxbits = {}
  for i = 1, countlim do
    maxbits[i] = max % 2LL == 1LL and 1 or 0
    max = max / 2LL
  end
  local dpmax, dpnormal = {}, {}
  for i = 1, countlim do
    dpmax[i] = 0
    dpnormal[i] = 0
  end
  dpmax[countlim] = 1LL
  dpnormal[countlim] = 0LL
  for i = countlim - 1, 1, -1 do
    if t[i] == 3 then
      dpmax[i] = dpmax[i + 1]
      dpnormal[i] = dpnormal[i + 1] * 2LL
      if maxbits[i] == 1 then
        dpnormal[i] = dpnormal[i] + dpmax[i + 1]
      end
    elseif t[i] == 2 then
      -- only "1" is available
      if maxbits[i] == 0 then
        dpmax[i] = 0
      else
        dpmax[i] = dpmax[i + 1]
      end
      dpnormal[i] = dpnormal[i + 1]
    else -- t[i] == 1
      -- only "0" is available
      if maxbits[i] == 0 then
        dpmax[i] = dpmax[i + 1]
        dpnormal[i] = dpnormal[i + 1]
      else
        dpmax[i] = 0
        dpnormal[i] = dpnormal[i + 1] + dpmax[i + 1]
      end
    end
  end
  return dpnormal[1] + dpmax[1]
end
if valid then
  local cnt = getCount(r)
  if 0 < l then
    cnt = cnt - getCount(l - 1)
  end
  local str = tostring(cnt):gsub("LL", "")
  print(str)
else
  print(0)
end
0