結果

問題 No.827 総神童数
ユーザー 37zigen
提出日時 2019-05-03 22:35:16
言語 Java8
(openjdk 1.8.0.222)
結果
AC  
実行時間 1,860 ms
コード長 1,622 Byte
コンパイル時間 1,676 ms
使用メモリ 130,108 KB
最終ジャッジ日時 2019-09-05 08:00:27

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
a_sample_1.txt AC 248 ms
46,968 KB
a_sample_2.txt AC 248 ms
46,664 KB
a_sample_3.txt AC 247 ms
46,696 KB
b_small_1.txt AC 247 ms
46,672 KB
b_small_4.txt AC 254 ms
46,668 KB
b_small_6.txt AC 248 ms
46,676 KB
b_small_7.txt AC 257 ms
46,668 KB
b_small_8.txt AC 257 ms
46,972 KB
b_small_10.txt AC 256 ms
46,664 KB
c_path_1.txt AC 1,795 ms
130,108 KB
c_path_2.txt AC 798 ms
83,748 KB
c_path_3.txt AC 325 ms
57,336 KB
c_path_4.txt AC 519 ms
65,624 KB
c_path_5.txt AC 1,479 ms
113,016 KB
c_path_6.txt AC 346 ms
59,156 KB
c_path_7.txt AC 741 ms
82,520 KB
c_path_8.txt AC 1,442 ms
102,360 KB
c_path_9.txt AC 1,680 ms
111,340 KB
c_path_10.txt AC 873 ms
87,052 KB
d_random_1.txt AC 1,860 ms
94,608 KB
d_random_2.txt AC 1,430 ms
87,568 KB
d_random_3.txt AC 1,096 ms
81,596 KB
d_random_4.txt AC 1,515 ms
89,588 KB
d_random_5.txt AC 297 ms
48,920 KB
d_random_6.txt AC 1,668 ms
90,208 KB
d_random_7.txt AC 1,314 ms
85,604 KB
d_random_8.txt AC 1,683 ms
90,384 KB
d_random_9.txt AC 1,198 ms
83,392 KB
d_random_10.txt AC 1,173 ms
82,484 KB
d_random_11.txt AC 998 ms
79,628 KB
d_random_12.txt AC 490 ms
62,996 KB
d_random_13.txt AC 826 ms
76,376 KB
d_random_14.txt AC 810 ms
76,224 KB
d_random_15.txt AC 1,807 ms
92,684 KB
d_random_16.txt AC 1,680 ms
91,628 KB
d_random_17.txt AC 839 ms
76,396 KB
d_random_18.txt AC 1,048 ms
79,236 KB
d_random_19.txt AC 1,298 ms
84,140 KB
d_random_20.txt AC 1,736 ms
93,724 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