結果
| 問題 |
No.827 総神童数
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-06-13 16:04:35 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 177 ms / 2,000 ms |
| コード長 | 1,923 bytes |
| コンパイル時間 | 765 ms |
| コンパイル使用メモリ | 81,396 KB |
| 実行使用メモリ | 19,388 KB |
| 最終ジャッジ日時 | 2024-10-14 13:25:13 |
| 合計ジャッジ時間 | 6,305 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll modpow(ll a, ll b, ll p = 1e9+7){
if(b == 0) return 1;
if(b % 2 == 0){
ll d = modpow(a, b/2, p);
return (d*d) % p;
}else{
return (a%p * modpow(a, b-1, p)) % p;
}
}
struct ModComb{
vector<ll> po, inv;
ll N;
ModComb(ll n) : N(n), po(n), inv(n) {
po[0] = 1;
for(int i = 1; i < N; i++) po[i] = (po[i-1] * i) % mod;
inv[N-1] = modpow(po[N-1], mod-2, mod);
for(int i = N-2; i >= 0; i--) inv[i] = (inv[i+1] * (i+1)) % mod;
}
ll nCk(ll n, ll k){
if(k == 0) return 1;
if(n < k) return 0;
return (((po[n]*inv[n-k])%mod)*inv[k])%mod;
}
ll nPk(ll n, ll k){
if(k == 0) return 1;
if(n < k) return 0;
return (po[n]*inv[n-k])%mod;
}
ll nHk(ll n, ll k){
if(n == 0 && k == 0) return 1;
return nCk(n+k-1, k-1);
}
};
int main(){
int n;
cin >> n;
vector<int> dep(n, 0);
vector<int> v[n];
for(int i = 0; i < n-1; i++){
int a, b;
cin >> a >> b;
a--, b--;
v[a].push_back(b);
v[b].push_back(a);
}
queue<pair<int,int>> q;
dep[0] = 1;
for(int next : v[0]) q.push({next, 2});
while(!q.empty()){
auto p = q.front(); q.pop();
if(dep[p.first]) continue;
dep[p.first] = p.second;
for(int next : v[p.first]){
if(dep[next] == 0) q.push({next, p.second+1});
}
}
vector<ll> fact(n+1);
fact[0] = 1;
for(int i = 1; i <= n; i++) fact[i] = fact[i-1] * i % mod;
ModComb mc(n+1);
ll ans = 0;
for(int i = 0; i < n; i++){
ans += mc.nCk(n, dep[i]) * fact[dep[i]-1] % mod * fact[n-dep[i]] % mod;
ans %= mod;
}
cout << ans << endl;
return 0;
}