結果

問題 No.1594 Three Classes
ユーザー 👑 obakyanobakyan
提出日時 2021-07-19 21:44:51
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,460 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 5,356 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-10 07:04:33
合計ジャッジ時間 1,480 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 6 ms
4,384 KB
testcase_04 AC 15 ms
4,384 KB
testcase_05 AC 14 ms
4,384 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 7 ms
4,384 KB
testcase_08 AC 14 ms
4,380 KB
testcase_09 AC 15 ms
4,388 KB
testcase_10 AC 14 ms
4,384 KB
testcase_11 AC 14 ms
4,388 KB
testcase_12 AC 14 ms
4,384 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 7 ms
4,380 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 7 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 6 ms
4,384 KB
testcase_20 AC 6 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local bls, brs = bit.lshift, bit.rshift
local bxor = bit.bxor
local bor, band = bit.bor, bit.band

local function grayWalk3(size, work_func, box)
  assert(size <= 15)
  local prv = 0
  local total = 3^size - 1
  local rep = 3^(size-1)
  local bpos = {}
  for i = 1, size do
    bpos[bls(1, i - 1)] = i
  end
  local v2 = 0
  for i = 1, size do
    box[i] = 0
  end
  local function incOne()
    if box[1] == 0 then
      box[1] = 1
      v2 = v2 + 1
    elseif box[1] == 1 then
      box[1] = 2
    else
      box[1] = 0
      v2 = v2 - 1
    end
    work_func(1)
  end
  local function incX()
    local z = band(v2, -v2) * 2
    local p = bpos[z]
    if box[p] == 0 then
      box[p] = 1
      v2 = v2 + z
    elseif box[p] == 1 then
      box[p] = 2
    else
      box[p] = 0
      v2 = v2 - z
    end
    work_func(p)
  end
  -- if need first work then use below
  -- work_func(1)

  incOne()
  incOne()
  for i = 1, rep - 1 do
    incX()
    incOne()
    incOne()
  end
end

local n = io.read("*n")
local t = {}
local pa, pb, pc = 0, 0, 0
for i = 1, n do
  t[i] = io.read("*n")
  pa = pa + t[i]
end
local graybox = {}
local function work_func(idx)
  if graybox[idx] == 1 then
    pa = pa - t[idx]
    pb = pb + t[idx]
  elseif graybox[idx] == 2 then
    pb = pb - t[idx]
    pc = pc + t[idx]
  else
    pc = pc - t[idx]
    pa = pa + t[idx]
  end
  if pa == pb and pb == pc then print("Yes") os.exit() end
end
grayWalk3(n, work_func, graybox)
print("No")

0