結果

問題 No.827 総神童数
ユーザー tentententen
提出日時 2023-03-15 11:27:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,644 ms / 2,000 ms
コード長 2,817 bytes
コンパイル時間 3,084 ms
コンパイル使用メモリ 95,288 KB
実行使用メモリ 131,144 KB
最終ジャッジ日時 2023-10-18 12:17:57
合計ジャッジ時間 26,267 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
53,524 KB
testcase_01 AC 53 ms
53,532 KB
testcase_02 AC 56 ms
53,524 KB
testcase_03 AC 54 ms
53,532 KB
testcase_04 AC 53 ms
53,532 KB
testcase_05 AC 55 ms
51,488 KB
testcase_06 AC 53 ms
53,528 KB
testcase_07 AC 54 ms
53,524 KB
testcase_08 AC 53 ms
53,540 KB
testcase_09 AC 1,644 ms
129,552 KB
testcase_10 AC 506 ms
83,872 KB
testcase_11 AC 135 ms
56,120 KB
testcase_12 AC 330 ms
72,096 KB
testcase_13 AC 953 ms
131,144 KB
testcase_14 AC 169 ms
59,364 KB
testcase_15 AC 498 ms
84,592 KB
testcase_16 AC 865 ms
114,016 KB
testcase_17 AC 1,010 ms
125,516 KB
testcase_18 AC 549 ms
94,864 KB
testcase_19 AC 866 ms
94,144 KB
testcase_20 AC 776 ms
82,440 KB
testcase_21 AC 561 ms
76,352 KB
testcase_22 AC 784 ms
88,400 KB
testcase_23 AC 92 ms
54,584 KB
testcase_24 AC 808 ms
91,500 KB
testcase_25 AC 662 ms
81,188 KB
testcase_26 AC 812 ms
91,428 KB
testcase_27 AC 593 ms
78,668 KB
testcase_28 AC 600 ms
79,000 KB
testcase_29 AC 517 ms
72,752 KB
testcase_30 AC 236 ms
62,888 KB
testcase_31 AC 420 ms
68,888 KB
testcase_32 AC 407 ms
68,608 KB
testcase_33 AC 834 ms
92,016 KB
testcase_34 AC 828 ms
91,764 KB
testcase_35 AC 419 ms
68,740 KB
testcase_36 AC 542 ms
76,280 KB
testcase_37 AC 674 ms
81,096 KB
testcase_38 AC 868 ms
91,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static HashMap<Integer, Integer> counts = new HashMap<>();
    static final int MOD = 1000000007;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        search(0, 0, 0);
        long ans = 0;
        for (int x : counts.keySet()) {
            ans += pow(x + 1, MOD - 2) * counts.get(x) % MOD;
            ans %= MOD;
        }
        ans *= fact(n);
        ans %= MOD;
        System.out.println(ans);
    }
    
    static void search(int idx, int p, int d) {
        counts.put(d, counts.getOrDefault(d, 0) + 1);
        for (int x : graph.get(idx)) {
            if (x != p) {
                search(x, idx, d + 1);
            }
        }
    }
    
    static long pow(long x, int p) {
        if (p == 0) {
            return 1;
        } else if (p % 2 == 0) {
            return pow(x * x % MOD, p / 2);
        } else {
            return pow(x, p - 1) * x % MOD;
        }
    }
    
    static long fact(int x) {
        long ans = 1;
        for (int i = 1; i <= x; i++) {
            ans *= i;
            ans %= MOD;
        }
        return ans;
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0