結果

問題 No.827 総神童数
ユーザー Yukino DX.Yukino DX.
提出日時 2024-09-08 21:46:32
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 4,045 bytes
コンパイル時間 14,814 ms
コンパイル使用メモリ 388,268 KB
実行使用メモリ 40,096 KB
最終ジャッジ日時 2024-09-08 21:46:52
合計ジャッジ時間 18,989 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 0 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 114 ms
40,096 KB
testcase_10 AC 34 ms
14,080 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 15 ms
7,808 KB
testcase_13 AC 85 ms
33,256 KB
testcase_14 AC 4 ms
6,944 KB
testcase_15 AC 34 ms
14,208 KB
testcase_16 AC 86 ms
28,088 KB
testcase_17 AC 104 ms
33,904 KB
testcase_18 AC 45 ms
17,280 KB
testcase_19 AC 120 ms
25,748 KB
testcase_20 AC 83 ms
19,328 KB
testcase_21 AC 52 ms
14,720 KB
testcase_22 AC 88 ms
20,944 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 104 ms
22,988 KB
testcase_25 AC 67 ms
17,864 KB
testcase_26 AC 102 ms
23,076 KB
testcase_27 AC 61 ms
16,000 KB
testcase_28 AC 60 ms
15,744 KB
testcase_29 AC 47 ms
13,056 KB
testcase_30 AC 13 ms
6,944 KB
testcase_31 AC 34 ms
10,624 KB
testcase_32 AC 33 ms
10,240 KB
testcase_33 AC 116 ms
25,060 KB
testcase_34 AC 103 ms
22,984 KB
testcase_35 AC 34 ms
10,752 KB
testcase_36 AC 53 ms
14,336 KB
testcase_37 AC 71 ms
17,908 KB
testcase_38 AC 109 ms
24,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::{input, marker::Usize1};

fn main() {
    input! {
        n:usize,
        uv:[(Usize1,Usize1);n-1],
    }

    let mut g = vec![vec![]; n];
    for &(u, v) in uv.iter() {
        g[u].push(v);
        g[v].push(u);
    }

    let mut cnts = vec![0; n];
    dfs(0, n, &g, &mut cnts, 0);
    let mut facs = vec![ModInt::<MOD>::new(1); n + 1];
    for i in 0..n {
        facs[i + 1] = facs[i] * ModInt::new(i + 1);
    }

    let mut f = vec![ModInt::new(0); n];
    for i in 0..n {
        f[i] = facs[n - i - 1] * ModInt::new(n - i) * facs[n] / (ModInt::new(i + 1) * facs[n - i]);
    }

    let mut ans = ModInt::new(0);
    for i in 0..n {
        ans += f[cnts[i]];
    }

    println!("{}", ans);
}

fn dfs(crr: usize, prv: usize, g: &Vec<Vec<usize>>, cnts: &mut Vec<usize>, cnt: usize) {
    cnts[crr] = cnt;
    for &nxt in g[crr].iter() {
        if nxt == prv {
            continue;
        }

        dfs(nxt, crr, g, cnts, cnt + 1);
    }
}

const MOD: usize = 1000000007;
use modint::*;
mod modint {
    use std::fmt;
    use std::ops;

    #[derive(Copy, Clone, PartialEq, Eq)]
    pub struct ModInt<const MOD: usize> {
        pub val: usize,
    }

    impl<const MOD: usize> ModInt<MOD> {
        pub fn new(val: usize) -> Self {
            Self { val: val % MOD }
        }

        pub fn pow(mut self, mut e: usize) -> Self {
            let mut res = Self::new(1);
            while 0 < e {
                if e & 1 != 0 {
                    res *= self;
                }

                self *= self;
                e >>= 1;
            }

            res
        }
    }

    impl<const MOD: usize> From<usize> for ModInt<MOD> {
        fn from(value: usize) -> Self {
            Self { val: value % MOD }
        }
    }

    impl<const MOD: usize> fmt::Display for ModInt<MOD> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.val)
        }
    }

    impl<const MOD: usize> fmt::Debug for ModInt<MOD> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.val)
        }
    }

    impl<const MOD: usize> ops::Neg for ModInt<MOD> {
        type Output = Self;

        fn neg(self) -> Self::Output {
            Self {
                val: (MOD - self.val) % MOD,
            }
        }
    }

    impl<const MOD: usize> ops::Add for ModInt<MOD> {
        type Output = Self;

        fn add(self, rhs: Self) -> Self::Output {
            Self {
                val: (self.val + rhs.val) % MOD,
            }
        }
    }

    impl<const MOD: usize> ops::AddAssign for ModInt<MOD> {
        fn add_assign(&mut self, rhs: Self) {
            *self = *self + rhs;
        }
    }

    impl<const MOD: usize> ops::Mul for ModInt<MOD> {
        type Output = Self;

        fn mul(self, rhs: Self) -> Self::Output {
            Self {
                val: self.val * rhs.val % MOD,
            }
        }
    }

    impl<const MOD: usize> ops::MulAssign for ModInt<MOD> {
        fn mul_assign(&mut self, rhs: Self) {
            *self = *self * rhs;
        }
    }

    impl<const MOD: usize> ops::Sub for ModInt<MOD> {
        type Output = Self;

        fn sub(mut self, rhs: Self) -> Self::Output {
            if self.val < rhs.val {
                self.val += MOD;
            }

            Self {
                val: (self.val - rhs.val) % MOD,
            }
        }
    }

    impl<const MOD: usize> ops::SubAssign for ModInt<MOD> {
        fn sub_assign(&mut self, rhs: Self) {
            if self.val < rhs.val {
                self.val += MOD;
            }

            *self = *self - rhs;
        }
    }

    impl<const MOD: usize> ops::Div for ModInt<MOD> {
        type Output = Self;

        fn div(self, rhs: Self) -> Self {
            assert!(rhs.val != 0);
            self * rhs.pow(MOD - 2)
        }
    }

    impl<const MOD: usize> ops::DivAssign for ModInt<MOD> {
        fn div_assign(&mut self, rhs: Self) {
            *self = *self / rhs
        }
    }
}
0