結果

問題 No.827 総神童数
ユーザー gazellegazelle
提出日時 2019-05-03 23:03:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,651 bytes
コンパイル時間 1,224 ms
コンパイル使用メモリ 123,400 KB
実行使用メモリ 32,004 KB
最終ジャッジ日時 2023-08-30 06:36:08
合計ジャッジ時間 6,108 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,328 KB
testcase_01 AC 6 ms
12,360 KB
testcase_02 WA -
testcase_03 AC 6 ms
12,344 KB
testcase_04 AC 7 ms
12,352 KB
testcase_05 AC 6 ms
12,364 KB
testcase_06 AC 6 ms
12,356 KB
testcase_07 AC 6 ms
12,352 KB
testcase_08 AC 6 ms
12,176 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<unordered_map>
#include<stack>
#include<queue>
#include<math.h>
#include<functional>
#include<bitset>
#include<cassert>
using namespace std;
using lint = long long;
using ld = long double;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define MOD 1000000007LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i,n,m) for(lint i=n;i<(int)m;i++)
#define REP(i,n) FOR(i,0,n)
#define DUMP(a) REP(d,a.size()){cout<<a[d];if(d!=a.size()-1)cout<<" ";else cout<<endl;}
#define ALL(v) v.begin(),v.end()
#define UNIQUE(v)  sort(ALL(v));v.erase(unique(ALL(v)),v.end());
#define pb push_back

int n;
vector<vector<int>> tree;
lint ans = 0;

#define MAX_N 1000000
long long extgcd(long long a, long long b, long long& x, long long& y) {
	long long d = a;
	if (b != 0) {
		d = extgcd(b, a % b, y, x);
		y -= (a / b) * x;
	} else {
		x = 1; y = 0;
	}
	return d;
}
long long mod_inverse(long long a, long long m) {
	long long x, y;
	if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;
	else return -1;
}
vector<long long> fact(MAX_N+1, INF);
long long mod_fact(long long n, long long& e) {
	if(fact[0] == INF) {
		fact[0]=1;
		if(MAX_N != 0) fact[1]=1;
		for(lint i = 2; i <= MAX_N; ++i) {
			fact[i] = (fact[i-1] * i) % MOD;
		}
	}
	e = 0;
	if(n == 0) return 1;
	long long res = mod_fact(n / MOD, e);
	e += n / MOD;
	if((n / MOD) % 2 != 0) return (res * (MOD - fact[n % MOD])) % MOD;
	return (res * fact[n % MOD]) % MOD;
}
// return nCk
long long mod_comb(long long n, long long k) {
	if(n < 0 || k < 0 || n < k) return 0;
	long long e1, e2, e3;
	long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);
	if(e1 > e2 + e3) return 0;
	return (a1 * mod_inverse((a2 * a3) % MOD, MOD)) % MOD;
}

lint _pow(lint a, lint n) {
	if(n == 0) return 1;
	else {
		lint res = 1;
		lint buf = a;
		while(n > 0) {
			if(n % 2 == 1) {
				res *= buf;
				res %= MOD;
			}
			buf *= buf;
			buf %= MOD;
			n/=2;
		}
		return res;
	}
}

lint factoria[202020];

void dfs(int pos, int par, int height) {
	ans += factoria[n] * _pow(height, MOD - 2);
	ans %= MOD;
	REP(i, tree[pos].size()) {
		if(tree[pos][i] == par) continue;
		dfs(tree[pos][i], pos, height + 1);
	}
	return;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	factoria[0] = 1;
	FOR(i, 1, 202020) {
		factoria[i] = factoria[i - 1] * i;
	}
	cin >> n;
	tree.resize(n);
	REP(i, n - 1) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		tree[u].pb(v);
		tree[v].pb(u);
	}
	dfs(0, -1, 1);
	cout << ans << endl;
	return 0;
}
/* --------------------------------------- */
0