結果

問題 No.827 総神童数
ユーザー ks2mks2m
提出日時 2019-05-04 00:45:06
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,643 bytes
コンパイル時間 2,136 ms
コンパイル使用メモリ 75,692 KB
実行使用メモリ 115,268 KB
最終ジャッジ日時 2023-08-30 08:09:54
合計ジャッジ時間 40,539 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,668 KB
testcase_01 AC 119 ms
55,672 KB
testcase_02 AC 126 ms
55,788 KB
testcase_03 AC 121 ms
55,720 KB
testcase_04 AC 120 ms
55,600 KB
testcase_05 AC 122 ms
55,740 KB
testcase_06 AC 122 ms
55,588 KB
testcase_07 RE -
testcase_08 AC 120 ms
55,988 KB
testcase_09 AC 1,556 ms
115,268 KB
testcase_10 AC 822 ms
79,068 KB
testcase_11 AC 237 ms
58,936 KB
testcase_12 AC 524 ms
64,536 KB
testcase_13 AC 1,306 ms
98,656 KB
testcase_14 AC 300 ms
60,572 KB
testcase_15 AC 778 ms
79,404 KB
testcase_16 AC 1,278 ms
98,600 KB
testcase_17 AC 1,487 ms
113,696 KB
testcase_18 AC 902 ms
80,928 KB
testcase_19 AC 1,713 ms
113,192 KB
testcase_20 AC 1,398 ms
97,940 KB
testcase_21 AC 1,185 ms
90,508 KB
testcase_22 AC 1,472 ms
100,144 KB
testcase_23 AC 203 ms
58,624 KB
testcase_24 AC 1,613 ms
102,108 KB
testcase_25 AC 1,355 ms
95,376 KB
testcase_26 AC 1,551 ms
102,452 KB
testcase_27 AC 1,283 ms
94,108 KB
testcase_28 AC 1,304 ms
93,244 KB
testcase_29 AC 1,094 ms
82,396 KB
testcase_30 AC 549 ms
64,908 KB
testcase_31 AC 977 ms
81,568 KB
testcase_32 AC 916 ms
79,168 KB
testcase_33 AC 1,726 ms
103,884 KB
testcase_34 AC 1,562 ms
101,992 KB
testcase_35 AC 950 ms
81,160 KB
testcase_36 AC 1,136 ms
90,308 KB
testcase_37 AC 1,298 ms
94,796 KB
testcase_38 AC 1,674 ms
102,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
		for (int i = 0; i < n - 1; i++) {
			int u = sc.nextInt();
			int v = sc.nextInt();

			List<Integer> list1 = map.get(u);
			if (list1 == null) {
				list1 = new ArrayList<Integer>();
				map.put(u, list1);
			}
			list1.add(v);

			List<Integer> list2 = map.get(v);
			if (list2 == null) {
				list2 = new ArrayList<Integer>();
				map.put(v, list2);
			}
			list2.add(u);
		}
		sc.close();

		int[] dep = new int[n + 1];
		Queue<Integer> que = new ArrayDeque<Integer>();
		que.add(1);
		dep[1] = 1;
		while (!que.isEmpty()) {
			Integer cur = que.poll();
			List<Integer> list = map.get(cur);
			for (Integer next : list) {
				if (dep[next] == 0) {
					que.add(next);
					dep[next] = dep[cur] + 1;
				}
			}
		}

		int mod = 1000000007;
		long p = 1;
		for (int i = 1; i <= n; i++) {
			p *= i;
			p %= mod;
		}

		long ans = 0;
		for (int i = 1; i <= n; i++) {
			ans += p * modinv(dep[i], mod) % mod;
			ans %= mod;
		}
		System.out.println(ans);
	}

	static long modinv(long a, long m) {
		long b = m;
		long u = 1;
		long v = 0;
		long tmp = 0;

		while (b > 0) {
			long t = a / b;
			a -= t * b;
			tmp = a;
			a = b;
			b = tmp;

			u -= t * v;
			tmp = u;
			u = v;
			v = tmp;
		}

		u %= m;
		if (u < 0) u += m;
		return u;
	}
}
0