結果
問題 | No.827 総神童数 |
ユーザー | ahe100 |
提出日時 | 2019-05-04 03:51:02 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 232 ms / 2,000 ms |
コード長 | 2,739 bytes |
コンパイル時間 | 1,785 ms |
コンパイル使用メモリ | 176,192 KB |
実行使用メモリ | 17,684 KB |
最終ジャッジ日時 | 2023-09-01 03:35:46 |
合計ジャッジ時間 | 9,309 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge11 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
9,552 KB |
testcase_01 | AC | 4 ms
9,492 KB |
testcase_02 | AC | 4 ms
9,560 KB |
testcase_03 | AC | 4 ms
9,540 KB |
testcase_04 | AC | 4 ms
9,556 KB |
testcase_05 | AC | 4 ms
9,696 KB |
testcase_06 | AC | 4 ms
9,556 KB |
testcase_07 | AC | 4 ms
9,692 KB |
testcase_08 | AC | 4 ms
9,556 KB |
testcase_09 | AC | 206 ms
16,844 KB |
testcase_10 | AC | 68 ms
11,828 KB |
testcase_11 | AC | 6 ms
9,620 KB |
testcase_12 | AC | 30 ms
10,684 KB |
testcase_13 | AC | 163 ms
15,432 KB |
testcase_14 | AC | 9 ms
9,804 KB |
testcase_15 | AC | 63 ms
11,560 KB |
testcase_16 | AC | 157 ms
15,316 KB |
testcase_17 | AC | 189 ms
16,768 KB |
testcase_18 | AC | 80 ms
12,412 KB |
testcase_19 | AC | 232 ms
17,684 KB |
testcase_20 | AC | 160 ms
15,692 KB |
testcase_21 | AC | 110 ms
13,684 KB |
testcase_22 | AC | 172 ms
16,092 KB |
testcase_23 | AC | 5 ms
9,596 KB |
testcase_24 | AC | 199 ms
16,896 KB |
testcase_25 | AC | 145 ms
15,176 KB |
testcase_26 | AC | 196 ms
16,952 KB |
testcase_27 | AC | 124 ms
14,248 KB |
testcase_28 | AC | 123 ms
14,324 KB |
testcase_29 | AC | 95 ms
13,144 KB |
testcase_30 | AC | 29 ms
10,732 KB |
testcase_31 | AC | 73 ms
12,368 KB |
testcase_32 | AC | 71 ms
12,196 KB |
testcase_33 | AC | 224 ms
17,496 KB |
testcase_34 | AC | 199 ms
16,904 KB |
testcase_35 | AC | 69 ms
12,472 KB |
testcase_36 | AC | 104 ms
13,860 KB |
testcase_37 | AC | 142 ms
15,004 KB |
testcase_38 | AC | 207 ms
17,556 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0;i<((int)(n));i++) #define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++) #define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--) #define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--) typedef long long ll; /* 根は1 直接求めるのは無理なので、期待値に変換する 数aが神童である確率は、上に来るd個がすべて自分より小さい場合 */ template<int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) { } ModInt(signed sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; } ModInt(signed long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { signed a = x, b = MOD, u = 1, v = 0; while (b) { signed t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } if (u < 0) u += Mod; ModInt res; res.x = (unsigned)u; return res; } }; typedef ModInt<1000000007> mint; vector<mint> fact, factinv; void nCr_computeFactinv(int N) { N = min(N, mint::Mod - 1); fact.resize(N + 1); factinv.resize(N + 1); fact[0] = 1; reg(i, 1, N) fact[i] = fact[i - 1] * i; factinv[N] = fact[N].inverse(); for (int i = N; i >= 1; i --) factinv[i - 1] = factinv[i] * i; } mint nCr(int n, int r) { if (n >= mint::Mod) return nCr(n % mint::Mod, r % mint::Mod) * nCr(n / mint::Mod, r / mint::Mod); return r > n ? 0 : fact[n] * factinv[n - r] * factinv[r]; } mint nHr(int n, int r) { return r == 0 ? 1 : nCr(n + r - 1, r); } mint ans=0; ll n,d[200010]; vector<ll> v[200010]; void init(){ cin>>n; rep(i,n-1){ ll a,b; cin>>a>>b; v[a].push_back(b); v[b].push_back(a); } reg(i,1,n)d[i]=1e18; queue<ll> Q; Q.push(1); d[1]=1; while(!Q.empty()){ ll p = Q.front();Q.pop(); for(ll q:v[p]){ if(d[q]>d[p]+1){ d[q]=d[p]+1; Q.push(q); } } } nCr_computeFactinv(n); } int main(void){ init(); mint sum=0; reg(i,1,n){ mint dinv = 1; dinv/=d[i]; ans+=dinv; } ans*=fact[n]; cout<<ans.get()<<endl; return 0; }