結果

問題 No.827 総神童数
ユーザー phsplsphspls
提出日時 2023-01-19 04:10:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 1,367 bytes
コンパイル時間 1,176 ms
コンパイル使用メモリ 145,768 KB
実行使用メモリ 33,024 KB
最終ジャッジ日時 2023-09-02 21:49:57
合計ジャッジ時間 8,436 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 189 ms
33,024 KB
testcase_10 AC 60 ms
11,664 KB
testcase_11 AC 4 ms
4,372 KB
testcase_12 AC 26 ms
6,688 KB
testcase_13 AC 143 ms
27,728 KB
testcase_14 AC 6 ms
4,372 KB
testcase_15 AC 57 ms
11,888 KB
testcase_16 AC 149 ms
22,468 KB
testcase_17 AC 178 ms
27,176 KB
testcase_18 AC 74 ms
14,268 KB
testcase_19 AC 201 ms
18,684 KB
testcase_20 AC 140 ms
14,264 KB
testcase_21 AC 99 ms
10,872 KB
testcase_22 AC 157 ms
15,336 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 184 ms
16,932 KB
testcase_25 AC 134 ms
13,188 KB
testcase_26 AC 184 ms
16,924 KB
testcase_27 AC 113 ms
11,876 KB
testcase_28 AC 114 ms
11,696 KB
testcase_29 AC 87 ms
9,752 KB
testcase_30 AC 24 ms
4,368 KB
testcase_31 AC 64 ms
8,020 KB
testcase_32 AC 62 ms
7,964 KB
testcase_33 AC 204 ms
18,244 KB
testcase_34 AC 184 ms
16,904 KB
testcase_35 AC 66 ms
8,232 KB
testcase_36 AC 99 ms
10,884 KB
testcase_37 AC 131 ms
13,196 KB
testcase_38 AC 194 ms
17,968 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 u = temp[0]-1;
        let v = temp[1]-1;
        paths[u].push(v);
        paths[v].push(u);
    }

    let mut depths = vec![1usize; n];
    dfs(0, 0, &paths, &mut depths);
    let total = (1..=n).fold(1usize, |x, y| x * y % MOD);
    let powers = (0..=n).map(|i| if i <= 1 { 1 } else { power(i, MOD-2) }).collect::<Vec<_>>();
    let mut result = 0usize;
    for i in 0..n {
        result += total * powers[depths[i]] % MOD;
        result %= MOD;
    }
    println!("{}", result);
}
0