結果

問題 No.1730 GCD on Blackboard in yukicoder
ユーザー 👑 obakyanobakyan
提出日時 2022-01-18 10:58:00
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 634 ms / 2,000 ms
コード長 2,306 bytes
コンパイル時間 243 ms
コンパイル使用メモリ 6,688 KB
実行使用メモリ 39,080 KB
最終ジャッジ日時 2024-04-25 17:56:08
合計ジャッジ時間 7,086 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
15,920 KB
testcase_01 AC 10 ms
10,872 KB
testcase_02 AC 10 ms
10,788 KB
testcase_03 AC 13 ms
10,788 KB
testcase_04 AC 20 ms
10,832 KB
testcase_05 AC 13 ms
10,892 KB
testcase_06 AC 19 ms
10,952 KB
testcase_07 AC 20 ms
10,856 KB
testcase_08 AC 20 ms
10,784 KB
testcase_09 AC 13 ms
10,900 KB
testcase_10 AC 22 ms
11,100 KB
testcase_11 AC 11 ms
10,784 KB
testcase_12 AC 10 ms
10,832 KB
testcase_13 AC 538 ms
38,704 KB
testcase_14 AC 527 ms
38,956 KB
testcase_15 AC 545 ms
38,312 KB
testcase_16 AC 585 ms
38,696 KB
testcase_17 AC 550 ms
38,448 KB
testcase_18 AC 467 ms
30,320 KB
testcase_19 AC 634 ms
39,080 KB
testcase_20 AC 75 ms
14,480 KB
testcase_21 AC 80 ms
14,420 KB
testcase_22 AC 70 ms
15,764 KB
testcase_23 AC 59 ms
13,820 KB
testcase_24 AC 62 ms
13,792 KB
testcase_25 AC 534 ms
34,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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 ptbl, ctbl = {}, {}
  local lim = mce(msq(x))
  local primepos = 1
  local dv = primes[primepos]
  while primepos <= prime_num and dv <= lim do
    if x % dv == 0 then
      local cnt = 1
      x = mfl(x / dv)
      while x % dv == 0 do
        x = mfl(x / dv)
        cnt = cnt + 1
      end
      table.insert(ptbl, dv)
      table.insert(ctbl, 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(ptbl, x)
    table.insert(ctbl, 1)
  end
  return ptbl, ctbl
end

local function getdivisorCore(ptbl, ctbl, box, mul)
  local pat = 1
  local len = #ptbl
  local allpat = 1
  for i = 1, len do
    allpat = allpat * (1 + ctbl[i])
  end
  for t_i_pat = 0, allpat - 1 do
    local div = allpat
    local i_pat = t_i_pat
    local ret = 1
    for i = 1, len do
      div = mfl(div / (ctbl[i] + 1))
      local mul = mfl(i_pat / div)
      i_pat = i_pat % div
      for j = 1, mul do
        ret = ret * ptbl[i]
      end
    end
    box[ret] = box[ret] + mul
  end
end

local function getdivisor(x, primes, box, mul)
  local ptbl, ctbl = getdivisorparts(x, primes)
  getdivisorCore(ptbl, ctbl, box, mul)
end

local n = io.read("*n")
local box = {}
for i = 1, 1000000 do
  box[i] = 0
end
local primes = getprimes(1000)
local amap = {}
for i = 1, n do
  local a = io.read("*n")
  if amap[a] then amap[a] = amap[a] + 1 else amap[a] = 1 end
end
for a, cnt in pairs(amap) do
  getdivisor(a, primes, box, cnt)
end
local ret = {}
for i = 1, n do
  ret[i] = 0
end
for i = 1, 1000000 do
  local cnt = box[i]
  if 0 < cnt then
    local rem = n - cnt
    ret[rem + 1] = i
  end
end
for i = 2, n do
  ret[i] = ret[i] < ret[i - 1] and ret[i - 1] or ret[i]
end
print(table.concat(ret, "\n"))
-- print(os.clock())
0