結果

問題 No.1611 Minimum Multiple with Double Divisors
ユーザー 👑 obakyanobakyan
提出日時 2024-08-04 15:01:50
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 3,498 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 7,072 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-08-04 15:02:25
合計ジャッジ時間 31,885 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 885 ms
7,040 KB
testcase_02 AC 835 ms
7,040 KB
testcase_03 AC 885 ms
7,040 KB
testcase_04 AC 856 ms
7,040 KB
testcase_05 AC 895 ms
7,040 KB
testcase_06 AC 902 ms
7,040 KB
testcase_07 AC 913 ms
7,040 KB
testcase_08 AC 911 ms
7,040 KB
testcase_09 AC 912 ms
7,040 KB
testcase_10 AC 346 ms
7,040 KB
testcase_11 TLE -
testcase_12 AC 1,762 ms
7,040 KB
testcase_13 AC 1,794 ms
6,944 KB
testcase_14 AC 1,764 ms
6,944 KB
testcase_15 AC 1,775 ms
7,040 KB
testcase_16 AC 1,747 ms
7,040 KB
testcase_17 AC 1,835 ms
7,040 KB
testcase_18 AC 1,763 ms
7,040 KB
testcase_19 AC 24 ms
7,040 KB
testcase_20 AC 21 ms
7,040 KB
testcase_21 AC 20 ms
7,040 KB
testcase_22 AC 18 ms
7,040 KB
testcase_23 AC 21 ms
7,040 KB
testcase_24 AC 23 ms
7,040 KB
testcase_25 AC 21 ms
6,940 KB
testcase_26 AC 20 ms
7,040 KB
testcase_27 AC 18 ms
6,944 KB
testcase_28 AC 9 ms
6,940 KB
testcase_29 AC 10 ms
7,040 KB
testcase_30 AC 11 ms
6,944 KB
testcase_31 AC 11 ms
7,040 KB
testcase_32 AC 11 ms
6,944 KB
testcase_33 AC 11 ms
7,040 KB
testcase_34 AC 11 ms
6,940 KB
testcase_35 AC 10 ms
7,040 KB
testcase_36 AC 10 ms
7,040 KB
testcase_37 AC 10 ms
7,040 KB
testcase_38 AC 11 ms
6,944 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 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
      table.insert(keys, dv)
      table.insert(vals, cnt)
      lim = mce(msq(x))
    end
    if primepos == prime_num then break 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 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 keys, vals = getdivisorparts(x, primes)
  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())
0