結果
問題 | No.1554 array_and_me |
ユーザー |
👑 |
提出日時 | 2021-08-12 21:42:03 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
AC
|
実行時間 | 162 ms / 2,000 ms |
コード長 | 2,548 bytes |
コンパイル時間 | 122 ms |
コンパイル使用メモリ | 6,812 KB |
実行使用メモリ | 10,752 KB |
最終ジャッジ日時 | 2024-10-02 02:51:12 |
合計ジャッジ時間 | 6,298 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 41 |
ソースコード
local bls, brs = bit.lshift, bit.rshiftlocal mod = 998244353local mfl = math.floorlocal function bmul(x, y)local x0, y0 = x % 31596, y % 31596local x1, y1 = mfl(x / 31596), mfl(y / 31596)return (x1 * y1 * 62863 + (x1 * y0 + x0 * y1) * 31596 + x0 * y0) % modendlocal function modpow(src, pow)local res = 1while 0 < pow doif pow % 2 == 1 thenres = bmul(res, src)pow = pow - 1endsrc = bmul(src, src)pow = mfl(pow / 2)endreturn resendlocal function modinv(src)return modpow(src, mod - 2)endlocal fact = {1}local invs = {1}local invfact = {1}for i = 2, 100010 dofact[i] = bmul(fact[i - 1], i)invs[i] = bmul(mfl(mod / i), mod - invs[mod % i])invfact[i] = bmul(invfact[i - 1], invs[i])endlocal Heapq = {}Heapq.create = function(self, lt)self.lt = ltself.cnt = 0self.t = {}endHeapq.push = function(self, v)local hqlt = self.ltlocal hqt = self.tlocal c = self.cnt + 1self.cnt = chqt[c] = vwhile 1 < c dolocal p = brs(c, 1)if hqlt(hqt[c], hqt[p]) thenhqt[c], hqt[p] = hqt[p], hqt[c]c = pelsebreakendendendHeapq.pop = function(self)local hqlt = self.ltlocal hqt = self.tlocal ret = hqt[1]local c = self.cnthqt[1] = hqt[c]c = c - 1self.cnt = clocal p = 1while true dolocal d1, d2 = p * 2, p * 2 + 1if c < d1 then breakelseif c < d2 thenif hqlt(hqt[d1], hqt[p]) thenhqt[d1], hqt[p] = hqt[p], hqt[d1]endbreakelseif hqlt(hqt[d1], hqt[d2]) thenif hqlt(hqt[d1], hqt[p]) thenhqt[d1], hqt[p] = hqt[p], hqt[d1]p = d1else breakendelseif hqlt(hqt[d2], hqt[p]) thenhqt[d2], hqt[p] = hqt[p], hqt[d2]p = d2else breakendendendendreturn retendHeapq.new = function(lt)local obj = {}setmetatable(obj, {__index = Heapq})obj:create(lt)return objendlocal q = io.read("*n")for iq = 1, q dolocal n, k = io.read("*n", "*n")local a = {}local box = {}local asum = 0for i = 1, n doa[i] = io.read("*n")box[i] = 1asum = asum + a[i]endlocal hq = Heapq.new(function(x, y) return a[x] * box[y] > a[y] * box[x] end)for i = 1, n dohq:push(i)endlocal ret = bmul(fact[k], modpow(modinv(asum), k))for i = 1, k dolocal idx = hq:pop()ret = bmul(ret, bmul(a[idx], invs[box[idx]]))box[idx] = box[idx] + 1hq:push(idx)endprint(ret)end