結果

問題 No.1092 modular arithmetic
ユーザー 👑 obakyan
提出日時 2020-06-24 12:46:36
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 1,896 ms / 2,000 ms
コード長 1,092 bytes
コンパイル時間 88 ms
コンパイル使用メモリ 5,504 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-03 20:07:48
合計ジャッジ時間 20,780 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

local mod = false
local mfl = math.floor

local function bmul(x, y)
  if x < y then x, y = y, x end
  local res = 0
  while 0 < y do
    if y % 2 == 1 then
      res = (res + x) % mod
      y = y - 1
    end
    x = (x + x) % mod
    y = mfl(y / 2)
  end
  return res
end

local function badd(x, y)
  return (x + y) % mod
end

local function bsub(x, y)
  return x < y and x - y + mod or x - y
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 p, n = io.read("*n", "*n")
mod = p
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
io.read()
local s = io.read()
local ans = a[1]
for i = 1, n - 1 do
  local ss = s:sub(i, i)
  if ss == "+" then
    ans = badd(ans, a[i + 1])
  elseif ss == "-" then
    ans = bsub(ans, a[i + 1])
  elseif ss == "*" then
    ans = bmul(ans, a[i + 1])
  else
    ans = bmul(ans, modinv(a[i + 1]))
  end
end
print(ans)
0