結果

問題 No.827 総神童数
ユーザー ward1302
提出日時 2019-05-03 22:30:10
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 322 ms / 2,000 ms
コード長 1,292 bytes
コンパイル時間 922 ms
コンパイル使用メモリ 82,592 KB
実行使用メモリ 29,856 KB
最終ジャッジ日時 2024-12-31 18:12:21
合計ジャッジ時間 7,585 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <utility>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
using namespace std;

#define ll long long
ll mmod=1000000007;

void dfs(vector<vector<int>> &V, int key, vector<int> &W){
    for(int i=0; i<V[key].size(); ++i){
        if(W[V[key][i]]<0){
            W[V[key][i]]=W[key]+1;
            dfs(V, V[key][i], W);
        }
    }
}
// a^n mod を計算する
ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
ll modinv(ll a, ll mod) {
    return modpow(a, mod - 2, mod);
}


int main() {
	int N; cin >> N;
	vector<vector<int>> v(N+1);
	for(int i=0; i<N-1; ++i){
	    int a, b;
	    cin >> a >> b;
	    v[a].push_back(b);
	    v[b].push_back(a);
	}
	vector<int> dpt(N+1, -1), cnt(N+1, 0);
	dpt[1]=1;
	dfs(v, 1, dpt);
	for(int i=1; i<=N; ++i) ++cnt[dpt[i]];
	ll frac=1;
	for(int i=1; i<=N; ++i) frac=(frac*i)%mmod;
	ll ans=0;
	for(int i=1; i<=N; ++i){
	    ll t=(frac*modinv(i, mmod))%mmod;
	    ans=(ans+(t*cnt[i])%mmod)%mmod;
	}
	cout << ans << endl;
	//for(int i=1; i<=N; ++i) cout << dpt[i] << ' ';
	//cout << endl;
	return 0;
}
0