結果

問題 No.827 総神童数
ユーザー lightninglightning
提出日時 2019-05-03 22:23:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 362 ms / 2,000 ms
コード長 4,429 bytes
コンパイル時間 2,141 ms
コンパイル使用メモリ 182,788 KB
実行使用メモリ 21,380 KB
最終ジャッジ日時 2023-08-30 06:11:53
合計ジャッジ時間 9,726 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 343 ms
21,380 KB
testcase_10 AC 116 ms
9,748 KB
testcase_11 AC 7 ms
4,376 KB
testcase_12 AC 48 ms
5,748 KB
testcase_13 AC 269 ms
19,052 KB
testcase_14 AC 11 ms
4,376 KB
testcase_15 AC 103 ms
8,260 KB
testcase_16 AC 279 ms
17,976 KB
testcase_17 AC 325 ms
20,424 KB
testcase_18 AC 138 ms
10,584 KB
testcase_19 AC 362 ms
20,380 KB
testcase_20 AC 257 ms
17,708 KB
testcase_21 AC 181 ms
12,232 KB
testcase_22 AC 288 ms
18,848 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 324 ms
20,196 KB
testcase_25 AC 238 ms
16,200 KB
testcase_26 AC 315 ms
19,528 KB
testcase_27 AC 202 ms
12,940 KB
testcase_28 AC 197 ms
12,756 KB
testcase_29 AC 158 ms
11,376 KB
testcase_30 AC 48 ms
5,652 KB
testcase_31 AC 122 ms
10,180 KB
testcase_32 AC 120 ms
9,812 KB
testcase_33 AC 348 ms
20,572 KB
testcase_34 AC 312 ms
19,120 KB
testcase_35 AC 123 ms
9,972 KB
testcase_36 AC 179 ms
12,020 KB
testcase_37 AC 231 ms
13,892 KB
testcase_38 AC 340 ms
20,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define put(a) cout<<a<<"\n"
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){if(a>b){a=b;return 1;}return 0;}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){if(a<b){a=b;return 1;}return 0;}

template<typename T,typename U>
inline T pow(T x,U exp){
    if(exp<=0){
        return 1;
    }
    if(exp%2==0){
        T d = pow(x,exp/2);
        return d*d;
    }
    else{
        return (x*pow(x,exp-1));
    }
}

template<typename T>
inline T fact(T n,vector<T>& table){
    if(n>=(int)table.size()){
        ll s = table.size();
        for(T i=s;i<n+1;++i){
            table.push_back(table.back()*i);
        }
    }
    if(n<0) return 1;
    else return table[n.a];
}

template<typename T>
inline T comb(T n,T m,vector<T>& table){//nCm
    if(n-m<m)return comb(n,n-m,table);
    else return fact(n,table)/fact(m,table)/fact(n-m,table);
}

class ModInt{
public:
    ull a;
    const int mod = 1e9+7;
    ModInt(ull _val=0){
        if(_val>=mod){_val%=mod;}
        a = _val;
    }
    ModInt operator=(const ModInt n){a=n.a;return a;}
    ModInt operator+(const ModInt n){if((a+n.a)>=mod){return a+n.a-mod;}else{return a+n.a;}}
    ModInt operator-(const ModInt n){return a+(-n.a);}
    ModInt operator*(const ModInt n){return a*n.a;}
    ModInt operator/(const ModInt n){return (*this)*pow(n,mod-2);}
    ModInt &operator+=(const ModInt n){(*this)=(*this)+n;return *this;}
    ModInt &operator-=(const ModInt n){(*this)=(*this)-n;return *this;}
    ModInt &operator*=(const ModInt n){(*this)=(*this)*n;return *this;}
    ModInt &operator/=(const ModInt n){(*this)=(*this)/n;return *this;}
    ModInt &operator++(int){(*this)=(*this)+1;return *this;}//前置インクリメントs(++a)のオーバーロード
    ModInt &operator++(){(*this)=(*this)+1;return *this;}//後置インクリメント(a++)のオーバーロード
    ModInt &operator--(int){(*this)=(*this)-1;return *this;}//前置デクリメント(--a)のオーバーロード
    ModInt &operator--(){(*this)=(*this)-1;return *this;}//後置デクリメント(a--)のオーバーロード
    ModInt operator-(){return mod-a;}//単項-演算子(-a)のオーバーロード
    ModInt inv(){ModInt temp(1);return temp/(*this);}//逆数を返す関数 return (*this)/(*this)/(*this);でもいい
    bool operator<(const ModInt n){return a<n.a;}
    bool operator<=(const ModInt n){return a<=n.a;}
    bool operator>(const ModInt n){return a>n.a;}
    bool operator>=(const ModInt n){return a>=n.a;}
    bool operator==(const ModInt n){return a==n.a;}
    //下の関係演算子はpow関数で要請される
    bool operator<(const int n){return a<n;}
    bool operator<=(const int n){return a<=n;}
    bool operator>(const int n){return a>n;}
    bool operator>=(const int n){return a>=n;}
    bool operator==(const int n){return a==n;}
    ModInt operator%(const int n){return a%n;}
};
ostream& operator <<(ostream &o, const ModInt &t) {
    o << t.a;
    return o;
}
istream& operator >>(istream &i,ModInt &t) {
    i >> t.a;
    return i;
}

int main(){
    int n;
    cin >> n;
    vector<int> u(n);
    vector<int> v(n);
    vector<vector<int>> p(n);
    REP(i,n-1){
        cin >> u[i] >> v[i];
        u[i]--;
        v[i]--;
        p[u[i]].push_back(v[i]);
        p[v[i]].push_back(u[i]);
    }
    
    vector<int> d(n,INT_MAX);
    d[0] = 0;
    queue<int> q;
    q.push(0);
    while(!q.empty()){
        int b = q.front();q.pop();
        REP(i,p[b].size()){
            if(d[p[b][i]]==INT_MAX){
                d[p[b][i]]=d[b]+1;
                q.push(p[b][i]);
            }
        }
    }
    ModInt res(0);
    vector<ModInt> table(1,1);
    ModInt a(n);
    ModInt b;
    res += fact(a, table);
    FOR(i,1,n){
        b = d[i];
        res += a*comb(a-1,b,table)/(b+1)*fact(b,table)*fact(a-b-1,table);
    }
    put(res);
    return 0;
}
0