結果

問題 No.1731 Product of Subsequence
ユーザー 👑 obakyanobakyan
提出日時 2022-05-08 20:17:02
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 422 ms / 2,000 ms
コード長 2,868 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-25 17:58:40
合計ジャッジ時間 4,213 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 295 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 13 ms
6,940 KB
testcase_09 AC 5 ms
6,940 KB
testcase_10 AC 167 ms
6,944 KB
testcase_11 AC 149 ms
6,944 KB
testcase_12 AC 8 ms
6,944 KB
testcase_13 AC 9 ms
6,940 KB
testcase_14 AC 143 ms
6,940 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 422 ms
6,940 KB
testcase_19 AC 10 ms
6,940 KB
testcase_20 AC 222 ms
6,940 KB
testcase_21 AC 166 ms
6,940 KB
testcase_22 AC 266 ms
6,944 KB
testcase_23 AC 5 ms
6,944 KB
testcase_24 AC 9 ms
6,940 KB
testcase_25 AC 8 ms
6,940 KB
testcase_26 AC 33 ms
6,944 KB
testcase_27 AC 42 ms
6,940 KB
testcase_28 AC 77 ms
6,944 KB
testcase_29 AC 29 ms
6,940 KB
testcase_30 AC 251 ms
6,940 KB
testcase_31 AC 261 ms
6,940 KB
testcase_32 AC 4 ms
6,944 KB
testcase_33 AC 24 ms
6,944 KB
testcase_34 AC 15 ms
6,940 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 getlcm(x, y)
  local gcd = getgcd(x, y)
  return mfl(x / gcd) * 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 = mfl(x / dv)
      while x % dv == 0 do
        x = mfl(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 ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
long long atoll(const char*);
]]

local function lltonumber(str)
  return C.atoll(str)
end

local function getgcdll(x, y)
  while 0LL < x do
    x, y = y % x, x
  end
  return y
end

local n, k = io.read("*n", "*n", "*l")
local primes = getprimes(mce(msq(1000000007)))
local dv = getdivisor(k, primes)
local dn = #dv
local dvmap = {}
for i = 1, dn do
  dvmap[dv[i]] = i
end
local mod = 1000000007
local function badd(x, y) return (x + y) % mod end
local t = {}
for i = 1, dn do
  t[i] = 0
end
t[1] = 1

local kll = 1LL * k
local s = io.read()
for w in s:gmatch("(%d+)") do
  w = lltonumber(w)
  local gcd = getgcdll(w, kll)
  w = tostring(gcd):gsub("LL", "")
  local a = tonumber(w)
  for i = dn, 1, -1 do
    local dst = getgcd(a, mfl(k / dv[i])) * dv[i]
    dst = dvmap[dst]
    t[dst] = badd(t[dst], t[i])
  end
end
if k == 1 then
  t[dn] = (t[dn] + mod - 1) % mod
end
print(t[dn])
0