結果
問題 | No.1330 Multiply or Divide |
ユーザー |
👑 |
提出日時 | 2022-05-05 19:32:59 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
AC
|
実行時間 | 1,209 ms / 2,000 ms |
コード長 | 2,754 bytes |
コンパイル時間 | 134 ms |
コンパイル使用メモリ | 5,120 KB |
実行使用メモリ | 31,660 KB |
最終ジャッジ日時 | 2024-07-04 21:12:44 |
合計ジャッジ時間 | 5,847 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 46 |
ソースコード
local mmi, mma = math.min, math.maxlocal mfl, mce = math.floor, math.ceillocal bls, brs = bit.lshift, bit.rshiftlocal Heapq = {}Heapq.create = function(self, lt)self.lt = ltself.cnt = 0self.t = {}endHeapq.empty = function(self)return self.cnt == 0endHeapq.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 n, m, p = io.read("*n", "*n", "*n")local a = {}local amax = 0for i = 1, n doa[i] = io.read("*n")amax = mma(amax, a[i])endif m < amax then print(1) os.exit() endlocal c = {}for i = 1, n dolocal ci = 1local ai = a[i]while ai % p == 0 doci = ci + 1ai = mfl(ai / p)endif 1 < ai thenif c[ai] thenc[ai] = mmi(c[ai], ci)elsec[ai] = ciendendendif not next(c) then print(-1) os.exit() endlocal cm = {}for k, v in pairs(c) dotable.insert(cm, {k, v})endtable.sort(cm, function(a, b) return a[1] < b[1] end)local cn = #cmlocal val, score, valmap = {}, {}, {}local lim = mfl(m / amax) + 1valmap[m] = 1val[1] = 1score[1] = 1local hq = Heapq.new(function(a, b) return val[a] < val[b] end)hq:push(1)local ret = 1000000007while not hq:empty() dolocal idx = hq:pop()local v, s = val[idx], score[idx]for i = 1, cn dolocal mul = cm[i][1]local cost = cm[i][2]if lim <= v * mul thenret = mmi(ret, s + cost)elselocal dst = v * mullocal dstidx = valmap[dst]if dstidx thenscore[dstidx] = mmi(score[dstidx], s + cost)elsetable.insert(val, dst)table.insert(score, s + cost)valmap[dst] = #valhq:push(#val)endendendendprint(ret)-- print(os.clock())