結果

問題 No.1092 modular arithmetic
ユーザー 👑 obakyan
提出日時 2020-06-24 12:40:47
言語 Lua
(LuaJit 2.1.1734355927)
結果
WA  
実行時間 -
コード長 1,057 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 6,688 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-03 20:08:45
合計ジャッジ時間 23,021 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13 WA * 17 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

local mod = 1000000007
local mfl = math.floor

local function bmul(x, y)
  local res = 0
  while 0 < y do
    if y % 2 == 1 then
      res = res + x
      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