結果
| 問題 | No.1352 Three Coins | 
| コンテスト | |
| ユーザー | 👑 | 
| 提出日時 | 2022-09-27 23:16:09 | 
| 言語 | Lua (LuaJit 2.1.1734355927) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 6 ms / 2,000 ms | 
| コード長 | 2,560 bytes | 
| コンパイル時間 | 157 ms | 
| コンパイル使用メモリ | 6,820 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-12-22 17:16:39 | 
| 合計ジャッジ時間 | 1,449 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 34 | 
ソースコード
local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local pow2 = {1}
for i = 2, 28 do pow2[i] = pow2[i - 1] * 2 end
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = pow2[i]
    for j = 0, cnt - 1 do
      self.stage[cnt + j] = self.func(self.stage[(cnt + j) * 2], self.stage[(cnt + j) * 2 + 1])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {} -- use 1D Tree (stage[], not stage[][])
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
  end
  self.stagenum = stagenum
  for i = 1, mul * 2 - 1 do self.stage[i] = emptyvalue end
  for i = 1, n do self.stage[mul + i - 1] = i end
  self:updateAll()
end
SegTree.update = function(self, idx, force)
  idx = idx + pow2[self.stagenum] - 1
  for i = self.stagenum - 1, 1, -1 do
    local dst = mfl(idx / 2)
    local rem = dst * 4 + 1 - idx
    self.stage[dst] = self.func(self.stage[idx], self.stage[rem])
    if not force and self.stage[dst] ~= self.stage[idx] then break end
    idx = dst
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end
local n, b, c = io.read("*n", "*n", "*n")
local function getgcd(x, y)
  while 0 < x do
    x, y = y % x, x
  end
  return y
end
if 1 < getgcd(n, getgcd(b, c)) then
  print("INF")
  os.exit()
end
local inf = 1000000007*1000
local len = {}
local asked = {}
for i = 1, n do
  len[i] = inf
  asked[i] = false
end
do
  local z = b % n
  if z == 0 then z = n end
  len[z] = b
  z = c % n
  if z == 0 then z = n end
  if c < len[z] then
    len[z] = c
  end
end
if n < len[n] then len[n] = n end
asked[n + 1] = true
local function mergefunc(x, y)
  if asked[x] then return y
  elseif asked[y] then return x
  else
    return len[x] < len[y] and x or y
  end
end
local st = SegTree.new(n, mergefunc, n + 1)
for i = 1, n do
  local src = st.stage[1]
  if asked[src] then break end
  if inf <= len[src] then break end
  asked[src] = true
  st:update(src, true)
  local dst = (src + b) % n
  if dst == 0 then dst = n end
  if len[src] + b < len[dst] then
    len[dst] = len[src] + b
    st:update(dst)
  end
  dst = (src + c) % n
  if dst == 0 then dst = n end
  if len[src] + c < len[dst] then
    len[dst] = len[src] + c
    st:update(dst)
  end
end
local ret = -1
for i = 1, n do
  -- print(i, len[i])
  ret = ret + math.floor(len[i] / n)
end
print(ret)
            
            
            
        