結果

問題 No.2382 Amidakuji M
ユーザー 👑 obakyanobakyan
提出日時 2023-07-16 00:49:51
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 1,179 bytes
コンパイル時間 435 ms
コンパイル使用メモリ 5,336 KB
実行使用メモリ 4,412 KB
最終ジャッジ日時 2023-10-17 10:50:57
合計ジャッジ時間 2,995 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 36 ms
4,348 KB
testcase_04 AC 65 ms
4,412 KB
testcase_05 AC 11 ms
4,348 KB
testcase_06 AC 39 ms
4,348 KB
testcase_07 AC 24 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 54 ms
4,348 KB
testcase_10 AC 81 ms
4,412 KB
testcase_11 AC 30 ms
4,348 KB
testcase_12 AC 55 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 88 ms
4,412 KB
testcase_20 AC 89 ms
4,412 KB
testcase_21 AC 88 ms
4,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local band = bit.band
local FenwickTree = {}
FenwickTree.create = function(self, n)
  self.n = n
  self.v = {}
  for i = 1, n do self.v[i] = 0 end
end
FenwickTree.add = function(self, pos, val)
  while pos <= self.n do
    self.v[pos] = self.v[pos] + val
    pos = pos + band(pos, -pos)
  end
end
FenwickTree.sum = function(self, r)
  local ret = 0
  while 0 < r do
    ret = ret + self.v[r]
    r = r - band(r, -r)
  end
  return ret
end
FenwickTree.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = FenwickTree})
  obj:create(n)
  return obj
end

local ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
long long atoll(const char*);
]]

local function lltonumber(str)
  return C.atoll(str)
end

local n, m = io.read():match("(%d+) (%d+)")
n = tonumber(n)
m = lltonumber(m)
local v = 0
local fw = FenwickTree.new(n)
for i = 1, n do
  local p = io.read("*n")
  v = v + i - 1 - fw:sum(p)
  fw:add(p, 1)
end
local a = 0LL
while a < 1LL * v do
  a = a + m
end
if (a - 1LL * v) % 2LL == 0LL then
  local ans = tostring(a):gsub("LL", "")
  print(ans)
else
  if v % 2 == 1 then
    print(-1)
  else
    local ans = tostring(a + m):gsub("LL", "")
    print(ans)
  end
end
0