結果

問題 No.1507 Road Blocked
ユーザー 👑 obakyanobakyan
提出日時 2021-05-16 20:05:35
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 333 ms / 2,000 ms
コード長 1,945 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 21,888 KB
最終ジャッジ日時 2024-04-15 11:01:03
合計ジャッジ時間 11,639 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 210 ms
21,120 KB
testcase_04 AC 322 ms
21,632 KB
testcase_05 AC 324 ms
21,632 KB
testcase_06 AC 324 ms
21,632 KB
testcase_07 AC 316 ms
21,632 KB
testcase_08 AC 321 ms
21,760 KB
testcase_09 AC 322 ms
21,632 KB
testcase_10 AC 319 ms
21,760 KB
testcase_11 AC 323 ms
21,760 KB
testcase_12 AC 315 ms
21,760 KB
testcase_13 AC 324 ms
21,760 KB
testcase_14 AC 326 ms
21,888 KB
testcase_15 AC 317 ms
21,632 KB
testcase_16 AC 319 ms
21,760 KB
testcase_17 AC 318 ms
21,760 KB
testcase_18 AC 312 ms
21,632 KB
testcase_19 AC 315 ms
21,632 KB
testcase_20 AC 309 ms
21,888 KB
testcase_21 AC 310 ms
21,760 KB
testcase_22 AC 314 ms
21,632 KB
testcase_23 AC 318 ms
21,760 KB
testcase_24 AC 320 ms
21,760 KB
testcase_25 AC 320 ms
21,632 KB
testcase_26 AC 325 ms
21,632 KB
testcase_27 AC 319 ms
21,760 KB
testcase_28 AC 327 ms
21,760 KB
testcase_29 AC 324 ms
21,760 KB
testcase_30 AC 333 ms
21,760 KB
testcase_31 AC 322 ms
21,632 KB
testcase_32 AC 331 ms
21,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mod = 998244353
local mfl = math.floor
local function bmul(x, y)
  local x0, y0 = x % 31596, y % 31596
  local x1, y1 = mfl(x / 31596), mfl(y / 31596)
  return (x1 * y1 * 62863 + (x1 * y0 + x0 * y1) * 31596 + x0 * y0) % mod
end

local function badd(x, y)
  return (x + y) % mod
end
local function bsub(x, y)
  return (x + mod - y) % mod
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 n = io.read("*n")
local edge = {}
local parent = {}
local childask = {}
local childcnt = {}
for i = 1, n do
  edge[i] = {}
  parent[i] = 0
  childask[i] = 0
  childcnt[i] = 0
end

local ea, eb, ec = {}, {}, {}
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  ea[i] = a
  eb[i] = b
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end

local tasks = {1}
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if parent[src] ~= dst then
      parent[dst] = src
      table.insert(tasks, dst)
      childask[src] = childask[src] + 1
    end
  end
end

tasks = {}
for i = 2, n do
  if childask[i] == 0 then
    table.insert(tasks, i)
  end
end
local ret = 0
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local child = edge[src][j]
    if child ~= parent[src] then
      local a = childcnt[child] + 1
      local b = n - a
      local numer = badd(bmul(a, a - 1), bmul(b, b - 1))
      local denom = badd(numer, bmul(2 * a, b))
      ret = badd(ret, bmul(numer, modinv(denom)))
    end
  end
  local p = parent[src]
  if 0 < p then
    childask[p] = childask[p] - 1
    childcnt[p] = childcnt[p] + 1 + childcnt[src]
    if childask[p] == 0 then
      table.insert(tasks, p)
    end
  end
end
ret = bmul(ret, modinv(n - 1))
print(ret)
0