結果
問題 | No.827 総神童数 |
ユーザー | mamekin |
提出日時 | 2019-05-18 13:09:46 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 235 ms / 2,000 ms |
コード長 | 2,646 bytes |
コンパイル時間 | 1,334 ms |
コンパイル使用メモリ | 124,876 KB |
実行使用メモリ | 33,868 KB |
最終ジャッジ日時 | 2023-10-17 07:37:11 |
合計ジャッジ時間 | 7,341 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,348 KB |
testcase_01 | AC | 2 ms
4,348 KB |
testcase_02 | AC | 2 ms
4,348 KB |
testcase_03 | AC | 2 ms
4,348 KB |
testcase_04 | AC | 2 ms
4,348 KB |
testcase_05 | AC | 2 ms
4,348 KB |
testcase_06 | AC | 2 ms
4,348 KB |
testcase_07 | AC | 2 ms
4,348 KB |
testcase_08 | AC | 2 ms
4,348 KB |
testcase_09 | AC | 219 ms
33,868 KB |
testcase_10 | AC | 71 ms
13,080 KB |
testcase_11 | AC | 4 ms
4,348 KB |
testcase_12 | AC | 29 ms
8,064 KB |
testcase_13 | AC | 171 ms
28,324 KB |
testcase_14 | AC | 7 ms
4,504 KB |
testcase_15 | AC | 66 ms
13,344 KB |
testcase_16 | AC | 174 ms
23,308 KB |
testcase_17 | AC | 206 ms
27,796 KB |
testcase_18 | AC | 85 ms
15,456 KB |
testcase_19 | AC | 235 ms
19,348 KB |
testcase_20 | AC | 167 ms
15,192 KB |
testcase_21 | AC | 117 ms
12,024 KB |
testcase_22 | AC | 183 ms
16,180 KB |
testcase_23 | AC | 3 ms
4,348 KB |
testcase_24 | AC | 205 ms
17,500 KB |
testcase_25 | AC | 151 ms
14,136 KB |
testcase_26 | AC | 207 ms
17,500 KB |
testcase_27 | AC | 131 ms
12,816 KB |
testcase_28 | AC | 128 ms
12,816 KB |
testcase_29 | AC | 98 ms
10,968 KB |
testcase_30 | AC | 26 ms
5,688 KB |
testcase_31 | AC | 76 ms
9,120 KB |
testcase_32 | AC | 69 ms
9,120 KB |
testcase_33 | AC | 232 ms
18,820 KB |
testcase_34 | AC | 208 ms
17,500 KB |
testcase_35 | AC | 76 ms
9,384 KB |
testcase_36 | AC | 112 ms
11,760 KB |
testcase_37 | AC | 150 ms
14,136 KB |
testcase_38 | AC | 227 ms
18,556 KB |
ソースコード
#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <complex> #include <string> #include <vector> #include <array> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> #include <iterator> #include <memory> #include <regex> using namespace std; void mod_inverse(int n, vector<long long>& inv, int mod) { inv.assign(n+1, -1); inv[1] = 1; for(int i=2; i<=n; ++i) inv[i] = inv[mod % i] * (mod - mod / i) % mod; } class FactorialCalculation { private: const int mod; vector<long long> factorial; vector<long long> invFactorial; public: FactorialCalculation(int n, int mod) : mod(mod) { factorial.resize(n+1, 1); invFactorial.resize(n+1, 1); vector<long long> inv; mod_inverse(n, inv, mod); for(int i=1; i<=n; ++i){ factorial[i] = factorial[i-1] * i % mod; invFactorial[i] = invFactorial[i-1] * inv[i] % mod; } } long long getFactorial(int n){ return factorial[n]; } long long getInvFactorial(int n){ return invFactorial[n]; } long long getPermutation(int n, int r){ if(n < r) return 0; return factorial[n] * invFactorial[n-r] % mod; } long long getCombination(int n, int r){ if(n < r) return 0; return getPermutation(n, r) * invFactorial[r] % mod; } long long getHomogeneous(int n, int r){ return getCombination(n+r-1, r); } }; void dfs(const vector<vector<int> >& edges, int curr, int prev, int d, vector<int>& depth) { depth[curr] = d; for(int next : edges[curr]){ if(next != prev) dfs(edges, next, curr, d+1, depth); } } const int MOD = 1000000007; int main() { int n; cin >> n; vector<vector<int> > edges(n); for(int i=0; i<n-1; ++i){ int u, v; cin >> u >> v; -- u; -- v; edges[u].push_back(v); edges[v].push_back(u); } vector<int> depth(n); dfs(edges, 0, -1, 0, depth); FactorialCalculation fc(n, MOD); long long ans = 0; for(int i=0; i<n; ++i){ long long tmp = fc.getCombination(n, depth[i] + 1); tmp *= fc.getFactorial(depth[i]); tmp %= MOD; tmp *= fc.getFactorial(n - depth[i] - 1); tmp %= MOD; ans += tmp; ans %= MOD; } cout << ans << endl; return 0; }