結果

問題 No.827 総神童数
ユーザー 37zigen
提出日時 2019-05-03 22:35:16
言語 Java8
(openjdk 1.8.0.191)
結果
AC  
実行時間 1,895 ms
コード長 1,622 Byte
コンパイル時間 1,760 ms
使用メモリ 128,616 KB
最終ジャッジ日時 2019-06-24 09:10:35

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
a_sample_1.txt AC 306 ms
45,008 KB
a_sample_2.txt AC 308 ms
45,008 KB
a_sample_3.txt AC 309 ms
45,048 KB
b_small_1.txt AC 304 ms
45,012 KB
b_small_4.txt AC 305 ms
45,012 KB
b_small_6.txt AC 325 ms
45,016 KB
b_small_7.txt AC 313 ms
45,008 KB
b_small_8.txt AC 312 ms
45,004 KB
b_small_10.txt AC 307 ms
45,012 KB
c_path_1.txt AC 1,877 ms
128,616 KB
c_path_2.txt AC 838 ms
82,252 KB
c_path_3.txt AC 361 ms
55,780 KB
c_path_4.txt AC 567 ms
65,620 KB
c_path_5.txt AC 1,524 ms
111,520 KB
c_path_6.txt AC 392 ms
57,656 KB
c_path_7.txt AC 798 ms
82,520 KB
c_path_8.txt AC 1,511 ms
100,864 KB
c_path_9.txt AC 1,727 ms
109,848 KB
c_path_10.txt AC 932 ms
87,052 KB
d_random_1.txt AC 1,895 ms
93,164 KB
d_random_2.txt AC 1,477 ms
86,104 KB
d_random_3.txt AC 1,148 ms
80,136 KB
d_random_4.txt AC 1,586 ms
88,136 KB
d_random_5.txt AC 340 ms
47,364 KB
d_random_6.txt AC 1,752 ms
90,212 KB
d_random_7.txt AC 1,381 ms
84,148 KB
d_random_8.txt AC 1,736 ms
90,380 KB
d_random_9.txt AC 1,253 ms
81,924 KB
d_random_10.txt AC 1,236 ms
81,020 KB
d_random_11.txt AC 1,061 ms
78,172 KB
d_random_12.txt AC 569 ms
61,544 KB
d_random_13.txt AC 896 ms
74,908 KB
d_random_14.txt AC 886 ms
74,756 KB
d_random_15.txt AC 1,876 ms
92,684 KB
d_random_16.txt AC 1,733 ms
90,184 KB
d_random_17.txt AC 896 ms
74,932 KB
d_random_18.txt AC 1,132 ms
79,240 KB
d_random_19.txt AC 1,382 ms
84,140 KB
d_random_20.txt AC 1,833 ms
92,280 KB
テストケース一括ダウンロード

ソースコード

diff #
import java.util.*;
import java.io.*;

public class Main {
	public static void main(String[] args) throws Exception {
		new Main().run();
	}

	int[] depth;
	ArrayList<Integer>[] g;
	long mo = (long) 1e9 + 7;
	long[] fac = new long[1000000];
	long[] ifac = new long[1000000];
	long[] inv = new long[1000000];

	{
		fac[0] = ifac[0] = inv[0] = inv[1] = 1;
		for (int i = 1; i < fac.length; ++i) {
			fac[i] = fac[i - 1] * i % mo;
		}
		// 0 = p/m * m + p%mo
		// inv(m) = inv(p%m)*(p/m)
		for (int i = 2; i < inv.length; ++i) {
			inv[i] = mo - inv[(int) (mo % i)] * (mo / i) % mo;
		}
		for (int i = 1; i < ifac.length; ++i) {
			ifac[i] = ifac[i - 1] * inv[i] % mo;
		}
	}

	long pow(long a, long n) {
		long ret = 1;
		for (; n > 0; n >>= 1, a = a * a % mo) {
			ret = ret * a % mo;
		}
		return ret;
	}

	void dfs(int cur, int par) {
		for (int dst : g[cur]) {
			if (dst == par) {
				continue;
			}
			depth[dst] = depth[cur] + 1;
			dfs(dst, cur);
		}
	}

	void run() throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] u = new int[N - 1];
		int[] v = new int[N - 1];
		depth = new int[N];

		long ans = 0;

		g = new ArrayList[N];
		for (int i = 0; i < N; ++i) {
			g[i] = new ArrayList<>();
		}

		for (int i = 0; i < N - 1; ++i) {
			u[i] = sc.nextInt();
			v[i] = sc.nextInt();
			--u[i];
			--v[i];
			g[u[i]].add(v[i]);
			g[v[i]].add(u[i]);
		}

		dfs(0, -1);

		for (int i = 0; i < N; ++i) {
			ans = (ans + fac[N] * inv[depth[i] + 1] % mo) % mo;
		}

		System.out.println(ans);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0