結果

問題 No.827 総神童数
ユーザー hipopohipopo
提出日時 2020-04-21 19:46:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 4,074 bytes
コンパイル時間 1,689 ms
コンパイル使用メモリ 144,200 KB
実行使用メモリ 23,808 KB
最終ジャッジ日時 2024-10-09 03:08:10
合計ジャッジ時間 7,260 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
8,192 KB
testcase_01 AC 9 ms
8,192 KB
testcase_02 AC 9 ms
8,192 KB
testcase_03 AC 9 ms
8,192 KB
testcase_04 AC 9 ms
8,192 KB
testcase_05 AC 9 ms
8,192 KB
testcase_06 AC 9 ms
8,192 KB
testcase_07 AC 10 ms
8,192 KB
testcase_08 AC 9 ms
8,064 KB
testcase_09 AC 213 ms
23,808 KB
testcase_10 AC 72 ms
13,312 KB
testcase_11 AC 11 ms
8,320 KB
testcase_12 AC 34 ms
10,368 KB
testcase_13 AC 168 ms
20,992 KB
testcase_14 AC 14 ms
8,704 KB
testcase_15 AC 68 ms
13,056 KB
testcase_16 AC 171 ms
19,328 KB
testcase_17 AC 208 ms
21,888 KB
testcase_18 AC 88 ms
14,464 KB
testcase_19 AC 226 ms
19,328 KB
testcase_20 AC 159 ms
16,384 KB
testcase_21 AC 114 ms
14,080 KB
testcase_22 AC 176 ms
17,152 KB
testcase_23 AC 10 ms
8,320 KB
testcase_24 AC 198 ms
18,048 KB
testcase_25 AC 143 ms
15,616 KB
testcase_26 AC 196 ms
18,176 KB
testcase_27 AC 120 ms
14,720 KB
testcase_28 AC 121 ms
14,592 KB
testcase_29 AC 99 ms
13,312 KB
testcase_30 AC 31 ms
9,728 KB
testcase_31 AC 77 ms
12,288 KB
testcase_32 AC 73 ms
11,904 KB
testcase_33 AC 217 ms
19,072 KB
testcase_34 AC 195 ms
18,176 KB
testcase_35 AC 74 ms
12,288 KB
testcase_36 AC 113 ms
13,952 KB
testcase_37 AC 141 ms
15,616 KB
testcase_38 AC 216 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