結果

問題 No.827 総神童数
ユーザー kyort0n
提出日時 2019-05-03 21:36:55
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 393 ms / 2,000 ms
コード長 2,049 bytes
コンパイル時間 1,924 ms
コンパイル使用メモリ 171,168 KB
実行使用メモリ 53,772 KB
最終ジャッジ日時 2024-12-31 17:35:09
合計ジャッジ時間 15,405 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;
ll N;
vector<ll> pathes[200500];
ll parent[200500];
ll depth[200500];
ll depthnum[200050];
ll inv[1000000];
ll FactorialInv[1000000];
ll Factorial[1000000];
ll beki(ll a, ll b){
    if(b == 0){
        return 1;
    }
    ll ans = beki(a, b / 2);
    ans = ans * ans % mod;
    if(b % 2 == 1){
        ans = ans * a % mod;
    }
    return ans;
}
void init_combination(){
    inv[1] = 1;
    FactorialInv[1] = 1;
    Factorial[1] = 1;
    Factorial[0] = 1;
    FactorialInv[0] = 1;
    inv[0] = 1;
    for(int i = 2; i < 1000000; i++){
        inv[i] = beki(i, mod - 2);
        Factorial[i] = Factorial[i - 1] * i % mod;
        FactorialInv[i] = FactorialInv[i - 1] * inv[i] % mod;
    }
}
ll combination(ll a, ll b){
    if((a == b) || (b == 0)){
        return 1;
    }
    ll ans = Factorial[a] * FactorialInv[b] % mod;
    ans = ans * FactorialInv[a - b] % mod;
    return ans;
}

void dfs(int from) {
    depthnum[depth[from]]++;
    for(int i = 0; i < pathes[from].size(); i++) {
        int to = pathes[from][i];
        if(to == parent[from]) continue;
        parent[to] = from;
        depth[to] = depth[from] + 1;
        dfs(to);
    }
}


int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    init_combination();
    for(int i = 1; i < N; i++) {
        int u, v;
        cin >> u >> v;
        pathes[u].push_back(v);
        pathes[v].push_back(u);
    }
    depth[1] = 0;
    dfs(1);
    //for(int i = 0; i < N; i++) cerr << i << " " << depthnum[i] << endl;
    ll ans = 0;
    for(ll d = 0; d < N; d++) {
        ll num = combination(N, d + 1) * Factorial[N - d - 1];
        num %= mod;
        num *= Factorial[d];
        num %= mod;
        ans += num * depthnum[d];
        ans %= mod;
        //cerr << ans << endl;
    }
    cout << ans << endl;
    return 0;
}
0