結果

問題 No.847 Divisors of Power
ユーザー 👑 obakyanobakyan
提出日時 2019-07-07 12:45:44
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 2,723 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 5,460 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-07-29 01:11:56
合計ジャッジ時間 6,465 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,384 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 1,797 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 613 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 TLE -
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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 low, high = {}, {}
  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 asdf = {}
      asdf.p = dv
      asdf.cnt = 1
      x = x / dv
      while(x % dv == 0) do
        x = x / dv
        asdf.cnt = asdf.cnt + 1
      end
      if asdf.p <= 5 then
        table.insert(low, asdf)
      else
        table.insert(high, asdf)
      end
      lim = mce(msq(x))
    end
    if(primepos == prime_num) then break end
    primepos = primepos + 1
    dv = primes[primepos]
  end
  if(x ~= 1) then
    local asdf = {}
    asdf.p, asdf.cnt = x, 1
    table.insert(high, asdf)
  end
  return low, high
end

local function getdivisor(divisorparts, max)
  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
        if max < ret then break end
      end
    end
    if ret <= max then
      table.insert(t, ret)
    end
  end
  -- table.sort(t)
  return t
end

local n, k, m = io.read("*n", "*n", "*n")
local primes = getprimes(31623)
local dlow, dhigh = getdivisorparts(n, primes)
for i = 1, #dlow do
  local c = dlow[i].cnt * k
  local p = dlow[i].p
  local r = 1
  for j = 1, c do
    r = r * p
    if m < r then c = mma(1, j - 1) break end
  end
  dlow[i].cnt = c
end
for i = 1, #dhigh do
  local c = dhigh[i].cnt * k
  local p = dhigh[i].p
  local r = 1
  for j = 1, c do
    r = r * p
    if m < r then c = mma(1, j - 1) break end
  end
  dhigh[i].dcnt = c
end
local lows = getdivisor(dlow, m)
local tot = 0
for j = 1, #lows do
  local relmax = mfl(m / lows[j])
  for i = 1, #dhigh do
    local c = dhigh[i].dcnt
    local p = dhigh[i].p
    local r = 1
    for j = 1, c do
      r = r * p
      if relmax < r then c = mma(1, j - 1) break end
    end
    dhigh[i].cnt = c
  end
  local ret = getdivisor(dhigh, relmax)
  tot = tot + #ret
end
print(tot)
0