結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
12,344 KB
testcase_01 AC 12 ms
12,284 KB
testcase_02 AC 12 ms
12,288 KB
testcase_03 AC 12 ms
12,172 KB
testcase_04 AC 13 ms
12,456 KB
testcase_05 AC 12 ms
12,300 KB
testcase_06 AC 12 ms
12,284 KB
testcase_07 AC 13 ms
12,448 KB
testcase_08 AC 12 ms
12,316 KB
testcase_09 AC 194 ms
34,716 KB
testcase_10 AC 71 ms
19,428 KB
testcase_11 AC 15 ms
12,576 KB
testcase_12 AC 36 ms
15,936 KB
testcase_13 AC 157 ms
30,940 KB
testcase_14 AC 17 ms
13,152 KB
testcase_15 AC 69 ms
19,860 KB
testcase_16 AC 157 ms
26,812 KB
testcase_17 AC 187 ms
29,976 KB
testcase_18 AC 87 ms
21,312 KB
testcase_19 AC 224 ms
23,116 KB
testcase_20 AC 155 ms
20,112 KB
testcase_21 AC 115 ms
18,108 KB
testcase_22 AC 173 ms
20,908 KB
testcase_23 AC 13 ms
12,564 KB
testcase_24 AC 198 ms
22,040 KB
testcase_25 AC 150 ms
19,752 KB
testcase_26 AC 204 ms
21,944 KB
testcase_27 AC 129 ms
18,944 KB
testcase_28 AC 128 ms
18,648 KB
testcase_29 AC 100 ms
17,568 KB
testcase_30 AC 37 ms
14,004 KB
testcase_31 AC 81 ms
16,248 KB
testcase_32 AC 74 ms
16,324 KB
testcase_33 AC 217 ms
22,800 KB
testcase_34 AC 199 ms
22,008 KB
testcase_35 AC 79 ms
16,460 KB
testcase_36 AC 113 ms
18,120 KB
testcase_37 AC 148 ms
19,696 KB
testcase_38 AC 215 ms
22,516 KB
権限があれば一括ダウンロードができます

ソースコード

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 factoria[202020];

void dfs(int pos, int par, int height) {
	ans += (mod_comb(n, height) * factoria[n - height]) % MOD * factoria[height - 1];
	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;
		factoria[i] %= MOD;
	}
	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 % MOD + MOD) % MOD << endl;
	return 0;
}
/* --------------------------------------- */
0