結果

問題 No.827 総神童数
ユーザー rpy3cpprpy3cpp
提出日時 2019-05-03 23:43:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 2,093 bytes
コンパイル時間 1,775 ms
コンパイル使用メモリ 173,744 KB
実行使用メモリ 17,504 KB
最終ジャッジ日時 2023-08-30 06:41:30
合計ジャッジ時間 6,906 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 160 ms
17,504 KB
testcase_10 AC 48 ms
8,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 21 ms
5,360 KB
testcase_13 AC 129 ms
14,916 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 49 ms
7,848 KB
testcase_16 AC 121 ms
14,916 KB
testcase_17 AC 147 ms
16,960 KB
testcase_18 AC 60 ms
9,176 KB
testcase_19 AC 187 ms
17,372 KB
testcase_20 AC 130 ms
13,860 KB
testcase_21 AC 86 ms
10,904 KB
testcase_22 AC 141 ms
14,660 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 163 ms
16,108 KB
testcase_25 AC 109 ms
13,016 KB
testcase_26 AC 159 ms
16,376 KB
testcase_27 AC 98 ms
11,816 KB
testcase_28 AC 95 ms
11,724 KB
testcase_29 AC 72 ms
10,068 KB
testcase_30 AC 19 ms
5,156 KB
testcase_31 AC 53 ms
8,388 KB
testcase_32 AC 52 ms
8,380 KB
testcase_33 AC 172 ms
17,168 KB
testcase_34 AC 159 ms
15,904 KB
testcase_35 AC 55 ms
8,536 KB
testcase_36 AC 80 ms
10,840 KB
testcase_37 AC 107 ms
12,692 KB
testcase_38 AC 173 ms
16,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9+7;

long long pow_mod(long long b, long long p, long long mod = 1'000'000'007){
    long long ret = 1;
    while (p){
        if (p & 1) ret = (ret * b) % mod;
        b = (b * b) % mod;
        p >>= 1;
    }
    return ret;
}

class ModuloMath{
private:
    long long N;
    long long mod;
public:
    vector<long long> fac;
    vector<long long> invfac;
    ModuloMath(long long N, long long mod = 1'000'000'007): N{N}, mod{mod}, fac(N + 1, 1), invfac(N + 1, 1){
        for (long long i = 1; i <= N; ++i) fac[i] = (fac[i - 1] * i) % mod;
        invfac[N] = pow_mod(fac[N], mod - 2, mod);
        for (long long i = N; i != 1; --i) invfac[i - 1] = (invfac[i] * i) % mod;
    }
    long long nCr(long long n, long long r){return (r > n or r < 0) ? 0 : (fac[n] * invfac[n - r] % mod * invfac[r] % mod);}
    long long nPr(long long n, long long r){return (r > n or r < 0) ? 0 : (fac[n] * invfac[n - r] % mod);}
    long long nHr(long long n, long long r){return nCr(n + r - 1, r);}
};

void fill_depth(int N, const vector<vector<int>> &Es, vector<int> &depth){
    depth[0] = 0;
    vector<int> pool = {0};
    vector<int> new_pool;
    int d = 0;
    while (pool.size() >= 1){
        ++d;
        new_pool.clear();
        for (auto p : pool){
            for (auto q : Es[p]){
                if (depth[q] != -1) continue;
                depth[q] = d;
                new_pool.push_back(q);
            }
        }
        swap(new_pool, pool);
    }
    return;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    ModuloMath mm(N + 1);
    vector<vector<int>> Es(N, vector<int>());
    for (int i = 1; i < N; ++i){
        int u, v;
        cin >> u >> v;
        Es[u - 1].push_back(v - 1);
        Es[v - 1].push_back(u - 1);
    }
    vector<int> depth(N, -1);
    fill_depth(N, Es, depth);
    long long f = mm.fac[N];
    long long ans = 0;
    for (auto d : depth){
        (ans += (f * pow_mod(d + 1, mod-2)) % mod) %= mod;
    }
    cout << ans << endl;
    return 0;
}

0