結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー obakyanobakyan
提出日時 2020-05-06 16:40:45
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 2,912 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 5,248 KB
実行使用メモリ 12,844 KB
最終ジャッジ日時 2024-06-28 18:20:49
合計ジャッジ時間 1,481 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 38 ms
7,384 KB
testcase_11 AC 44 ms
6,688 KB
testcase_12 AC 119 ms
8,612 KB
testcase_13 AC 31 ms
5,908 KB
testcase_14 AC 45 ms
6,432 KB
testcase_15 AC 27 ms
5,376 KB
testcase_16 AC 44 ms
6,220 KB
testcase_17 AC 20 ms
5,376 KB
testcase_18 AC 120 ms
8,612 KB
testcase_19 AC 77 ms
6,300 KB
testcase_20 AC 75 ms
12,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

local function getgcd(x, y)
  while 0 < x do
    x, y = y % x, x
  end
  return y
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 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, m, k = io.read("*n", "*n", "*n", "*l")
local s = io.read()
local b = {}
for ss in s:gmatch("%d+") do
  table.insert(b, tonumber(ss))
end
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
if s:sub(1, 1) == "+" then
  local t = {}
  for i = 1, m do
    local rem = b[i] % k
    if not t[rem] then t[rem] = 1
    else t[rem] = t[rem] + 1 end
  end
  local ans = 0
  for i = 1, n do
    local rem = a[i] % k
    rem = (k - rem) % k
    if t[rem] then ans = ans + t[rem] end
  end
  print(ans)
  os.exit()
end
local primes = getprimes(33333)
local divs = getdivisor(k, primes)
local bp = {}
for i = 1, #divs do
  bp[divs[i]] = 0
end
for ib = 1, m do
  local gcd = getgcd(b[ib], k)
  bp[gcd] = bp[gcd] + 1
end
local bpsum = {}
for i = 1, #divs do
  local dv = divs[i]
  local inv = mfl(k / dv)
  local invs = getdivisor(inv, primes)
  local cnt = 0
  for i = 1, #invs do
    cnt = cnt + bp[invs[i] * dv]
  end
  bpsum[dv] = cnt
end

local ans = 0
for ia = 1, n do
  local gcd = getgcd(a[ia], k)
  local invgcd = mfl(k / gcd)
  ans = ans + bpsum[invgcd]
end
print(ans)
0