結果

問題 No.827 総神童数
ユーザー face4face4
提出日時 2019-06-13 16:04:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 1,923 bytes
コンパイル時間 1,319 ms
コンパイル使用メモリ 82,316 KB
実行使用メモリ 19,328 KB
最終ジャッジ日時 2024-04-22 14:18:06
合計ジャッジ時間 6,621 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 187 ms
19,328 KB
testcase_10 AC 58 ms
8,960 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 24 ms
5,760 KB
testcase_13 AC 145 ms
16,128 KB
testcase_14 AC 6 ms
5,376 KB
testcase_15 AC 53 ms
8,448 KB
testcase_16 AC 143 ms
16,256 KB
testcase_17 AC 173 ms
18,560 KB
testcase_18 AC 67 ms
9,984 KB
testcase_19 AC 179 ms
19,328 KB
testcase_20 AC 131 ms
14,976 KB
testcase_21 AC 87 ms
11,776 KB
testcase_22 AC 145 ms
16,128 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 162 ms
17,408 KB
testcase_25 AC 119 ms
14,080 KB
testcase_26 AC 165 ms
17,536 KB
testcase_27 AC 97 ms
12,672 KB
testcase_28 AC 95 ms
12,544 KB
testcase_29 AC 76 ms
10,752 KB
testcase_30 AC 23 ms
5,504 KB
testcase_31 AC 57 ms
9,216 KB
testcase_32 AC 55 ms
8,832 KB
testcase_33 AC 174 ms
18,688 KB
testcase_34 AC 160 ms
17,536 KB
testcase_35 AC 59 ms
9,088 KB
testcase_36 AC 84 ms
11,648 KB
testcase_37 AC 110 ms
14,208 KB
testcase_38 AC 171 ms
18,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0