結果

問題 No.1514 Squared Matching
ユーザー 👑 obakyanobakyan
提出日時 2021-08-09 23:13:56
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 3,780 ms / 4,000 ms
コード長 2,393 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 5,152 KB
実行使用メモリ 101,060 KB
最終ジャッジ日時 2023-10-21 20:54:49
合計ジャッジ時間 47,254 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
35,060 KB
testcase_01 AC 3,610 ms
101,060 KB
testcase_02 AC 25 ms
35,060 KB
testcase_03 AC 25 ms
35,060 KB
testcase_04 AC 26 ms
35,060 KB
testcase_05 AC 24 ms
35,060 KB
testcase_06 AC 67 ms
36,748 KB
testcase_07 AC 627 ms
51,896 KB
testcase_08 AC 3,525 ms
101,060 KB
testcase_09 AC 3,015 ms
101,060 KB
testcase_10 AC 3,369 ms
101,060 KB
testcase_11 AC 3,550 ms
101,060 KB
testcase_12 AC 3,648 ms
101,060 KB
testcase_13 AC 3,634 ms
101,060 KB
testcase_14 AC 3,664 ms
101,060 KB
testcase_15 AC 3,743 ms
101,060 KB
testcase_16 AC 3,666 ms
101,060 KB
testcase_17 AC 3,675 ms
101,060 KB
testcase_18 AC 3,704 ms
101,060 KB
testcase_19 AC 3,737 ms
101,060 KB
testcase_20 AC 3,736 ms
101,060 KB
testcase_21 AC 3,780 ms
101,060 KB
testcase_22 AC 657 ms
51,896 KB
testcase_23 AC 1,347 ms
68,272 KB
testcase_24 AC 2,075 ms
68,272 KB
testcase_25 AC 2,583 ms
101,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mce, mfl, msq, mmi, mma, mab = math.ceil, math.floor, math.sqrt, math.min, math.max, math.abs
local bls, brs = bit.lshift, bit.rshift
local bor, band = bit.bor, bit.band
local BITBOX = {}

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

BITBOX.set = function(self, x)
  local group = mce(x / self.sz)
  local idx = x - (group - 1) * self.sz
  self.b1[group] = bor(self.b1[group], bls(1, idx - 1))
end

BITBOX.query = function(self, x)
  local group = mce(x / self.sz)
  local idx = x - (group - 1) * self.sz
  return band(self.b1[group], bls(1, idx - 1)) ~= 0
end
BITBOX.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = BITBOX})
  obj:initialize(n)
  return obj
end
local bbprime = BITBOX.new(50000000)
local bbsq = BITBOX.new(50000000)

local function comp(a, b)
  return a < b
end

local function lower_bound(ary, x)
  local num = #ary
  if num == 0 then return 1 end
  if not comp(ary[1], x) then return 1 end
  if comp(ary[num], x) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if comp(ary[mid], x) then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local function getprimes(x)
  local primes = {}
  local withsq = {1}
  for i = 2, x do
    if bbsq:query(i) then
      table.insert(withsq, i)
    end
    if not bbprime:query(i) then
      table.insert(primes, i)
      local lim = mfl(x / i)
      for j = 2, lim do
        bbprime:set(j * i)
      end
      if 4 < i then
        lim = mfl(x / (i * i))
        for j = 1, lim do
          if 0 < j % 4 and 0 < j % 9 then
            bbsq:set(j * i * i)
          end
        end
      end
    end
  end
  return primes, withsq
end

local n = io.read("*n")
local primes, withsq = getprimes(n)
-- print(table.concat(withsq, " "))
-- print(#primes, #withsq)
-- print(os.clock())
local t = {}
local baselim = 1
while (baselim + 1) * (baselim + 1) <= n do
  baselim = baselim + 1
end
local ans = baselim * baselim
for base = 1, baselim do
  local cnt = mfl(n / (base * base))
  local lb = lower_bound(withsq, cnt + 1)
  cnt = cnt - mfl(cnt / 4) - mfl(cnt / 9) + mfl(cnt / 36) - (lb - 1)
  ans = ans + cnt * (2 * base - 1)
end
print(ans)
-- print(os.clock())
0