結果

問題 No.827 総神童数
ユーザー okok
提出日時 2019-05-03 23:52:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,909 bytes
コンパイル時間 691 ms
コンパイル使用メモリ 75,132 KB
実行使用メモリ 26,620 KB
最終ジャッジ日時 2023-08-30 07:03:19
合計ジャッジ時間 4,956 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
12,940 KB
testcase_01 AC 8 ms
12,784 KB
testcase_02 AC 8 ms
12,912 KB
testcase_03 AC 8 ms
12,796 KB
testcase_04 AC 8 ms
12,796 KB
testcase_05 AC 8 ms
12,976 KB
testcase_06 AC 9 ms
12,872 KB
testcase_07 AC 8 ms
12,808 KB
testcase_08 AC 8 ms
12,972 KB
testcase_09 AC 113 ms
26,620 KB
testcase_10 AC 38 ms
17,144 KB
testcase_11 AC 9 ms
13,020 KB
testcase_12 AC 20 ms
14,960 KB
testcase_13 AC 89 ms
23,840 KB
testcase_14 AC 11 ms
13,364 KB
testcase_15 AC 37 ms
17,192 KB
testcase_16 AC 87 ms
21,884 KB
testcase_17 AC 106 ms
23,812 KB
testcase_18 AC 46 ms
18,228 KB
testcase_19 AC 140 ms
21,392 KB
testcase_20 AC 93 ms
19,232 KB
testcase_21 AC 64 ms
17,424 KB
testcase_22 AC 106 ms
19,692 KB
testcase_23 AC 9 ms
12,868 KB
testcase_24 AC 117 ms
20,548 KB
testcase_25 AC 80 ms
18,672 KB
testcase_26 AC 120 ms
20,476 KB
testcase_27 AC 72 ms
17,980 KB
testcase_28 AC 72 ms
18,056 KB
testcase_29 AC 53 ms
16,852 KB
testcase_30 AC 21 ms
14,100 KB
testcase_31 AC 43 ms
16,180 KB
testcase_32 AC 39 ms
15,828 KB
testcase_33 AC 136 ms
21,400 KB
testcase_34 AC 117 ms
20,660 KB
testcase_35 AC 47 ms
16,064 KB
testcase_36 AC 64 ms
17,384 KB
testcase_37 AC 83 ms
18,588 KB
testcase_38 AC 132 ms
21,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 210000


long long fac[MAX], mmi[MAX];
int used[MAX], temp[MAX];

int N;
const int MAX_VAL = MAX;

vector<int> G[MAX];
void factorial_mod(){
	 fac[0]=fac[1]=1;
	for(long long i = 2; i < MAX_VAL; fac[i]%=MOD,i++)
		fac[i] = fac[i-1]*(i%MOD);
}

long long power_mod(long long x, long long n){
	long long ans = 1;
	for(;n;n>>=1,x*=x,ans%=MOD,x%=MOD)
		if(n&1)ans*=x;
	return ans%MOD;
}

void exgcd(int a, int b, int &x, int &y){
	if(b == 0){
		x = 1;
		y = 0;
		return ;
	}
	exgcd(b,a%b,y,x);
	y -= a/b * x;
}

void modular_multiplicatibe_inverse(){
	int x, y;  
	exgcd(fac[MAX_VAL-1],MOD,x,y);
	mmi[MAX_VAL-1] = x; 
	for(long long i = MAX_VAL-2; i >= 0; mmi[i]%=MOD,i--)
		mmi[i] = mmi[i+1]*((i+1)%MOD);
}

long long combination(long long n, long long r){
	return fac[n]*(mmi[r]*mmi[n-r]%MOD)%MOD;
}

void dfs(int depth = 0, int num = 1){
	if(used[num]) return;
	used[num] = true;
	
	temp[depth]++;
	
	for(int i = 0; i < G[num].size(); i++){
		if(used[G[num][i]]) continue;
		dfs(depth+1, G[num][i]);
	}
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int u, v;
	int ans = 0;
	
	cin>>N;
	
	factorial_mod();
	modular_multiplicatibe_inverse();
	
	for(int i = 0; i < N-1; i++){
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	
	dfs();
	
	for(int i = 0; i < N; i++){
		// cout<<i<<" "<<temp[i]<<" "<<ans<<endl;
		if(!temp[i]) break;
		ans += temp[i]*combination(N,i+1)%MOD*fac[i]%MOD*fac[N-i-1]%MOD;
		ans %= MOD;
		// cout<<combination(N,i+1)*fac[i]%MOD*fac[N-i-1]<<endl;;
	}
	
	cout<<ans<<endl;

	return 0;
}
0