結果

問題 No.827 総神童数
ユーザー cotton_fn_cotton_fn_
提出日時 2020-02-09 01:01:12
言語 Rust
(1.77.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 3,668 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 190,324 KB
実行使用メモリ 33,136 KB
最終ジャッジ日時 2024-04-08 11:49:21
合計ジャッジ時間 7,262 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,548 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 AC 1 ms
6,548 KB
testcase_03 AC 1 ms
6,548 KB
testcase_04 AC 1 ms
6,548 KB
testcase_05 AC 1 ms
6,548 KB
testcase_06 AC 1 ms
6,548 KB
testcase_07 AC 1 ms
6,548 KB
testcase_08 AC 1 ms
6,548 KB
testcase_09 AC 150 ms
33,136 KB
testcase_10 AC 44 ms
11,776 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 20 ms
6,784 KB
testcase_13 AC 113 ms
27,616 KB
testcase_14 AC 5 ms
6,548 KB
testcase_15 AC 43 ms
12,032 KB
testcase_16 AC 117 ms
22,412 KB
testcase_17 AC 138 ms
27,176 KB
testcase_18 AC 58 ms
14,464 KB
testcase_19 AC 176 ms
18,744 KB
testcase_20 AC 120 ms
14,220 KB
testcase_21 AC 83 ms
11,008 KB
testcase_22 AC 131 ms
15,424 KB
testcase_23 AC 2 ms
6,548 KB
testcase_24 AC 151 ms
16,828 KB
testcase_25 AC 104 ms
13,312 KB
testcase_26 AC 147 ms
16,852 KB
testcase_27 AC 89 ms
11,904 KB
testcase_28 AC 91 ms
11,776 KB
testcase_29 AC 68 ms
9,856 KB
testcase_30 AC 17 ms
6,548 KB
testcase_31 AC 49 ms
8,064 KB
testcase_32 AC 48 ms
7,808 KB
testcase_33 AC 170 ms
18,232 KB
testcase_34 AC 149 ms
16,824 KB
testcase_35 AC 52 ms
8,192 KB
testcase_36 AC 77 ms
10,752 KB
testcase_37 AC 104 ms
13,308 KB
testcase_38 AC 160 ms
17,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused)]

use std::io::{self, BufRead, Write};

pub struct Input<R> {
    src: R,
    buf: Vec<u8>,
    pos: usize,
}

impl<R: BufRead> Input<R> {
    pub fn new(src: R) -> Self {
        Self {
            src,
            buf: Vec::new(),
            pos: 0,
        }
    }

    pub fn input_bytes(&mut self) -> &[u8] {
        loop {
            self.advance_until(|b| b.is_ascii_whitespace());
            if self.pos == self.buf.len() {
                self.buf.clear();
                self.src
                    .read_until(b'\n', &mut self.buf)
                    .expect("input error");
                self.pos = 0;
            } else {
                break;
            }
        }
        let l = self.pos;
        self.advance_until(|b| !b.is_ascii_whitespace());
        &self.buf[l..self.pos]
    }

    fn advance_until(&mut self, f: impl Fn(u8) -> bool) {
        while let Some(b) = self.buf.get(self.pos) {
            if !f(*b) {
                break;
            }
            self.pos += 1;
        }
    }

    pub fn input<T: std::str::FromStr>(&mut self) -> T
    where
        <T as std::str::FromStr>::Err: std::fmt::Debug,
    {
        String::from_utf8_lossy(self.input_bytes())
            .parse()
            .expect("parse error")
    }
}

pub fn mod_pow(a: i64, b: i64, m: i64) -> i64 {
    let mut b = b;
    let mut x = a;
    let mut y = 1;
    while b > 0 {
        if b & 1 == 1 {
            y = y * x % m;
        }
        x = x * x % m;
        b >>= 1;
    }
    y
}

pub fn mod_inv(x: i64, m: i64) -> i64 {
    mod_pow(x, m - 2, m)
}

struct Combination {
    m: i64,
    fact: Vec<i64>,
}

impl Combination {
    pub fn new(size: usize, m: i64) -> Self {
        let mut comb = Self {
            m,
            fact: Vec::new(),
        };
        comb.resize(size);
        comb
    }

    pub fn resize(&mut self, size: usize) {
        let old_len = self.fact.len();
        self.fact.resize(size + 1, 0);
        for i in old_len..size + 1 {
            self.fact[i] = if i == 0 {
                1
            } else {
                i as i64 * self.fact[i - 1] % self.m
            };
        }
    }

    pub fn fact(&self, x: i64) -> i64 {
        self.fact[x as usize]
    }

    pub fn perm(&self, a: i64, b: i64) -> i64 {
        if a >= b {
            self.fact(a) * mod_inv(self.fact(a - b), self.m) % self.m
        } else {
            0
        }
    }

    pub fn comb(&self, a: i64, b: i64) -> i64 {
        if a >= b {
            self.fact(a) * mod_inv(self.fact(b) * self.fact(a - b) % self.m, self.m) % self.m
        } else {
            0
        }
    }
}

fn dfs(u: usize, p: usize, g: &Vec<Vec<usize>>, depth: &mut [i64]) {
    for &v in &g[u] {
        if v == p {
            continue;
        }
        depth[v] = depth[u] + 1;
        dfs(v, u, g, depth);
    }
}

fn main() {
    let stdin = io::stdin();
    let mut input = Input::new(stdin.lock());
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    let stderr = io::stderr();
    let mut err = io::BufWriter::new(stderr.lock());

    const MOD: i64 = 1e9 as i64 + 7;

    let n = input.input();
    let mut g = vec![Vec::new(); n + 1];
    for i in 0..n - 1 {
        let u: usize = input.input();
        let v: usize = input.input();
        g[u].push(v);
        g[v].push(u);
    }
    let mut depth = vec![1; n + 1];

    dfs(1, 0, &g, &mut depth);
    let mut exp = 0;
    for &d in depth.iter().skip(1) {
        exp += mod_inv(d, MOD);
    }
    let comb = Combination::new(n, MOD);
    let ans = exp % MOD * comb.fact(n as i64) % MOD;
    writeln!(&mut out, "{}", ans);
}
0