結果

問題 No.1611 Minimum Multiple with Double Divisors
ユーザー 👑 obakyanobakyan
提出日時 2024-08-04 15:24:51
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 3,509 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2024-08-04 15:25:22
合計ジャッジ時間 30,565 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 920 ms
7,040 KB
testcase_02 AC 944 ms
7,040 KB
testcase_03 AC 941 ms
7,040 KB
testcase_04 AC 970 ms
7,040 KB
testcase_05 AC 916 ms
7,040 KB
testcase_06 AC 926 ms
7,040 KB
testcase_07 AC 934 ms
7,040 KB
testcase_08 AC 939 ms
7,040 KB
testcase_09 AC 900 ms
7,040 KB
testcase_10 AC 358 ms
7,040 KB
testcase_11 TLE -
testcase_12 AC 1,595 ms
6,940 KB
testcase_13 AC 1,564 ms
7,040 KB
testcase_14 AC 1,618 ms
7,040 KB
testcase_15 AC 1,608 ms
7,040 KB
testcase_16 AC 1,614 ms
7,040 KB
testcase_17 AC 1,637 ms
7,040 KB
testcase_18 AC 1,591 ms
7,040 KB
testcase_19 AC 21 ms
6,944 KB
testcase_20 AC 21 ms
6,944 KB
testcase_21 AC 20 ms
7,040 KB
testcase_22 AC 20 ms
7,040 KB
testcase_23 AC 21 ms
7,040 KB
testcase_24 AC 21 ms
7,040 KB
testcase_25 AC 20 ms
7,040 KB
testcase_26 AC 19 ms
7,040 KB
testcase_27 AC 19 ms
7,040 KB
testcase_28 AC 10 ms
6,940 KB
testcase_29 AC 10 ms
6,944 KB
testcase_30 AC 11 ms
7,040 KB
testcase_31 AC 11 ms
6,944 KB
testcase_32 AC 11 ms
7,040 KB
testcase_33 AC 11 ms
7,040 KB
testcase_34 AC 11 ms
7,040 KB
testcase_35 AC 10 ms
7,040 KB
testcase_36 AC 11 ms
6,940 KB
testcase_37 AC 10 ms
7,040 KB
testcase_38 AC 11 ms
7,040 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 Heapq = {}
Heapq.create = function(self, lt)
  self.lt = lt
  self.cnt = 0
  self.t = {}
end

Heapq.push = function(self, v)
  local hqlt = self.lt
  local hqt = self.t
  local c = self.cnt + 1
  self.cnt = c
  hqt[c] = v
  while 1 < c do
    local p = brs(c, 1)
    if hqlt(hqt[c], hqt[p]) then
      hqt[c], hqt[p] = hqt[p], hqt[c]
      c = p
    else
      break
    end
  end
end

Heapq.pop = function(self)
  local hqlt = self.lt
  local hqt = self.t
  local ret = hqt[1]
  local c = self.cnt
  hqt[1] = hqt[c]
  c = c - 1
  self.cnt = c
  local p = 1
  while true do
    local d1, d2 = p * 2, p * 2 + 1
    if c < d1 then break
    elseif c < d2 then
      if hqlt(hqt[d1], hqt[p]) then
        hqt[d1], hqt[p] = hqt[p], hqt[d1]
      end
      break
    else
      if hqlt(hqt[d1], hqt[d2]) then
        if hqlt(hqt[d1], hqt[p]) then
          hqt[d1], hqt[p] = hqt[p], hqt[d1]
          p = d1
        else break
        end
      else
        if hqlt(hqt[d2], hqt[p]) then
          hqt[d2], hqt[p] = hqt[p], hqt[d2]
          p = d2
        else break
        end
      end
    end
  end
  return ret
end

Heapq.new = function(lt)
  local obj = {}
  setmetatable(obj, {__index = Heapq})
  obj:create(lt)
  return obj
end

local function getprimes(x)
  local primes = {}
  local allnums = {}
  for i = 1, x do allnums[i] = true end
  for i = 2, x do
    if allnums[i] then
      table.insert(primes, i)
      local lim = mfl(x / i)
      for j = 2, lim do
        allnums[j * i] = false
      end
    end
  end
  return primes
end

local function getdivisorparts(x, primes)
  local prime_num = #primes
  local lim = mce(msq(x))
  local primepos = 1
  local dv = primes[primepos]
  local keys = {}
  local vals = {}
  while dv and dv <= lim do
    if x % dv == 0 then
      local cnt = 0
      while x % dv == 0 do
        x = mfl(x / dv)
        cnt = cnt + 1
      end
      table.insert(keys, dv)
      table.insert(vals, cnt)
      lim = mce(msq(x))
    end
    primepos = primepos + 1
    dv = primes[primepos]
  end
  if x ~= 1 then
    table.insert(keys, x)
    table.insert(vals, 1)
  end
  return keys, vals
end

local ztime = 0
local primes = getprimes(mce(msq(100000000000)))
local q = io.read("*n")
for iq = 1, q do
  local x = io.read("*n")
  local v = false
  for i = 1, #primes do
    local p = primes[i]
    if x % p ~= 0 then
      v = p
      break
    end
  end
  local ans = 1 * v
  -- local z1 = os.clock()
  local keys, vals = getdivisorparts(x, primes)
  -- ztime = ztime + os.clock() - z1
  local tot = 1
  for i = 1, #vals do
    tot = tot * (1 + vals[i])
  end
  local insed = {}
  local hq = Heapq.new(function(a, b) return a < b end)
  for i = 1, #keys do
    hq:push(keys[i])
    insed[keys[i]] = true
  end
  while 0 < hq.cnt do
    local top = hq:pop()
    if ans < top then break end
    local top_ = top
    local tot2 = tot
    for i = 1, #keys do
      local k = keys[i]
      local v = 0
      while top % k == 0 do
        top = mfl(top / k)
        v = v + 1
      end
      tot2 = mfl(tot2 * (vals[i] + v + 1) / (vals[i] + 1))
    end
    if tot * 2 == tot2 then
      ans = top_
      break
    end
    for i = 1, #keys do
      local nxt = top_ * keys[i]
      if not insed[nxt] then
        insed[nxt] = true
        hq:push(nxt)
      end
    end
  end
  print(ans * x)
end
-- print(os.clock())
-- print(ztime)
0