結果

問題 No.1611 Minimum Multiple with Double Divisors
ユーザー 👑 obakyanobakyan
提出日時 2024-08-04 15:16:07
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 3,630 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 7,068 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-08-04 15:16:39
合計ジャッジ時間 28,978 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 730 ms
7,040 KB
testcase_02 AC 735 ms
7,040 KB
testcase_03 AC 727 ms
7,040 KB
testcase_04 AC 740 ms
7,040 KB
testcase_05 AC 722 ms
7,040 KB
testcase_06 AC 812 ms
7,040 KB
testcase_07 AC 705 ms
7,040 KB
testcase_08 AC 731 ms
7,040 KB
testcase_09 AC 775 ms
7,040 KB
testcase_10 AC 299 ms
7,040 KB
testcase_11 TLE -
testcase_12 AC 1,782 ms
6,944 KB
testcase_13 AC 1,769 ms
7,040 KB
testcase_14 AC 1,773 ms
6,944 KB
testcase_15 AC 1,770 ms
7,040 KB
testcase_16 AC 1,585 ms
7,040 KB
testcase_17 AC 1,773 ms
7,040 KB
testcase_18 AC 1,757 ms
7,040 KB
testcase_19 AC 18 ms
7,040 KB
testcase_20 AC 18 ms
7,040 KB
testcase_21 AC 19 ms
7,040 KB
testcase_22 AC 16 ms
6,944 KB
testcase_23 AC 19 ms
7,040 KB
testcase_24 AC 20 ms
7,040 KB
testcase_25 AC 19 ms
7,040 KB
testcase_26 AC 18 ms
7,040 KB
testcase_27 AC 16 ms
7,040 KB
testcase_28 AC 10 ms
7,040 KB
testcase_29 AC 9 ms
7,040 KB
testcase_30 AC 9 ms
7,040 KB
testcase_31 AC 10 ms
6,944 KB
testcase_32 AC 10 ms
6,944 KB
testcase_33 AC 9 ms
7,040 KB
testcase_34 AC 10 ms
7,040 KB
testcase_35 AC 10 ms
7,040 KB
testcase_36 AC 11 ms
7,040 KB
testcase_37 AC 10 ms
7,040 KB
testcase_38 AC 9 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 keys = {}
local vals = {}
local kn = 0
local function getdivisorparts(x, primes)
  local prime_num = #primes
  local lim = mce(msq(x))
  local primepos = 1
  local dv = primes[primepos]
  kn = 0
  while primepos <= prime_num and dv <= lim do
    if x % dv == 0 then
      x = mfl(x / dv)
      local cnt = 1
      while x % dv == 0 do
        x = mfl(x / dv)
        cnt = cnt + 1
      end
      kn = kn + 1
      keys[kn] = dv
      vals[kn] = cnt
      lim = mce(msq(x))
    end
    if primepos == prime_num then break end
    primepos = primepos + 1
    dv = primes[primepos]
  end
  if x ~= 1 then
    kn = kn + 1
    keys[kn] = x
    vals[kn] = 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, kn do
    tot = tot * (1 + vals[i])
  end
  local insed = {}
  local hq = Heapq.new(function(a, b) return a < b end)
  for i = 1, kn 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, kn 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))
      if top == 1 then break end
    end
    if tot * 2 == tot2 then
      ans = top_
      break
    end
    for i = 1, kn 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