結果
問題 |
No.827 総神童数
|
ユーザー |
![]() |
提出日時 | 2019-05-07 17:24:05 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 373 ms / 2,000 ms |
コード長 | 2,159 bytes |
コンパイル時間 | 1,573 ms |
コンパイル使用メモリ | 165,840 KB |
実行使用メモリ | 17,408 KB |
最終ジャッジ日時 | 2024-07-01 23:34:54 |
合計ジャッジ時間 | 9,513 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<pii,int> ppii; typedef pair<int,pii> pipi; typedef pair<ll,ll> pll; typedef pair<ll,pll> plpl; typedef tuple<ll,ll,ll> tl; ll mod=1000000007; ll inf=1000000000000000000; #define rep(i,m,n) for(int i=m;i<n;i++) #define rrep(i,n,m) for(int i=n;i>=m;i--) ll lmax(ll a,ll b){ if(a<b)return b; else return a; } ll lmin(ll a,ll b){ if(a<b)return a; else return b; } int dh[4]={-1,1,0,0}; int dw[4]={0,0,1,-1}; ll gya[200010]; ll kai[200010]; ll beki(ll n,ll k){ ll ret=1; ll now=n; while(k>0){ if(k%2==1){ ret*=now; ret%=mod; } now*=now; now%=mod; k/=2; } return ret; } ll gyaku(ll n){ return beki(n,mod-2); } void nckinit(ll n){ gya[0]=1; gya[1]=1; for(int i=2;i<=n;i++){ gya[i]=gya[i-1]*gyaku(i); gya[i]%=mod; } kai[0]=1; kai[1]=1; for(int i=2;i<=n;i++){ kai[i]=kai[i-1]*i; kai[i]%=mod; } } ll nck(ll n,ll k){ if(k<0)return 0; if(k==0||n==k)return 1; ll ret=kai[n]; ret*=gya[n-k]; ret%=mod; ret*=gya[k]; ret%=mod; return ret; } ll npk(ll n,ll k){ if(k<0)return 0; if(k==0)return 1; ll ret=kai[n]; ret*=gya[n-k]; ret%=mod; return ret; } int main(){ int n;cin>>n; vector<int> v[n+1]; rep(i,0,n-1){ int a,b;cin>>a>>b; v[a].push_back(b); v[b].push_back(a); } queue<int> q; q.push(1); queue<int> d; d.push(1); bool used[n+1];fill(used,used+n+1,false); used[1]=true; ll ans=0; nckinit(n+1); while(q.size()>0){ int now=q.front(); ll dis=d.front(); q.pop();d.pop(); ll sum=nck(n,dis)*kai[n-dis]; sum%=mod; sum*=kai[dis-1]; sum%=mod; ans+=sum; //cout<<sum<<" "; ans%=mod; rep(i,0,v[now].size()){ int ne=v[now][i]; if(used[ne])continue; used[ne]=true; q.push(ne); d.push(dis+1); } } cout<<ans<<endl; }