結果

問題 No.822 Bitwise AND
ユーザー 👑 obakyanobakyan
提出日時 2019-04-26 22:40:37
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,080 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 5,332 KB
実行使用メモリ 11,092 KB
最終ジャッジ日時 2023-09-08 01:10:31
合計ジャッジ時間 6,718 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

local n, k = io.read("*n", "*n")
-- 2^a + n - (2^a - 1) <= k
if(n < k) then print("INF")
else
  local len = 0
  local lastzero = -1
  local zeropos = {}
  local tmpn = n
  while(0 < tmpn) do
    len = len + 1
    local s = tmpn % 2
    if(s == 0) then
      lastzero = len
      table.insert(zeropos, len)
    end
    tmpn = math.floor(tmpn / 2)
  end
  if(lastzero == -1) then
    print(1)
  else
    local allcnt = 0
    local a_base = 0
    local arb_count = #zeropos
    local iter = 3^arb_count - 1
    for i = 0, iter do
      local i_tmp = i
      local a_cand = a_base
      local b_cand = 0
      local mult = 1
      for k_tmp = 1, arb_count do
        local s = i_tmp % 3
        if(s == 0) then a_cand = a_cand + 2^(zeropos[mult] - 1)
        elseif(s == 1) then b_cand = b_cand + 2^(zeropos[mult] - 1)
        end
        mult = mult + 1
        i_tmp = math.floor(i_tmp / 3)
      end
      local flg = (b_cand <= a_cand) and (a_cand - b_cand <= k)
      --print(i, a_cand, b_cand, flg)
      if(flg) then allcnt = allcnt + 1 end
    end
    print(allcnt)
  end
end
0