結果

問題 No.827 総神童数
ユーザー ttttan2ttttan2
提出日時 2019-05-07 17:24:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 347 ms / 2,000 ms
コード長 2,159 bytes
コンパイル時間 1,400 ms
コンパイル使用メモリ 151,268 KB
実行使用メモリ 17,316 KB
最終ジャッジ日時 2023-09-14 16:27:43
合計ジャッジ時間 9,079 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 304 ms
17,316 KB
testcase_10 AC 104 ms
8,364 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 45 ms
5,504 KB
testcase_13 AC 241 ms
14,552 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 93 ms
7,952 KB
testcase_16 AC 247 ms
14,976 KB
testcase_17 AC 295 ms
16,772 KB
testcase_18 AC 122 ms
9,196 KB
testcase_19 AC 347 ms
17,244 KB
testcase_20 AC 247 ms
13,728 KB
testcase_21 AC 176 ms
10,844 KB
testcase_22 AC 274 ms
14,672 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 307 ms
15,760 KB
testcase_25 AC 230 ms
12,832 KB
testcase_26 AC 311 ms
15,808 KB
testcase_27 AC 200 ms
11,704 KB
testcase_28 AC 194 ms
11,588 KB
testcase_29 AC 151 ms
10,028 KB
testcase_30 AC 43 ms
5,668 KB
testcase_31 AC 117 ms
8,544 KB
testcase_32 AC 113 ms
8,416 KB
testcase_33 AC 341 ms
16,936 KB
testcase_34 AC 307 ms
15,740 KB
testcase_35 AC 116 ms
8,740 KB
testcase_36 AC 171 ms
10,700 KB
testcase_37 AC 222 ms
12,756 KB
testcase_38 AC 330 ms
16,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0