結果

問題 No.1554 array_and_me
ユーザー 👑 obakyanobakyan
提出日時 2021-08-12 21:42:03
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 2,548 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-10 01:35:38
合計ジャッジ時間 5,558 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
6,816 KB
testcase_01 AC 68 ms
8,064 KB
testcase_02 AC 63 ms
7,808 KB
testcase_03 AC 60 ms
7,552 KB
testcase_04 AC 61 ms
7,552 KB
testcase_05 AC 63 ms
7,424 KB
testcase_06 AC 114 ms
8,960 KB
testcase_07 AC 117 ms
8,960 KB
testcase_08 AC 114 ms
8,960 KB
testcase_09 AC 113 ms
9,088 KB
testcase_10 AC 115 ms
8,960 KB
testcase_11 AC 41 ms
8,960 KB
testcase_12 AC 39 ms
9,088 KB
testcase_13 AC 41 ms
9,088 KB
testcase_14 AC 43 ms
9,088 KB
testcase_15 AC 40 ms
8,960 KB
testcase_16 AC 37 ms
6,940 KB
testcase_17 AC 37 ms
6,940 KB
testcase_18 AC 37 ms
6,940 KB
testcase_19 AC 36 ms
6,944 KB
testcase_20 AC 35 ms
6,944 KB
testcase_21 AC 102 ms
8,320 KB
testcase_22 AC 100 ms
8,320 KB
testcase_23 AC 98 ms
8,192 KB
testcase_24 AC 127 ms
8,832 KB
testcase_25 AC 98 ms
8,192 KB
testcase_26 AC 144 ms
10,880 KB
testcase_27 AC 114 ms
8,948 KB
testcase_28 AC 118 ms
8,704 KB
testcase_29 AC 120 ms
8,948 KB
testcase_30 AC 113 ms
9,088 KB
testcase_31 AC 115 ms
8,952 KB
testcase_32 AC 115 ms
9,024 KB
testcase_33 AC 110 ms
9,000 KB
testcase_34 AC 116 ms
8,704 KB
testcase_35 AC 132 ms
10,720 KB
testcase_36 AC 128 ms
10,732 KB
testcase_37 AC 104 ms
8,704 KB
testcase_38 AC 105 ms
8,704 KB
testcase_39 AC 107 ms
8,816 KB
testcase_40 AC 107 ms
8,692 KB
testcase_41 AC 78 ms
7,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local bls, brs = bit.lshift, bit.rshift
local mod = 998244353
local mfl = math.floor
local function bmul(x, y)
  local x0, y0 = x % 31596, y % 31596
  local x1, y1 = mfl(x / 31596), mfl(y / 31596)
  return (x1 * y1 * 62863 + (x1 * y0 + x0 * y1) * 31596 + x0 * y0) % mod
end
local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

local function modinv(src)
  return modpow(src, mod - 2)
end
local fact = {1}
local invs = {1}
local invfact = {1}
for i = 2, 100010 do
  fact[i] = bmul(fact[i - 1], i)
  invs[i] = bmul(mfl(mod / i), mod - invs[mod % i])
  invfact[i] = bmul(invfact[i - 1], invs[i])
end

local Heapq = {}
Heapq.create = function(self, lt)
  self.lt = lt
  self.cnt = 0
  self.t = {}
end

Heapq.push = function(self, v)
  local hqlt = self.lt
  local hqt = self.t
  local c = self.cnt + 1
  self.cnt = c
  hqt[c] = v
  while 1 < c do
    local p = brs(c, 1)
    if hqlt(hqt[c], hqt[p]) then
      hqt[c], hqt[p] = hqt[p], hqt[c]
      c = p
    else
      break
    end
  end
end

Heapq.pop = function(self)
  local hqlt = self.lt
  local hqt = self.t
  local ret = hqt[1]
  local c = self.cnt
  hqt[1] = hqt[c]
  c = c - 1
  self.cnt = c
  local p = 1
  while true do
    local d1, d2 = p * 2, p * 2 + 1
    if c < d1 then break
    elseif c < d2 then
      if hqlt(hqt[d1], hqt[p]) then
        hqt[d1], hqt[p] = hqt[p], hqt[d1]
      end
      break
    else
      if hqlt(hqt[d1], hqt[d2]) then
        if hqlt(hqt[d1], hqt[p]) then
          hqt[d1], hqt[p] = hqt[p], hqt[d1]
          p = d1
        else break
        end
      else
        if hqlt(hqt[d2], hqt[p]) then
          hqt[d2], hqt[p] = hqt[p], hqt[d2]
          p = d2
        else break
        end
      end
    end
  end
  return ret
end

Heapq.new = function(lt)
  local obj = {}
  setmetatable(obj, {__index = Heapq})
  obj:create(lt)
  return obj
end

local q = io.read("*n")
for iq = 1, q do
  local n, k = io.read("*n", "*n")
  local a = {}
  local box = {}
  local asum = 0
  for i = 1, n do
    a[i] = io.read("*n")
    box[i] = 1
    asum = asum + a[i]
  end
  local hq = Heapq.new(function(x, y) return a[x] * box[y] > a[y] * box[x] end)
  for i = 1, n do
    hq:push(i)
  end
  local ret = bmul(fact[k], modpow(modinv(asum), k))
  for i = 1, k do
    local idx = hq:pop()
    ret = bmul(ret, bmul(a[idx], invs[box[idx]]))
    box[idx] = box[idx] + 1
    hq:push(idx)
  end
  print(ret)
end
0