結果

問題 No.827 総神童数
ユーザー NyaanNyaanNyaanNyaan
提出日時 2019-05-03 22:38:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 6,594 bytes
コンパイル時間 1,127 ms
コンパイル使用メモリ 100,456 KB
実行使用メモリ 16,284 KB
最終ジャッジ日時 2023-08-30 06:15:38
合計ジャッジ時間 7,822 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 248 ms
16,284 KB
testcase_10 AC 81 ms
7,584 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 34 ms
5,196 KB
testcase_13 AC 198 ms
13,488 KB
testcase_14 AC 9 ms
4,376 KB
testcase_15 AC 74 ms
7,220 KB
testcase_16 AC 193 ms
13,644 KB
testcase_17 AC 233 ms
15,500 KB
testcase_18 AC 97 ms
8,592 KB
testcase_19 AC 279 ms
16,028 KB
testcase_20 AC 195 ms
12,468 KB
testcase_21 AC 135 ms
9,964 KB
testcase_22 AC 218 ms
13,344 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 239 ms
14,548 KB
testcase_25 AC 174 ms
11,636 KB
testcase_26 AC 241 ms
14,844 KB
testcase_27 AC 150 ms
10,696 KB
testcase_28 AC 148 ms
10,752 KB
testcase_29 AC 116 ms
9,212 KB
testcase_30 AC 32 ms
4,952 KB
testcase_31 AC 87 ms
7,852 KB
testcase_32 AC 87 ms
7,492 KB
testcase_33 AC 270 ms
15,644 KB
testcase_34 AC 241 ms
14,636 KB
testcase_35 AC 90 ms
7,912 KB
testcase_36 AC 129 ms
9,908 KB
testcase_37 AC 182 ms
11,640 KB
testcase_38 AC 262 ms
15,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define whlie while
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 1001001001
#define mod 1000000007
#define FOR(i,a,b) for(int (i)=((int)a); (i)<((int)b); (i)++) // [a,b)
#define rep(i,N) FOR((i), 0, ((int)N)) // [0,N)
#define FORR(i,a,b) for(int (i)=((int)b) - 1; (i)>=((int)a); (i)--)
#define repr(i,N) FORR((i), 0, ((int)N))
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
#define vrep(v,it) for(auto it=v.begin();it<v.end();it++)
#define vrepr(v,it) for(auto it=v.rbegin();it<v.rend();it++)
#define inx(t,...) t __VA_ARGS__; in(__VA_ARGS__)
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) double __VA_ARGS__; in(__VA_ARGS__)
#define inpii(...) pii __VA_ARGS__; in(__VA_ARGS__)
#define invi(v,...) vi v; in(v,##__VA_ARGS__)
#define invl(v,...) vl v; in(v,##__VA_ARGS__)

// デバッグ用
// dbg(...) printfと同様に使用可能 
// trc(var) (変数名)=(値)と表示
// stopif(val) 条件分が真の時に止まる
// vd(v,N) vの中身を表示

#ifdef LOCAL
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
    #define trc(...) cout << #__VA_ARGS__ << "="; dbg_out(__VA_ARGS__)
    #define stopif(val) assert( !(val) )
    #define vdbg(v,...) cout << #v << "={"; vector_debug(v , ##__VA_ARGS__)
#else
    #define dbg(...) 1
    #define trc(...) 1
    #define stopif(...) 1
    #define vdbg(...) 1
