結果

問題 No.827 総神童数
ユーザー 37zigen37zigen
提出日時 2019-05-03 22:35:16
言語 Java19
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,622 bytes
コンパイル時間 2,407 ms
コンパイル使用メモリ 76,928 KB
実行使用メモリ 142,484 KB
最終ジャッジ日時 2023-08-30 06:15:09
合計ジャッジ時間 40,597 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 230 ms
78,432 KB
testcase_01 AC 226 ms
78,480 KB
testcase_02 AC 231 ms
78,436 KB
testcase_03 AC 228 ms
78,528 KB
testcase_04 AC 230 ms
78,836 KB
testcase_05 AC 231 ms
78,632 KB
testcase_06 AC 227 ms
78,544 KB
testcase_07 AC 229 ms
78,608 KB
testcase_08 AC 235 ms
78,504 KB
testcase_09 TLE -
testcase_10 AC 813 ms
121,152 KB
testcase_11 AC 405 ms
91,424 KB
testcase_12 AC 592 ms
103,984 KB
testcase_13 TLE -
testcase_14 AC 426 ms
91,880 KB
testcase_15 AC 766 ms
121,920 KB
testcase_16 AC 1,713 ms
132,088 KB
testcase_17 TLE -
testcase_18 AC 849 ms
121,236 KB
testcase_19 AC 1,371 ms
132,028 KB
testcase_20 AC 1,180 ms
124,012 KB
testcase_21 AC 1,013 ms
116,676 KB
testcase_22 AC 1,129 ms
125,608 KB
testcase_23 AC 376 ms
81,980 KB
testcase_24 AC 1,301 ms
128,932 KB
testcase_25 AC 1,113 ms
121,656 KB
testcase_26 AC 1,206 ms
128,060 KB
testcase_27 AC 985 ms
118,828 KB
testcase_28 AC 996 ms
119,304 KB
testcase_29 AC 975 ms
117,188 KB
testcase_30 AC 569 ms
98,364 KB
testcase_31 AC 876 ms
113,196 KB
testcase_32 AC 836 ms
112,548 KB
testcase_33 AC 1,377 ms
131,508 KB
testcase_34 AC 1,223 ms
126,352 KB
testcase_35 AC 895 ms
112,376 KB
testcase_36 AC 937 ms
116,464 KB
testcase_37 AC 1,135 ms
121,760 KB
testcase_38 AC 1,331 ms
129,672 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