結果

問題 No.827 総神童数
ユーザー yuruhiyayuruhiya
提出日時 2020-10-21 21:57:09
言語 Crystal
(1.11.2)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 2,727 bytes
コンパイル時間 20,187 ms
コンパイル使用メモリ 255,544 KB
実行使用メモリ 46,792 KB
最終ジャッジ日時 2023-09-13 12:24:50
合計ジャッジ時間 25,625 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
14,712 KB
testcase_01 AC 19 ms
15,008 KB
testcase_02 AC 20 ms
15,468 KB
testcase_03 AC 21 ms
14,280 KB
testcase_04 AC 18 ms
15,304 KB
testcase_05 AC 20 ms
15,960 KB
testcase_06 AC 18 ms
14,820 KB
testcase_07 AC 19 ms
15,472 KB
testcase_08 AC 18 ms
14,688 KB
testcase_09 AC 182 ms
46,792 KB
testcase_10 AC 66 ms
26,776 KB
testcase_11 AC 21 ms
15,844 KB
testcase_12 AC 38 ms
19,516 KB
testcase_13 AC 141 ms
41,860 KB
testcase_14 AC 23 ms
17,272 KB
testcase_15 AC 60 ms
25,624 KB
testcase_16 AC 145 ms
39,216 KB
testcase_17 AC 168 ms
42,556 KB
testcase_18 AC 76 ms
27,208 KB
testcase_19 AC 187 ms
35,792 KB
testcase_20 AC 132 ms
31,496 KB
testcase_21 AC 94 ms
27,544 KB
testcase_22 AC 146 ms
32,624 KB
testcase_23 AC 20 ms
14,576 KB
testcase_24 AC 164 ms
33,688 KB
testcase_25 AC 125 ms
31,904 KB
testcase_26 AC 166 ms
34,360 KB
testcase_27 AC 106 ms
28,684 KB
testcase_28 AC 105 ms
27,872 KB
testcase_29 AC 85 ms
25,316 KB
testcase_30 AC 37 ms
17,888 KB
testcase_31 AC 68 ms
22,560 KB
testcase_32 AC 66 ms
22,392 KB
testcase_33 AC 183 ms
35,936 KB
testcase_34 AC 162 ms
34,584 KB
testcase_35 AC 72 ms
22,300 KB
testcase_36 AC 99 ms
27,404 KB
testcase_37 AC 122 ms
29,060 KB
testcase_38 AC 179 ms
34,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

lib C
  fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64
end

class String
  def to_i64
    C.strtoll(self, nil, 10)
  end
end

struct ModInt
  @@MOD = 1_000_000_007i64

  def self.mod
    @@MOD
  end

  def self.zero
    ModInt.new(0)
  end

  def initialize(n)
    @n = n.to_i64 % @@MOD
  end

  getter n : Int64

  def + : self
    self
  end

  def - : self
    ModInt.new(n != 0 ? @@MOD - @n : 0)
  end

  def +(m)
    ModInt.new(@n + m.to_i64 % @@MOD)
  end

  def -(m)
    ModInt.new(@n - m.to_i64 % @@MOD)
  end

  def *(m)
    ModInt.new(@n * m.to_i64 % @@MOD)
  end

  def /(m)
    raise DivisionByZeroError.new if m == 0
    a, b, u, v = m.to_i64, @@MOD, 1i64, 0i64
    while b != 0
      t = a // b
      a -= t * b
      a, b = b, a
      u -= t * v
      u, v = v, u
    end
    ModInt.new(@n * u)
  end

  def //(m)
    self / m
  end

  def **(m : Int)
    t, res = self, ModInt.new(1)
    while m > 0
      res *= t if m.odd?
      t *= t
      m >>= 1
    end
    res
  end

  def ==(m)
    @n == m.to_i64
  end

  def !=(m)
    @n != m.to_i64
  end

  def succ
    self + 1
  end

  def pred
    self - 1
  end

  def to_i64 : Int64
    @n
  end

  delegate to_s, to: @n
  delegate inspect, to: @n
end

struct Int
  def to_mint : ModInt
    ModInt.new(self)
  end
end

class Comb(T)
  def initialize(@Max = 400009)
    @fac = Array(T).new(@Max, T.zero)
    @finv = Array(T).new(@Max, T.zero)
    @inv = Array(T).new(@Max, T.zero)
    @fac[0] = @fac[1] = T.new(1)
    @finv[0] = @finv[1] = T.new(1)
    @inv[1] = T.new(1)
    (2...@Max).each do |i|
      @fac[i] = @fac[i - 1] * i
      @inv[i] = -@inv[T.mod % i] * (T.mod / i)
      @finv[i] = @finv[i - 1] * @inv[i]
    end
  end

  def permutation(n, r)
    (n < r || n < 0 || r < 0) ? T.zero : @fac[n] * @finv[n - r]
  end

  def combination(n, r)
    (n < r || n < 0 || r < 0) ? T.zero : @fac[n] * @finv[r] * @finv[n - r]
  end

  def homogeneous_product(n, r)
    (n < 0 || r < 0) ? T.zero : r == T.zero ? T.new(1) : c(n + r - 1, r)
  end

  def factorial(n)
    @fac[n]
  end
end

class Solve
  @n : Int32
  @g : Array(Array(Int32))
  @parent_size : Array(Int32)

  def initialize
    @n = read_line.to_i
    @g = Array.new(@n) { [] of Int32 }
    (@n - 1).times do
      u, v = read_line.split.map(&.to_i.pred)
      @g[u] << v
      @g[v] << u
    end
    @parent_size = Array.new(@n, 1)
  end

  def dfs(v, prev, size)
    @parent_size[v] = size
    @g[v].each do |u|
      if u != prev
        dfs(u, v, size + 1)
      end
    end
  end

  def solve
    dfs(0, -1, 1)
    comb = Comb(ModInt).new
    puts @parent_size.sum { |s|
      comb.combination(@n, s) * comb.factorial(s - 1) * comb.factorial(@n - s)
    }
  end
end

Solve.new.solve
0