結果

問題 No.758 VVVVV
ユーザー tottoripapertottoripaper
提出日時 2018-12-25 15:55:48
言語 OCaml
(5.1.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 2,040 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 21,584 KB
実行使用メモリ 14,592 KB
最終ジャッジ日時 2024-04-17 08:45:02
合計ジャッジ時間 2,685 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
6,784 KB
testcase_01 AC 13 ms
6,912 KB
testcase_02 AC 13 ms
6,784 KB
testcase_03 AC 83 ms
14,592 KB
testcase_04 AC 43 ms
11,392 KB
testcase_05 AC 34 ms
10,496 KB
testcase_06 AC 32 ms
10,368 KB
testcase_07 AC 87 ms
14,208 KB
testcase_08 AC 47 ms
11,520 KB
testcase_09 AC 25 ms
9,856 KB
testcase_10 AC 36 ms
10,624 KB
testcase_11 AC 87 ms
14,464 KB
testcase_12 AC 30 ms
10,240 KB
testcase_13 AC 34 ms
10,624 KB
testcase_14 AC 25 ms
9,856 KB
testcase_15 AC 21 ms
9,472 KB
testcase_16 AC 22 ms
9,856 KB
testcase_17 AC 75 ms
13,696 KB
testcase_18 AC 43 ms
11,392 KB
testcase_19 AC 87 ms
14,592 KB
testcase_20 AC 31 ms
10,368 KB
testcase_21 AC 33 ms
10,624 KB
testcase_22 AC 91 ms
14,592 KB
testcase_23 AC 13 ms
6,912 KB
testcase_24 AC 13 ms
6,784 KB
testcase_25 AC 13 ms
6,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

let rec expt ( * ) e a n =  
  if n = 0 then
    e
  else if n land 1 = 1 then
    a * expt ( * ) e (a * a) (n / 2)
  else
    expt ( * ) e (a * a) (n / 2)

let mod_expt a n mo = expt (fun x y -> x * y mod mo) 1 a n
let mod_inv a p = mod_expt a (p - 2) p

module Combination : sig
  type t
  val init : n:int -> modulo:int -> t
  val fact : int -> t -> int
  val inv_fact : int -> t -> int
  val nPk : int -> int -> t -> int
  val nCk : int -> int -> t -> int
  val nHk : int -> int -> t -> int
end = struct
  type t = int * int array * int array
  let init ~n ~modulo =
    let f = Array.make (n + 1) 0 in
    let () =
      f.(0) <- 1;
      for i = 1 to n do
        f.(i) <- f.(i - 1) * i mod modulo
      done in
    let inv_f = Array.make (n + 1) 0 in
    let () =
      inv_f.(n) <- mod_inv f.(n) modulo;
      for i = n - 1 downto 0 do
        inv_f.(i) <- inv_f.(i + 1) * (i + 1) mod modulo
      done in
    (modulo, f, inv_f)
  let fact n (_, f, _) = f.(n)
  let inv_fact n (_, _, inv_f) = inv_f.(n)
  let nPk n k ((mo, _, _) as c) =
    if n < k then
      0
    else
      fact n c * inv_fact (n - k) c mod mo
  let nCk n k ((mo, _, _) as c) =
    if n < k then
      0
    else
      fact n c * inv_fact k c mod mo * inv_fact (n - k) c mod mo
  let nHk n k c = if n = 0 && k = 0 then 1 else nCk (n + k - 1) k c                               
end

let id x = x

let n = Scanf.scanf "%d" id
let g = Array.make (n + 1) []
let () =
  for i = 1 to n - 1 do
    let (a, b) = Scanf.scanf " %d %d" (fun a b -> (a, b)) in
    g.(a) <- b :: g.(a);
    g.(b) <- a :: g.(b)
  done

let rec dfs v p =
  match g.(v) with
    [w] when w = p -> 1 (* v は葉 *)
  | _ -> List.fold_left (fun s w -> if w <> p then s + dfs w v else s) 0 g.(v)

let m = dfs 1 (-1)

let mo = 1_000_000_007
let c = Combination.init ~n:200100 ~modulo:mo

let cnt = if n >= 3 then
            Combination.nCk (n - 1) (m - 1) c * Combination.nCk (n - 2) (m - 1) c mod mo * mod_inv m mo mod mo
          else
            1
let () =
  print_int cnt;
  print_newline ()
0