結果

問題 No.827 総神童数
ユーザー phsplsphspls
提出日時 2022-12-22 01:53:47
言語 Rust
(1.77.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 1,403 bytes
コンパイル時間 13,504 ms
コンパイル使用メモリ 377,508 KB
実行使用メモリ 33,024 KB
最終ジャッジ日時 2024-04-29 03:37:15
合計ジャッジ時間 18,147 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 0 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 186 ms
33,024 KB
testcase_10 AC 50 ms
11,648 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 24 ms
6,784 KB
testcase_13 AC 145 ms
27,520 KB
testcase_14 AC 6 ms
5,376 KB
testcase_15 AC 54 ms
12,160 KB
testcase_16 AC 120 ms
21,760 KB
testcase_17 AC 153 ms
26,624 KB
testcase_18 AC 66 ms
14,336 KB
testcase_19 AC 136 ms
17,152 KB
testcase_20 AC 91 ms
13,184 KB
testcase_21 AC 56 ms
9,984 KB
testcase_22 AC 109 ms
14,208 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 125 ms
15,488 KB
testcase_25 AC 83 ms
12,288 KB
testcase_26 AC 117 ms
15,616 KB
testcase_27 AC 75 ms
11,008 KB
testcase_28 AC 74 ms
10,880 KB
testcase_29 AC 51 ms
9,088 KB
testcase_30 AC 13 ms
5,376 KB
testcase_31 AC 36 ms
7,552 KB
testcase_32 AC 34 ms
7,296 KB
testcase_33 AC 135 ms
16,768 KB
testcase_34 AC 123 ms
15,616 KB
testcase_35 AC 35 ms
7,552 KB
testcase_36 AC 57 ms
9,856 KB
testcase_37 AC 84 ms
12,288 KB
testcase_38 AC 136 ms
16,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fn power(base: usize, times: usize) -> usize {
    if times == 0 { return 1usize; }
    if times == 1 { return base; }
    let temp = power(base, times/2);
    temp * temp % MOD * power(base, times%2) % MOD
}

fn dfs(cur: usize, prev: usize, paths: &Vec<Vec<usize>>, depths: &mut Vec<usize>) {
    for &v in paths[cur].iter() {
        if v == prev { continue; }
        depths[v] = depths[cur] + 1;
        dfs(v, cur, paths, depths);
    }
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut paths = vec![vec![]; n];
    for _ in 0..n-1 {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let a = temp[0]-1;
        let b = temp[1]-1;
        paths[a].push(b);
        paths[b].push(a);
    }

    let mut depths = vec![0usize; n];
    dfs(0, 0, &paths, &mut depths);
    let total_pattern = (1..=n).fold(1usize, |x, y| x * y % MOD);
    let limit = depths.iter().max().unwrap()+2;
    let mut mods = vec![0usize; limit+1];
    for i in 1..=limit {
        mods[i] = power(i, MOD-2);
    }
    let result = total_pattern * ((0..n).map(|i| mods[depths[i]+1]).sum::<usize>() % MOD) % MOD;
    println!("{}", result);
}
0