結果

問題 No.979 Longest Divisor Sequence
ユーザー 👑 obakyanobakyan
提出日時 2020-03-30 23:51:23
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,076 ms / 2,000 ms
コード長 2,170 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 5,216 KB
実行使用メモリ 11,724 KB
最終ジャッジ日時 2023-09-05 11:40:07
合計ジャッジ時間 3,380 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
6,316 KB
testcase_01 AC 7 ms
6,304 KB
testcase_02 AC 6 ms
6,192 KB
testcase_03 AC 6 ms
6,308 KB
testcase_04 AC 6 ms
6,192 KB
testcase_05 AC 7 ms
6,128 KB
testcase_06 AC 6 ms
6,188 KB
testcase_07 AC 6 ms
6,304 KB
testcase_08 AC 7 ms
6,128 KB
testcase_09 AC 7 ms
6,312 KB
testcase_10 AC 20 ms
7,988 KB
testcase_11 AC 19 ms
8,016 KB
testcase_12 AC 20 ms
8,068 KB
testcase_13 AC 148 ms
11,724 KB
testcase_14 AC 1,076 ms
11,672 KB
testcase_15 AC 384 ms
11,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mmi, mma = math.min, math.max
local mfl, mce = math.floor, math.ceil
local msq = math.sqrt
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 tmp = {}
  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 t = {}
      t.p = dv
      t.cnt = 1
      x = x / dv
      while x % dv == 0 do
        x = x / dv
        t.cnt = t.cnt + 1
      end
      table.insert(tmp, t)
      lim = mce(msq(x))
    end
    if primepos == prime_num then break end
    primepos = primepos + 1
    dv = primes[primepos]
  end
  if x ~= 1 then
    local t = {}
    t.p, t.cnt = x, 1
    table.insert(tmp, t)
  end
  return tmp
end

local function getdivisorCore(divisorparts)
  local t = {}
  local pat = 1
  local len = #divisorparts
  local allpat = 1
  for i = 1, len do
    allpat = allpat * (1 + divisorparts[i].cnt)
  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 / (divisorparts[i].cnt + 1))
      local mul = mfl(i_pat / div)
      i_pat = i_pat % div
      for j = 1, mul do
        ret = ret * divisorparts[i].p
      end
    end
    table.insert(t, ret)
  end
  -- table.sort(t)
  return t
end

local function getdivisor(x, primes)
  local dvp = getdivisorparts(x, primes)
  return getdivisorCore(dvp)
end

local n = io.read("*n")
local map = {}
for i = 1, 300000 do
  map[i] = 0
end
local primes = getprimes(mce(msq(300000)))
for i = 1, n do
  local a = io.read("*n")
  local facts = getdivisor(a, primes)
  local max = 0
  for j = 1, #facts do
    if facts[j] ~= a then
      max = mma(max, map[facts[j]])
    end
  end
  map[a] = mma(map[a], max + 1)
end
local ret = 0
for i = 1, 300000 do
  ret = mma(ret, map[i])
end
print(ret)
0