結果

問題 No.1245 ANDORゲーム(calc)
ユーザー 👑 obakyan
提出日時 2021-08-12 14:55:29
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 359 ms / 2,000 ms
コード長 1,113 bytes
コンパイル時間 52 ms
コンパイル使用メモリ 5,248 KB
実行使用メモリ 50,036 KB
最終ジャッジ日時 2024-10-01 19:39:11
合計ジャッジ時間 7,045 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local n, q = io.read("*n", "*n")
local t = {}
for i = 1, 30 do t[i] = {} end
for i = 1, n do
  local a = io.read("*n")
  for j = 1, 30 do
    t[j][i] = a % 2
    a = mfl(a / 2)
  end
end
io.read()
local s = io.read()
local tp = {}
for i = 1, n do
  tp[i] = s:byte(i) == 48
end

local score_0 = {}
local score_1 = {}

for i = 1, 30 do
  local s0, s1 = 0, 0
  local c0, c1 = 0, 1
  for j = 1, n do
    if tp[j] then -- AND
      if t[i][j] == 0 then
        if c0 == 1 then s0 = s0 + 1 end
        if c1 == 1 then s1 = s1 + 1 end
        c0, c1 = 0, 0
      end
    else -- OR
      if t[i][j] == 1 then
        if c0 == 0 then s0 = s0 + 1 end
        if c1 == 0 then s1 = s1 + 1 end
        c0, c1 = 1, 1
      end
    end
  end
  score_0[i], score_1[i] = s0, s1
end

for iq = 1, q do
  local a = io.read("*n")
  local mul = 1
  local ret = 0
  for i = 1, 30 do
    if a % 2 == 1 then
      ret = ret + score_1[i] * mul
    else
      ret = ret + score_0[i] * mul
    end
    a = mfl(a / 2)
    mul = mul * 2
  end
  ret = tostring(ret * 1LL):gsub("LL", "")
  print(ret)
end
0