結果

問題 No.1514 Squared Matching
ユーザー 👑 obakyanobakyan
提出日時 2021-08-09 23:05:16
言語 Lua
(LuaJit 2.1.1696795921)
結果
MLE  
実行時間 -
コード長 1,664 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 5,328 KB
実行使用メモリ 813,244 KB
最終ジャッジ日時 2023-10-21 20:46:06
合計ジャッジ時間 3,099 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 MLE -
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 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

local mce, mfl, msq, mmi, mma, mab = math.ceil, math.floor, math.sqrt, math.min, math.max, math.abs

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 allnums = {}
  local alive = {}
  for i = 1, x do allnums[i] = true end
  for i = 1, x do alive[i] = true end
  local withsq = {1}
  for i = 2, x do
    if not alive[i] then
      table.insert(withsq, i)
    end
    if allnums[i] then
      table.insert(primes, i)
      local lim = mfl(x / i)
      for j = 2, lim do
        allnums[j * i] = false
      end
      if 4 < i then
        lim = mfl(x / (i * i))
        for j = 1, lim do
          if 0 < j % 4 and 0 < j % 9 then
            alive[j * i * i] = false
          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)
0