結果

問題 No.827 総神童数
ユーザー 👑 obakyan
提出日時 2020-05-04 21:04:49
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 385 ms / 2,000 ms
コード長 1,022 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 7,068 KB
実行使用メモリ 42,368 KB
最終ジャッジ日時 2024-06-24 23:30:34
合計ジャッジ時間 9,447 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

local mod = 1000000007
local mfl = math.floor

local function bmul(x, y)
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  local x0, y0 = x - x1 * 31623, y - y1 * 31623
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

local function badd(x, y)
  return (x + y) % mod
end
local n = io.read("*n")
local edge = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  len[i] = -1
end
for i = 1, n - 1 do
  local u, v = io.read("*n", "*n")
  edge[u][v], edge[v][u] = true, true
end
len[1] = 0
local tasks = {1}
for i = 1, n do
  local src = tasks[i]
  for dst, _u in pairs(edge[src]) do
    if len[dst] < 0 then
      len[dst] = len[src] + 1
      table.insert(tasks, dst)
    end
  end
end

local fact = {1}
local invs = {1}
local invfact = {1}
for i = 2, n do
  fact[i] = bmul(fact[i - 1], i)
  invs[i] = bmul(mfl(mod / i), mod - invs[mod % i])
  invfact[i] = bmul(invfact[i - 1], invs[i])
end

local ret = 0
for i = 1, n do
  ret = badd(ret, invs[1 + len[i]])
end
ret = bmul(ret, fact[n])
print(ret)
0