結果

問題 No.827 総神童数
ユーザー coco18000coco18000
提出日時 2019-05-03 23:18:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,519 bytes
コンパイル時間 1,838 ms
コンパイル使用メモリ 174,316 KB
実行使用メモリ 24,500 KB
最終ジャッジ日時 2023-08-30 06:40:26
合計ジャッジ時間 6,200 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
15,836 KB
testcase_01 AC 6 ms
11,188 KB
testcase_02 AC 5 ms
11,284 KB
testcase_03 AC 6 ms
11,256 KB
testcase_04 AC 5 ms
11,148 KB
testcase_05 AC 6 ms
11,124 KB
testcase_06 AC 6 ms
11,192 KB
testcase_07 AC 6 ms
11,228 KB
testcase_08 AC 5 ms
11,184 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

struct Combination
{
    void init(ll n)
    {
        mNumTbl.resize(n + 1);
        mInverseNumTbl.resize(n + 1);
        mNumTbl[0] = 1;
        mInverseNumTbl[0] = 1;
        FOR(i, n + 1, 1)
        {
            mNumTbl[i] = (mNumTbl[i - 1] * i) % MOD;
        }
        FOR(i, n + 1, 1)
        {
            mInverseNumTbl[i] = modpow(mNumTbl[i]);
        }
    }

    ll get(ll n, ll r)
    {
        if (n < r || n < 0)
            return 0;
        return (((mNumTbl[n] * mInverseNumTbl[r]) % MOD) * mInverseNumTbl[n - r]) % MOD;
    }

    //! バイナリ方で累乗を計算
    ll modpow(ll n)
    {
        ll s = 1, p = n;
        for (ll i = 0; (1 << i) <= MOD - 2; ++i, p = (p * p) % MOD)
        {
            if (((MOD - 2) & (1 << i)) == 0)
                continue;
            s *= p;
            s %= MOD;
        }
        return s;
    }

    vector<ll> mInverseNumTbl;
    vector<ll> mNumTbl;
};

struct Vertex
{
    vector<int> n;
    ll depth = INF;
};

Vertex V[200005];
ll dp[200005];
ll N;
Combination cmb;

ll get(int n)
{
    int d = V[n].depth;
    ll c = 0;
    FOR(i, N + 1, 1)
    {
        c += cmb.get(i - 1, d);
        c %= MOD;
    }
    return (((dp[d] * c) % MOD) * dp[N - 1 - d]) % MOD;
}

int main()
{
    cin >> N;
    REP(i, N - 1)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        V[a].n.push_back(b);
        V[b].n.push_back(a);
    }

    queue<pll> q;
    q.push(pll(0, 0));
    while (!q.empty())
    {
        pll t = q.front();
        q.pop();
        V[t.first].depth = t.second;
        int nd = t.second + 1;
        REP(i, V[t.first].n.size())
        {
            int n = V[t.first].n[i];
            if (nd >= V[n].depth)
                continue;
            q.push(pll(n, nd));
        }
    }
    ll t = 1;
    memset(dp, 0, sizeof(dp));
    cmb.init(N + 1);
    FOR(i, N + 1, 1)
    {
        t = (t * i) % MOD;
        dp[i] = t;
    }
    dp[0] = 1;

    // for (int i = N - 1; i >= 0; --i)
    // {
    //     dp[i] += dp[i + 1];
    //     dp[i] %= MOD;
    // }
    // dp[0] = (t * N) % MOD;
    ll ans = 0;
    REP(i, N)
    {
        ans += get(i); //dp[V[i].depth];
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}
0