結果

問題 No.827 総神童数
ユーザー ward1302ward1302
提出日時 2019-05-03 22:30:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 317 ms / 2,000 ms
コード長 1,292 bytes
コンパイル時間 726 ms
コンパイル使用メモリ 82,452 KB
実行使用メモリ 29,824 KB
最終ジャッジ日時 2023-08-30 06:13:07
合計ジャッジ時間 7,669 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 297 ms
29,824 KB
testcase_10 AC 99 ms
11,244 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 43 ms
7,212 KB
testcase_13 AC 237 ms
25,152 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 92 ms
11,708 KB
testcase_16 AC 235 ms
19,816 KB
testcase_17 AC 285 ms
23,960 KB
testcase_18 AC 120 ms
13,576 KB
testcase_19 AC 314 ms
15,508 KB
testcase_20 AC 229 ms
11,924 KB
testcase_21 AC 161 ms
9,632 KB
testcase_22 AC 250 ms
13,288 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 280 ms
13,808 KB
testcase_25 AC 204 ms
11,544 KB
testcase_26 AC 282 ms
14,068 KB
testcase_27 AC 175 ms
10,388 KB
testcase_28 AC 178 ms
10,484 KB
testcase_29 AC 140 ms
8,860 KB
testcase_30 AC 40 ms
5,024 KB
testcase_31 AC 108 ms
7,520 KB
testcase_32 AC 102 ms
7,328 KB
testcase_33 AC 317 ms
14,920 KB
testcase_34 AC 284 ms
13,884 KB
testcase_35 AC 108 ms
7,596 KB
testcase_36 AC 152 ms
9,688 KB
testcase_37 AC 207 ms
11,288 KB
testcase_38 AC 303 ms
14,604 KB
権限があれば一括ダウンロードができます

ソースコード

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