結果
問題 | No.827 総神童数 |
ユーザー |
|
提出日時 | 2019-05-03 22:27:41 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 108 ms / 2,000 ms |
コード長 | 1,357 bytes |
コンパイル時間 | 1,736 ms |
コンパイル使用メモリ | 172,956 KB |
実行使用メモリ | 29,336 KB |
最終ジャッジ日時 | 2024-12-31 18:11:21 |
合計ジャッジ時間 | 5,382 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;const ll MOD = 1000000007;ll modpow(ll x, ll n, ll mod = MOD) {ll res = 1;while (n > 0) {if (n & 1) res = res * x % mod;x = x * x % mod;n >>= 1;}return res;}const int MAXV = 200000;ll f[200005], fi[200005];vector< vector<int> > g;void dfs(int cur, int par, vector<int>& dist) {for (int nex : g[cur]) {if (nex == par) continue;dist[nex] = dist[cur] + 1;dfs(nex, cur, dist);}}ll comb(int r, int c) {if (r < c) return 0;return f[r] * fi[c] % MOD * fi[r - c] % MOD;}int main() {cin.tie(0);ios::sync_with_stdio(false);f[0] = 1;for (int i = 1; i <= MAXV; i++) {f[i] = f[i - 1] * i % MOD;}fi[MAXV] = modpow(f[MAXV], MOD - 2);for (int i = MAXV; i > 0; i--) {fi[i-1] = fi[i] * i % MOD;}int n;cin >> n;g.resize(n);for (int i = 1; i < n; i++) {int u, v;cin >> u >> v;u--; v--;g[u].push_back(v);g[v].push_back(u);}vector<int> dist(n, 0);dfs(0, -1, dist);ll ans = 0;for (int i = 0; i < n; i++) {(ans += comb(n, dist[i] + 1) * f[dist[i]] % MOD * f[n - dist[i] - 1] % MOD) %= MOD;}cout << ans << endl;return 0;}