#endif

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector< vector<int> > vvi;
int gcd(int a, int b){if(a>b) swap(a,b); return a==0 ? b : gcd(b%a,a);} ll gcd(ll a, ll b){if(a>b) swap(a,b); return a==0 ? b : gcd(b%a,a);}
int lcm(int a, int b){return (a / gcd(a,b)) * b;} ll lcm(ll a, ll b){return (a / gcd(a,b)) * b;}
template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename C> inline void _cin(C &c){cin >> c;} template<typename T,typename U> inline void _cin(pair<T,U> &p){cin >> p.fi; cin >> p.se;}
template<typename C> inline void _cout(const C &c){cout << c;} template<typename T,typename U> inline void _cout(const pair<T,U> &p){cout << p.fi << ' ' << p.se;}
void in(){} template <typename T,class... U> void in(T &t,U &...u){ _cin(t); in(u...);}
void out(){cout << endl;} template <typename T,class... U> void out(const T &t,U ...u){ _cout(t); if(sizeof...(u)) cout << ' '; out(u...);}
void dbg_out(){cout << endl;} template <typename T,class... U> void dbg_out(const T &t,U ...u){ _cout(t); if(sizeof...(u)) cout << ','; out(u...);}
template<typename C> inline void in(vector<C> &v,int N=-1){if(sz(v) != 0){int M=(N == -1) ? sz(v) : N; rep(i,M) _cin(v[i]);}else{C c;rep(i,N) v.pb((_cin(c),c));}} template<typename C> inline void in(C v[],int N){rep(i,N) _cin(v[i]);}
template<typename C> inline void out(const vector<C> &v,int N=-1){int M=(N == -1) ? sz(v) : N; rep(i,M) {cout<<( (i)?" ":"" ); _cout(v[i]);} cout<<endl; } template<typename C> inline void out(C v[],int N){rep(i,N) {cout<<( (i)?" ":"" ); _cout(v[i]);} cout<<endl; }
template<typename C> inline void vector_debug(const vector<C> &v,int N=-1){int M=(N == -1) ? sz(v) : N; rep(i,M) {cout<<( (i)?", ":"" ); _cout(v[i]);} cout<<"}"<<endl; } template<typename C> inline void vector_debug(C v[], int N){rep(i,N) {cout<<((i)?", ":""); _cout(*(v+i));} cout<<"}"<<endl;}
template<typename C> inline C vmax(const vector<C> &v){C n=v[0]; rep(i,sz(v)) amax(n,v[i]); return n;} template<typename C> inline C vmax(C v[], int N){C n=v[0]; rep( i , N ) amax(n,v[i]); return n;}
template<typename C> inline C vmin(const vector<C> &v){C n=v[0]; rep(i,sz(v)) amin(n,v[i]); return n;} template<typename C> inline C vmin(C v[], int N){C n=v[0]; rep( i , N ) amin(n,v[i]); return n;}
template<typename C> inline C vsum(const vector<C> &v){C n=0; rep(i,sz(v)) n+=v[i]; return n;} template<typename C> inline C vsum(C v[], int N){C n=0; rep( i , N ) n+=v[i]; return n;}
const int dx[]={1,0,-1,0}; const int dy[]={0,1,0,-1};
//const int dx[]={1,1,1,0,-1,-1,-1,0}; const int dy[]={1,0,-1,-1,-1,0,1,1};

// ini(A,B,C) => int A,B,C; cin >> A >> B >> C; 
// in(A,B,C) => cin >> A >> B >> C;   invi(v,N) =>vi v; rep(i,N) v.pb( (int x,cin >> x,x) );
// out(A,B,C) cout << A << ' ' <<  B << ' ' << C;
// in,outともにベクトル・配列・pairに使用可能(ただしベクトル・配列は1つしか入れられない)
// amax(x,y) bool関数 x<yの時にxにyを代入してtrueを返す

// mod
inline ll madd(ll a, ll b){return (a + b) % mod;}
inline ll msub(ll a, ll b){return (a + mod - b) % mod;}
inline ll mmul(ll a, ll b){return ((a % mod) * (b % mod)) % mod;}
ll mpow(ll a, ll b){if(b == 0) return 1; else if(b == 1) return a % mod; else if(b % 2 == 0){ll hlf = mpow(a,b/2);return (hlf * hlf) % mod;} else {ll hlf = mpow(a,b/2);return ( mmul(hlf , hlf) * a ) % mod;}}
inline ll mdiv(ll a, ll b){return mmul(a, mpow(b, mod-2));}

ll fct[200010];
void f(int M){
    rep(i,M+1){
        if(i == 0) fct[i] = 1;
        else fct[i] = mmul(fct[i-1],i);
    }
}

constexpr int an = 200010;
int N;

int A[an]; // 深さを記入する配列
void bfs(int src, vvi &v){
    rep(i,N+1) A[i] = -1; // Aの初期化
    queue<int> Q;
    Q.push(src);
    A[src] = 1; // A[スタート地点]の深さ = 0
    while(! Q.empty()){
        int cur = Q.front();
        trc(cur);
        Q.pop();
        for(int x : v[cur]){ // 探索範囲
            if(A[x] == -1){ // 条件
                int dst = x;
                A[dst]=A[cur]+1; // dstの深さはA[cur]+1
                Q.push(dst); // Qにdstを追加
            }
        }
    }
}

int main(){
    in(N);
    f(N);
    vdbg(fct,N+1);
    vvi v(N+1);
    rep(i,N-1){
        ini(u,V);
        v[u].pb(V); v[V].pb(u);
    }
    vdbg(v[1]); vdbg(v[2]);
    bfs(1,v);
    ll ans = 0;
    for(int i=1; i<=N; i++){
        ans = madd( ans, mdiv(fct[N] , A[i]) );
    }
    out(ans);
}
0