結果

問題 No.827 総神童数
ユーザー okok
提出日時 2019-05-03 23:30:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,914 bytes
コンパイル時間 621 ms
コンパイル使用メモリ 74,776 KB
実行使用メモリ 25,056 KB
最終ジャッジ日時 2023-08-30 06:41:07
合計ジャッジ時間 5,219 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
13,192 KB
testcase_01 AC 9 ms
13,228 KB
testcase_02 AC 9 ms
13,240 KB
testcase_03 AC 10 ms
13,216 KB
testcase_04 AC 9 ms
13,236 KB
testcase_05 AC 9 ms
13,236 KB
testcase_06 AC 9 ms
13,224 KB
testcase_07 AC 9 ms
13,304 KB
testcase_08 AC 9 ms
13,176 KB
testcase_09 AC 121 ms
25,056 KB
testcase_10 AC 42 ms
17,108 KB
testcase_11 AC 10 ms
13,476 KB
testcase_12 AC 22 ms
15,044 KB
testcase_13 AC 97 ms
23,256 KB
testcase_14 AC 12 ms
13,632 KB
testcase_15 AC 40 ms
17,060 KB
testcase_16 AC 99 ms
20,964 KB
testcase_17 AC 116 ms
22,916 KB
testcase_18 AC 51 ms
17,984 KB
testcase_19 AC 161 ms
20,336 KB
testcase_20 AC 118 ms
18,416 KB
testcase_21 AC 85 ms
17,100 KB
testcase_22 AC 126 ms
18,852 KB
testcase_23 AC 10 ms
13,224 KB
testcase_24 AC 141 ms
19,512 KB
testcase_25 AC 102 ms
18,016 KB
testcase_26 AC 141 ms
19,552 KB
testcase_27 AC 88 ms
17,460 KB
testcase_28 AC 90 ms
17,392 KB
testcase_29 AC 68 ms
16,572 KB
testcase_30 AC 22 ms
14,240 KB
testcase_31 AC 55 ms
15,804 KB
testcase_32 AC 49 ms
15,724 KB
testcase_33 AC 157 ms
20,152 KB
testcase_34 AC 139 ms
19,508 KB
testcase_35 AC 51 ms
15,940 KB
testcase_36 AC 76 ms
16,904 KB
testcase_37 AC 102 ms
17,968 KB
testcase_38 AC 154 ms
20,032 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

#define MAX_VAL MAX

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

int ans, N;
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 permutation(long long n, long long r){
	if(n < r) return 0;
	return fac[n]*mmi[n-r]%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;
	
	ans += combination(N,depth+1)*fac[depth]%MOD*fac[N-depth-1]%MOD;
	
	// cout<<depth<<" "<<combination(N,depth+1)*fac[depth]<<endl;
	ans %= MOD;
	
	for(int i = 0; i < G[num].size(); i++){
		if(used[G[num][i]]) continue;
		// cout<<num<<" i = "<<i<<endl;
		dfs(depth+1, G[num][i]);
	}
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int u, v;
	
	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();
	
	cout<<ans<<endl;

	return 0;
}
0