結果

問題 No.827 総神童数
ユーザー kyort0nkyort0n
提出日時 2019-05-03 21:36:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 374 ms / 2,000 ms
コード長 2,049 bytes
コンパイル時間 1,853 ms
コンパイル使用メモリ 168,068 KB
実行使用メモリ 50,976 KB
最終ジャッジ日時 2023-08-30 05:59:13
合計ジャッジ時間 15,033 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 237 ms
36,160 KB
testcase_01 AC 237 ms
36,148 KB
testcase_02 AC 236 ms
36,200 KB
testcase_03 AC 237 ms
36,212 KB
testcase_04 AC 236 ms
36,444 KB
testcase_05 AC 238 ms
36,156 KB
testcase_06 AC 237 ms
36,264 KB
testcase_07 AC 238 ms
36,400 KB
testcase_08 AC 236 ms
36,196 KB
testcase_09 AC 344 ms
50,976 KB
testcase_10 AC 267 ms
40,740 KB
testcase_11 AC 238 ms
36,380 KB
testcase_12 AC 248 ms
38,636 KB
testcase_13 AC 328 ms
48,464 KB
testcase_14 AC 240 ms
36,964 KB
testcase_15 AC 270 ms
40,968 KB
testcase_16 AC 330 ms
45,444 KB
testcase_17 AC 342 ms
47,524 KB
testcase_18 AC 283 ms
42,068 KB
testcase_19 AC 372 ms
43,476 KB
testcase_20 AC 331 ms
41,404 KB
testcase_21 AC 302 ms
39,960 KB
testcase_22 AC 340 ms
41,896 KB
testcase_23 AC 237 ms
36,216 KB
testcase_24 AC 358 ms
42,476 KB
testcase_25 AC 312 ms
40,980 KB
testcase_26 AC 355 ms
42,472 KB
testcase_27 AC 299 ms
40,556 KB
testcase_28 AC 298 ms
40,312 KB
testcase_29 AC 289 ms
39,508 KB
testcase_30 AC 248 ms
37,244 KB
testcase_31 AC 274 ms
38,948 KB
testcase_32 AC 270 ms
38,916 KB
testcase_33 AC 374 ms
43,016 KB
testcase_34 AC 352 ms
42,484 KB
testcase_35 AC 273 ms
38,852 KB
testcase_36 AC 296 ms
39,928 KB
testcase_37 AC 321 ms
41,052 KB
testcase_38 AC 353 ms
43,040 KB
権限があれば一括ダウンロードができます

ソースコード

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