結果

問題 No.827 総神童数
ユーザー hipopohipopo
提出日時 2020-04-21 19:46:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 4,074 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 142,880 KB
実行使用メモリ 23,844 KB
最終ジャッジ日時 2024-04-17 09:47:07
合計ジャッジ時間 7,218 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
8,192 KB
testcase_01 AC 8 ms
8,320 KB
testcase_02 AC 8 ms
8,448 KB
testcase_03 AC 9 ms
8,268 KB
testcase_04 AC 8 ms
8,272 KB
testcase_05 AC 8 ms
8,320 KB
testcase_06 AC 7 ms
8,268 KB
testcase_07 AC 9 ms
8,192 KB
testcase_08 AC 8 ms
8,272 KB
testcase_09 AC 215 ms
23,844 KB
testcase_10 AC 74 ms
13,308 KB
testcase_11 AC 11 ms
8,524 KB
testcase_12 AC 35 ms
10,500 KB
testcase_13 AC 167 ms
20,992 KB
testcase_14 AC 13 ms
8,652 KB
testcase_15 AC 67 ms
13,076 KB
testcase_16 AC 175 ms
19,456 KB
testcase_17 AC 204 ms
21,760 KB
testcase_18 AC 86 ms
14,464 KB
testcase_19 AC 225 ms
19,312 KB
testcase_20 AC 166 ms
16,436 KB
testcase_21 AC 117 ms
14,052 KB
testcase_22 AC 180 ms
17,216 KB
testcase_23 AC 8 ms
8,320 KB
testcase_24 AC 201 ms
18,048 KB
testcase_25 AC 142 ms
15,616 KB
testcase_26 AC 203 ms
18,364 KB
testcase_27 AC 128 ms
14,720 KB
testcase_28 AC 128 ms
14,720 KB
testcase_29 AC 100 ms
13,552 KB
testcase_30 AC 32 ms
9,552 KB
testcase_31 AC 80 ms
12,116 KB
testcase_32 AC 75 ms
12,160 KB
testcase_33 AC 224 ms
18,996 KB
testcase_34 AC 193 ms
18,168 KB
testcase_35 AC 77 ms
12,288 KB
testcase_36 AC 109 ms
13,928 KB
testcase_37 AC 146 ms
15,616 KB
testcase_38 AC 219 ms
18,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <functional>
#include <cstring>
#include <regex>
#include <random>
#include <cassert>
#include <stack>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)

const double PI = acos(-1.0);

using ll = long long;
using P = pair<int, int>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class T>
istream& operator >>(istream &is, vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i);
    return is;
}

template<class T, class U>
ostream& operator <<(ostream &os, pair<T, U> p) {
    cout << '(' << p.first << ", " << p.second << ')';
    return os;
}

template<class T>
void print(const vector<T> &v, const string &delimiter) { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; }

template<class T>
void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); }

struct ModInt {
    static ll mod;
    ll val;
    ModInt(ll v = 0) : val(v % mod) {}
    ModInt operator-() const { return ModInt(val ? mod - val : val); }
    ModInt &operator+=(ModInt a) {
        if ((val += a.val) >= mod) val -= mod;
        return *this;
    }
    ModInt &operator-=(ModInt a) {
        if ((val -= a.val) < 0) val += mod;
        return *this;
    }
    ModInt &operator*=(ModInt a) {
        val = (__uint128_t(val) * a.val) % mod;
        return *this;
    }
    ModInt &operator/=(ModInt a) {
        ll u = 1, v = a.val, s = 0, t = mod;
        while (v) {
            ll q = t / v;
            swap(s -= u * q, u);
            swap(t -= v * q, v);
        }
        a.val = (s < 0 ? s + mod : s);
        val /= t;
        return (*this) *= a;
    }
    ModInt inv() const { return ModInt(1) /= (*this); }
    bool operator<(ModInt x) const { return val < x.val; }
};
ll ModInt::mod = 1e9 + 7;

ostream &operator<<(ostream &os, ModInt a) {
    os << a.val;
    return os;
}
ModInt operator+(ModInt a, ModInt b) { return a += b; }
ModInt operator-(ModInt a, ModInt b) { return a -= b; }
ModInt operator*(ModInt a, ModInt b) { return a *= b; }
ModInt operator/(ModInt a, ModInt b) { return a /= b; }
ModInt pow(ModInt a, ll e) {
    ModInt x(1);
    for (; e > 0; e /= 2) {
        if (e % 2 == 1) x *= a;
        a *= a;
    }
    return x;
}

ModInt comb(int n, int k) {
    ModInt x = 1, y = 1;
    rep(i, k) {
        x *= n - i;
        y *= i + 1;
    }
    return x / y;
}

const long long MOD = 1000000007;
const int MAX = 200010;
long long fac[MAX], finv[MAX], inv[MAX];

void comb_init() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

long long mod_comb(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}


vector<vector<int>> graph;

int depth[200000];
void dfs(int v, int d, int pre = -1) {
    depth[v] = d;
    for (int to: graph[v]) if (to != pre) dfs(to, d + 1, v);
}

int main() {
    int n;
    cin >> n;
    graph.resize(n);
    rep(i, n - 1) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    
    dfs(0, 0);
    
    comb_init();
    ModInt ans = 0;
    ans += fac[n];
    repr(i, 1, n) { 
        ans += ModInt(mod_comb(n, depth[i] + 1)) * fac[depth[i]] * fac[n - depth[i] - 1];
    }
    cout << ans << endl;
}
0