結果

問題 No.827 総神童数
ユーザー polyomino_24
提出日時 2019-05-03 22:01:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 2,251 bytes
コンパイル時間 1,097 ms
コンパイル使用メモリ 123,660 KB
最終ジャッジ日時 2025-01-07 03:24:18
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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