結果

問題 No.1667 Forest
ユーザー 👑 obakyanobakyan
提出日時 2022-06-25 14:08:21
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,493 ms / 3,000 ms
コード長 1,866 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-26 20:22:54
合計ジャッジ時間 10,579 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,492 ms
5,248 KB
testcase_01 AC 1,493 ms
5,248 KB
testcase_02 AC 1,466 ms
5,376 KB
testcase_03 AC 16 ms
5,376 KB
testcase_04 AC 1,438 ms
5,376 KB
testcase_05 AC 857 ms
5,376 KB
testcase_06 AC 526 ms
5,376 KB
testcase_07 AC 325 ms
5,376 KB
testcase_08 AC 157 ms
5,376 KB
testcase_09 AC 92 ms
5,376 KB
testcase_10 AC 47 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n, mod = io.read("*n", "*n")
local mfl = math.floor

local function bmulslow(x, y)
  local x1, y1 = x % 10000, y % 10000
  local x2, y2 = mfl(x / 10000) % 10000, mfl(y / 10000) % 10000
  local x3, y3 = mfl(x / 100000000), mfl(y / 100000000)
  local ret = (x1 * y1 + (x1 * y2 + x2 * y1) * 10000) % mod
  ret = (ret + (x1 * y3 + x2 * y2 + x3 * y1) * 10000 % mod * 10000 % mod) % mod
  ret = (ret + (x2 * y3 + x3 * y2) * 10000 % mod * 10000 % mod * 10000) % mod
  ret = (ret +  x3 * y3 * 10000 % mod * 10000 % mod * 10000 % mod * 10000) % mod
  return ret
end
local bmul = bmulslow

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 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 function getComb(n, k)
  if k == 0 or k == n then return 1 end
  return bmul(fact[n], bmul(invfact[k], invfact[n - k]))
end

local tree = {1, 1}
for i = 3, n do
  tree[i] = modpow(i, i - 2)
end
local t = {{1}}

-- i: dst node count
for i = 2, n do
  t[i] = {}
  -- j: dst edge count - 1
  for j = 1, i do
    -- k: src node count
    local v = 0
    for k = 1, i - 1 do
      local sz = i - k
      if 0 < j - sz + 1 and j - sz + 1 <= k then
        v = badd(v, bmul(t[k][j - sz + 1] , bmul(getComb(n - k - 1, sz - 1), tree[sz])))
      end
    end
    t[i][j] = v
    t[i][i] = bmul(getComb(n - 1, i - 1), tree[i])
  end
end
for i = 1, n do
  print(t[n][i])
end
0