結果

問題 No.2016 Countdown Divisors
ユーザー 👑 obakyanobakyan
提出日時 2022-07-22 22:16:49
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 1,339 bytes
コンパイル時間 34 ms
コンパイル使用メモリ 5,216 KB
実行使用メモリ 44,656 KB
最終ジャッジ日時 2023-09-17 10:32:08
合計ジャッジ時間 3,804 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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 lim = mce(msq(x))
  local primepos = 1
  local dv = primes[primepos]
  local ans = 1
  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
      ans = ans * (cnt + 1)
      lim = mce(msq(x))
    end
    if primepos == prime_num then break end
    primepos = primepos + 1
    dv = primes[primepos]
  end
  if x ~= 1 then
    ans = ans * 2
  end
  return ans
end
local primes = getprimes(100000)
local t = {}
t[1] = 1
t[2] = 2
local function solve(n)
  if t[n] then return n end
  local z = getdivisorparts(n, primes)
  t[n] = n - z
  if n == z then
    t[n] = z
    return z
  end
  return solve(n - z)
end
local q = io.read("*n")
for iq = 1, q do
  local n = io.read("*n")
  local z = solve(n)
  print(z)
end
0