結果

問題 No.1092 modular arithmetic
ユーザー 👑 obakyanobakyan
提出日時 2020-06-24 12:40:47
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 1,057 bytes
コンパイル時間 225 ms
コンパイル使用メモリ 5,280 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-16 21:04:56
合計ジャッジ時間 21,650 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 551 ms
4,380 KB
testcase_05 AC 543 ms
4,384 KB
testcase_06 WA -
testcase_07 AC 453 ms
4,380 KB
testcase_08 WA -
testcase_09 AC 456 ms
4,380 KB
testcase_10 WA -
testcase_11 AC 467 ms
4,380 KB
testcase_12 AC 410 ms
4,380 KB
testcase_13 AC 17 ms
4,380 KB
testcase_14 AC 19 ms
4,380 KB
testcase_15 AC 5 ms
4,384 KB
testcase_16 AC 14 ms
4,384 KB
testcase_17 AC 20 ms
4,380 KB
testcase_18 AC 645 ms
4,384 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 TLE -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

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