結果

問題 No.827 総神童数
ユーザー 37zigen37zigen
提出日時 2019-05-03 22:41:42
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,659 bytes
コンパイル時間 2,266 ms
コンパイル使用メモリ 76,680 KB
実行使用メモリ 145,284 KB
最終ジャッジ日時 2023-08-30 06:17:11
合計ジャッジ時間 39,575 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 222 ms
78,512 KB
testcase_01 AC 219 ms
78,628 KB
testcase_02 AC 227 ms
78,468 KB
testcase_03 AC 220 ms
78,440 KB
testcase_04 AC 226 ms
78,520 KB
testcase_05 AC 228 ms
78,512 KB
testcase_06 AC 223 ms
78,704 KB
testcase_07 AC 223 ms
78,412 KB
testcase_08 AC 226 ms
78,516 KB
testcase_09 TLE -
testcase_10 AC 803 ms
121,288 KB
testcase_11 AC 393 ms
91,104 KB
testcase_12 AC 568 ms
103,736 KB
testcase_13 TLE -
testcase_14 AC 433 ms
93,592 KB
testcase_15 AC 791 ms
122,216 KB
testcase_16 AC 1,685 ms
132,468 KB
testcase_17 TLE -
testcase_18 AC 854 ms
122,332 KB
testcase_19 AC 1,395 ms
131,920 KB
testcase_20 AC 1,170 ms
124,228 KB
testcase_21 AC 993 ms
117,100 KB
testcase_22 AC 1,135 ms
124,748 KB
testcase_23 AC 373 ms
81,988 KB
testcase_24 AC 1,314 ms
129,068 KB
testcase_25 AC 1,123 ms
121,392 KB
testcase_26 AC 1,223 ms
128,936 KB
testcase_27 AC 969 ms
118,820 KB
testcase_28 AC 986 ms
119,504 KB
testcase_29 AC 968 ms
117,220 KB
testcase_30 AC 576 ms
98,804 KB
testcase_31 AC 879 ms
112,612 KB
testcase_32 AC 821 ms
112,404 KB
testcase_33 AC 1,385 ms
131,492 KB
testcase_34 AC 1,205 ms
127,212 KB
testcase_35 AC 889 ms
112,632 KB
testcase_36 AC 926 ms
116,816 KB
testcase_37 AC 1,134 ms
121,436 KB
testcase_38 AC 1,343 ms
129,288 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);

		ans = fac[N];

		long s = 0;

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

		ans = ans * s % mo;

		System.out.println(ans);
	}

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

}
0