結果

問題 No.827 総神童数
ユーザー polyomino_24polyomino_24
提出日時 2019-05-03 22:01:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 2,251 bytes
コンパイル時間 1,137 ms
コンパイル使用メモリ 126,544 KB
実行使用メモリ 30,116 KB
最終ジャッジ日時 2023-08-30 06:08:50
合計ジャッジ時間 5,972 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
18,268 KB
testcase_01 AC 27 ms
18,204 KB
testcase_02 AC 28 ms
18,208 KB
testcase_03 AC 27 ms
18,460 KB
testcase_04 AC 27 ms
18,236 KB
testcase_05 AC 27 ms
18,228 KB
testcase_06 AC 28 ms
18,212 KB
testcase_07 AC 27 ms
18,268 KB
testcase_08 AC 27 ms
18,204 KB
testcase_09 AC 131 ms
30,116 KB
testcase_10 AC 57 ms
22,200 KB
testcase_11 AC 28 ms
18,424 KB
testcase_12 AC 39 ms
20,064 KB
testcase_13 AC 103 ms
28,004 KB
testcase_14 AC 30 ms
18,860 KB
testcase_15 AC 54 ms
22,092 KB
testcase_16 AC 103 ms
26,148 KB
testcase_17 AC 123 ms
27,756 KB
testcase_18 AC 65 ms
22,996 KB
testcase_19 AC 140 ms
24,364 KB
testcase_20 AC 101 ms
22,744 KB
testcase_21 AC 75 ms
21,600 KB
testcase_22 AC 110 ms
23,160 KB
testcase_23 AC 28 ms
18,340 KB
testcase_24 AC 122 ms
23,728 KB
testcase_25 AC 93 ms
22,508 KB
testcase_26 AC 123 ms
23,888 KB
testcase_27 AC 83 ms
22,164 KB
testcase_28 AC 80 ms
21,932 KB
testcase_29 AC 67 ms
21,128 KB
testcase_30 AC 39 ms
19,220 KB
testcase_31 AC 59 ms
20,792 KB
testcase_32 AC 57 ms
20,680 KB
testcase_33 AC 135 ms
24,184 KB
testcase_34 AC 124 ms
23,704 KB
testcase_35 AC 58 ms
20,628 KB
testcase_36 AC 75 ms
21,476 KB
testcase_37 AC 93 ms
22,468 KB
testcase_38 AC 138 ms
24,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}

vector<int>g[200005];
int dist[200005];
void dfs(int s){
    for(auto x: g[s]){
        if(dist[x] == -1){
            dist[x] = dist[s] + 1;
            dfs(x);
        }
    }
}
ll mod=1000000007;
const int NUM_=400001;

ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];

ll combi(ll N_, ll C_) {
    if (fact[0]==0) {
        inv[1]=fact[0]=factr[0]=1;
        for (int i=2;i<=NUM_;++i) inv[i] = inv[mod % i] * (mod - mod / i) % mod;
        for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mod, factr[i]=factr[i-1]*inv[i]%mod;
    }
    if(C_<0 || C_>N_) return 0;
    return factr[C_]*fact[N_]%mod*factr[N_-C_]%mod;
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    combi(1,1);
    memset(dist,-1,sizeof(dist));
    for(int i = 0; i < n - 1; i++){
        int a,b;
        cin >>a >> b;
        a--,b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dist[0] = 0;
    dfs(0);
    ll ans = 0;
    for(int i = 0; i < n; i++){
        int t = dist[i];
        show(i,t);
        ans += fact[n] * factr[t + 1] % mod * fact[t] % mod;
        ans %= mod;
        show(ans);
    }
    cout << ans << endl;
}
0