結果

問題 No.1876 Xor of Sum
ユーザー 👑 obakyanobakyan
提出日時 2022-03-16 09:55:03
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,233 ms / 2,000 ms
コード長 1,684 bytes
コンパイル時間 325 ms
コンパイル使用メモリ 5,260 KB
実行使用メモリ 6,812 KB
最終ジャッジ日時 2023-10-24 14:28:17
合計ジャッジ時間 15,366 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 119 ms
4,348 KB
testcase_04 AC 320 ms
4,348 KB
testcase_05 AC 9 ms
4,348 KB
testcase_06 AC 628 ms
4,716 KB
testcase_07 AC 12 ms
4,348 KB
testcase_08 AC 10 ms
4,348 KB
testcase_09 AC 280 ms
4,348 KB
testcase_10 AC 84 ms
4,348 KB
testcase_11 AC 444 ms
4,348 KB
testcase_12 AC 118 ms
4,348 KB
testcase_13 AC 246 ms
4,348 KB
testcase_14 AC 173 ms
4,348 KB
testcase_15 AC 218 ms
4,348 KB
testcase_16 AC 7 ms
4,348 KB
testcase_17 AC 515 ms
4,348 KB
testcase_18 AC 638 ms
4,716 KB
testcase_19 AC 629 ms
4,716 KB
testcase_20 AC 623 ms
4,716 KB
testcase_21 AC 633 ms
4,716 KB
testcase_22 AC 645 ms
4,716 KB
testcase_23 AC 1,223 ms
4,716 KB
testcase_24 AC 1,233 ms
4,716 KB
testcase_25 AC 1,228 ms
4,716 KB
testcase_26 AC 1,225 ms
4,716 KB
testcase_27 AC 1,223 ms
4,716 KB
testcase_28 AC 1,194 ms
6,812 KB
testcase_29 AC 4 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local bls, brs = bit.lshift, bit.rshift
local bor, band = bit.bor, bit.band
local bxor = bit.bxor
local TFDP = {}

TFDP.initialize = function(self, lim)
  self.lim = lim
  self.sz = 30
  self.way = true
  self.bcnt = mce((lim + 1) / self.sz)
  self.b1, self.b2 = {1}, {}
  for i = 2, self.bcnt do
    self.b1[i] = 0
    self.b2[i] = 0
  end
  self.curmax = 0
end

TFDP.add = function(self, v, cursum)
  local src = self.way and self.b1 or self.b2
  local dst = self.way and self.b2 or self.b1
  self.way = not self.way
  local bcnt = self.bcnt
  local bc2 = mce((cursum + 1) / self.sz)
  for i = 1, bc2 do
    dst[i] = src[i]
  end
  local sz = self.sz
  local jump, rem = mfl(v / sz), v % sz
  local denom, mul = bls(1, sz - rem), bls(1, rem)
  for i = 1, bcnt - jump do
    if bc2 < i then break end
    local v1 = src[i] % denom
    dst[i + jump] = bxor(dst[i + jump], v1 * mul)
  end
  for i = 1, bcnt - jump - 1 do
    if bc2 < i then break end
    local v2 = brs(src[i], sz - rem)
    dst[i + jump + 1] = bxor(dst[i + jump + 1], v2)
  end
end

TFDP.query = function(self, x)
  local group = mfl(x / self.sz) + 1
  local idx = x - (group - 1) * self.sz
  local tbl = self.way and self.b1 or self.b2
  return band(tbl[group], bls(1, idx)) ~= 0
end

local tf = TFDP
local n = io.read("*n")
local asum = 0
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
  asum = asum + a[i]
end
tf:initialize(asum)
local cursum = 0
for i = 1, n do
  local ai = a[i]
  tf:add(ai, cursum)
  cursum = cursum + ai
end
-- print(os.clock())
local ret = 0
for i = 1, asum do
  if tf:query(i) then
    ret = bxor(ret, i)
  end
end
print(ret)
-- print(os.clock())
0