题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Suppose the function $\text{randint}(l,r)$ returns an integer independently and uniformly at random from the range $[l, r]$.
Bessie generates a random labeled tree on $N$ vertices ($2 \le N \le 2\cdot10^5$) using the following two-step process:
- Start with vertices labeled $1$ through $N$. For each $i$ from $2$ to $N$, add an edge between vertex $i$ and $\text{randint}(1, i-1)$.
- Choose a permutation $p_1,p_2,\dots,p_N$ of $\{1, 2, \ldots, N\}$ uniformly at random. Relabel every vertex $v$ as $p_v$.
Now, Farmer John is looking at the edge set of the final tree and wants to know the probability that the two-step process above produces a tree with exactly this edge set. Can you determine this probability modulo $10^9+7$?
INPUT FORMAT (input arrives from the terminal / stdin):
The input consists of $T$ ($1\le T\le 10$) independent inputs. Each input is specified as follows:The first line contains $N$.
The next $N-1$ lines contain the edges of the tree specified by two space-separated integers $u$ and $v$ ($1\le u, v\le N$). It is guaranteed that these edges induce a tree.
It is guaranteed that the sum of $N$ across all tests does not exceed $5\cdot 10^5$.
OUTPUT FORMAT (print output to the terminal / stdout):
For each test, output the probability modulo $10^9+7$ on a new line (note that the output probability is a ratio of integers, so you will want to print the result of this division when working modulo $10^9+7$).SAMPLE INPUT:
4 2 2 1 3 1 2 2 3 4 1 2 2 3 2 4 4 1 2 2 3 3 4
SAMPLE OUTPUT:
1 333333336 83333334 55555556
The probabilities are $1$, $1/3$, $1/12$, $1/18$.
First test: There is only one tree on $N=2$ vertices, so the probability of generating it is just $1$.
Second test: there are three trees on $N=3$ vertices, and each of them is equally likely to have been generated by the process above. And $1/3\equiv 333333336\pmod{10^9+7}$.
SCORING:
- Input 2-3: $N\le 8$
- Inputs 4-9: $N\le 2000$
- Inputs 10-21: No additional constraints.
Problem credits: Benjamin Qi
结果
Kimi K3.1 ProKimi
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 3.6k平均输出 token(含思考) ↓
- 27.9输出速度 tokens/s ↑
- 117 秒延迟 ↓
- 0.88×CPU 时间 / 官方解 ↓
- 不确定 2、同阶 1时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 11.5k平均输出 token(含思考) ↓
- 212.2输出速度 tokens/s ↑
- 53 秒延迟 ↓
- 0.86×CPU 时间 / 官方解 ↓
- 不确定 3时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 13.5k平均输出 token(含思考) ↓
- 178.4输出速度 tokens/s ↑
- 70 秒延迟 ↓
- 0.87×CPU 时间 / 官方解 ↓
- 同阶 2、不确定 1时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; static const int MOD = 1000000007; static const int MAXN = 200000; long long modpow(long long a, long long e) { long long r = 1; while (e) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); vector<long long> inv(MAXN + 1), fact(MAXN + 1), invfact(MAXN + 1); inv[1] = 1; for (int i = 2; i <= MAXN; i++) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; } fact[0] = 1; for (int i = 1; i <= MAXN; i++) { fact[i] = fact[i - 1] * i % MOD; } invfact[MAXN] = modpow(fact[MAXN], MOD - 2); for (int i = MAXN; i >= 1; i--) { invfact[i - 1] = invfact[i] * i % MOD; } int T; cin >> T; while (T--) { int N; cin >> N; vector<vector<int>> adj(N); for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u); } vector<int> parent(N, -1), order; order.reserve(N); parent[0] = 0; order.push_back(0); for (int i = 0; i < (int)order.size(); i++) { int u = order[i]; for (int v : adj[u]) { if (v == parent[u]) continue; parent[v] = u; order.push_back(v); } } vector<int> sub(N, 1); for (int i = N - 1; i >= 1; i--) { int u = order[i]; sub[parent[u]] += sub[u]; } // invProd[r] is the inverse of the product of subtree sizes // when the tree is rooted at r. vector<long long> invProd(N); invProd[0] = 1; for (int u = 0; u < N; u++) { invProd[0] = invProd[0] * inv[sub[u]] % MOD; } long long sum = invProd[0]; for (int i = 0; i < N; i++) { int u = order[i]; for (int v : adj[u]) { if (v == parent[u]) continue; int s = sub[v]; invProd[v] = invProd[u] * s % MOD * inv[N - s] % MOD; sum += invProd[v]; if (sum >= MOD) sum -= MOD; } } cout << sum * invfact[N - 1] % MOD << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; static const int MOD = 1000000007; static const int MAXN = 200000; long long modpow(long long a, long long e) { long long r = 1; while (e) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); vector<long long> inv(MAXN + 1), fact(MAXN + 1), invfact(MAXN + 1); inv[1] = 1; for (int i = 2; i <= MAXN; i++) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; } fact[0] = 1; for (int i = 1; i <= MAXN; i++) { fact[i] = fact[i - 1] * i % MOD; } invfact[MAXN] = modpow(fact[MAXN], MOD - 2); for (int i = MAXN; i >= 1; i--) { invfact[i - 1] = invfact[i] * i % MOD; } int T; cin >> T; while (T--) { int N; cin >> N; vector<vector<int>> adj(N + 1); for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } vector<int> parent(N + 1, 0), order; order.reserve(N); parent[1] = -1; vector<int> st = {1}; while (!st.empty()) { int u = st.back(); st.pop_back(); order.push_back(u); for (int v : adj[u]) { if (v == parent[u]) continue; parent[v] = u; st.push_back(v); } } vector<int> sz(N + 1, 1); for (int i = N - 1; i >= 1; i--) { int u = order[i]; sz[parent[u]] += sz[u]; } vector<long long> f(N + 1); f[1] = 1; for (int u = 1; u <= N; u++) { f[1] = f[1] * inv[sz[u]] % MOD; } long long sum = f[1]; for (int i = 1; i < N; i++) { int u = order[i]; int p = parent[u]; f[u] = f[p] * sz[u] % MOD * inv[N - sz[u]] % MOD; sum = (sum + f[u]) % MOD; } cout << sum * invfact[N - 1] % MOD << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; long long modpow(long long a, long long e) { long long r = 1; while (e > 0) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } struct TestCase { int n; vector<pair<int, int>> edges; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; vector<TestCase> tests(T); int maxN = 2; for (int tc = 0; tc < T; ++tc) { cin >> tests[tc].n; maxN = max(maxN, tests[tc].n); tests[tc].edges.resize(tests[tc].n - 1); for (auto &e : tests[tc].edges) { cin >> e.first >> e.second; } } vector<long long> inv(maxN + 1), fact(maxN + 1), invfact(maxN + 1); inv[1] = 1; for (int i = 2; i <= maxN; ++i) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; } fact[0] = 1; for (int i = 1; i <= maxN; ++i) { fact[i] = fact[i - 1] * i % MOD; } invfact[maxN] = modpow(fact[maxN], MOD - 2); for (int i = maxN; i >= 1; --i) { invfact[i - 1] = invfact[i] * i % MOD; } for (const auto &test : tests) { int n = test.n; vector<vector<int>> adj(n + 1); for (auto [u, v] : test.edges) { adj[u].push_back(v); adj[v].push_back(u); } vector<int> parent(n + 1, 0), sz(n + 1, 1), order; order.reserve(n); parent[1] = -1; order.push_back(1); for (int i = 0; i < n; ++i) { int v = order[i]; for (int to : adj[v]) { if (to == parent[v]) continue; parent[to] = v; order.push_back(to); } } // For a fixed root r, let P_r be the product of all non-root // subtree sizes. First compute the modular inverse of P_1. long long invP = 1; for (int i = n - 1; i >= 0; --i) { int v = order[i]; if (v != 1) { invP = invP * inv[sz[v]] % MOD; sz[parent[v]] += sz[v]; } } vector<long long> value(n + 1); value[1] = invP; long long sum = invP; // Moving the root across an edge with child subtree size s changes // inv(P) by a factor of s / (n - s). for (int v : order) { for (int to : adj[v]) { if (parent[to] != v) continue; value[to] = value[v] * sz[to] % MOD * inv[n - sz[to]] % MOD; sum = (sum + value[to]) % MOD; } } cout << sum * invfact[n] % MOD << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; long long modpow(long long a, long long e) { long long r = 1; while (e > 0) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); const int MAXN = 500005; vector<long long> inv(MAXN), fact(MAXN), invfact(MAXN); inv[1] = 1; for (int i = 2; i < MAXN; i++) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; } fact[0] = 1; for (int i = 1; i < MAXN; i++) { fact[i] = fact[i - 1] * i % MOD; } invfact[MAXN - 1] = modpow(fact[MAXN - 1], MOD - 2); for (int i = MAXN - 2; i >= 0; i--) { invfact[i] = invfact[i + 1] * (i + 1) % MOD; } int T; cin >> T; while (T--) { int N; cin >> N; vector<vector<int>> adj(N); for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u); } vector<int> parent(N, -1); vector<int> order; order.reserve(N); parent[0] = -2; order.push_back(0); for (int i = 0; i < (int)order.size(); i++) { int u = order[i]; for (int v : adj[u]) { if (v == parent[u]) continue; parent[v] = u; order.push_back(v); } } vector<int> sub(N, 1); for (int i = N - 1; i > 0; i--) { int u = order[i]; sub[parent[u]] += sub[u]; } long long P0 = 1; for (int i = 1; i < N; i++) { int u = order[i]; P0 = P0 * sub[u] % MOD; } vector<long long> invP(N); invP[0] = modpow(P0, MOD - 2); long long sumInvP = invP[0]; for (int i = 1; i < N; i++) { int u = order[i]; int p = parent[u]; invP[u] = invP[p] * sub[u] % MOD * inv[N - sub[u]] % MOD; sumInvP += invP[u]; if (sumInvP >= MOD) sumInvP -= MOD; } long long answer = sumInvP * invfact[N] % MOD; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; static const int MAXN = 500005; long long modpow(long long a, long long e) { long long r = 1; while (e > 0) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); vector<long long> inv(MAXN), fact(MAXN), invfact(MAXN); inv[1] = 1; for (int i = 2; i < MAXN; ++i) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; } fact[0] = 1; for (int i = 1; i < MAXN; ++i) { fact[i] = fact[i - 1] * i % MOD; } invfact[MAXN - 1] = modpow(fact[MAXN - 1], MOD - 2); for (int i = MAXN - 2; i >= 0; --i) { invfact[i] = invfact[i + 1] * (i + 1) % MOD; } int T; cin >> T; while (T--) { int N; cin >> N; vector<vector<int>> adj(N + 1); for (int i = 0; i < N - 1; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } vector<int> parent(N + 1, 0); vector<int> order; order.reserve(N); parent[1] = -1; order.push_back(1); for (size_t i = 0; i < order.size(); ++i) { int u = order[i]; for (int v : adj[u]) { if (v == parent[u]) continue; parent[v] = u; order.push_back(v); } } vector<int> sub(N + 1, 1); for (int i = N - 1; i >= 1; --i) { int u = order[i]; sub[parent[u]] += sub[u]; } vector<long long> H(N + 1); long long h1 = 1; for (int u = 1; u <= N; ++u) { h1 = h1 * inv[sub[u]] % MOD; } H[1] = h1; long long sumH = h1; for (int i = 1; i < N; ++i) { int u = order[i]; int p = parent[u]; int s = sub[u]; H[u] = H[p] * s % MOD * inv[N - s] % MOD; sumH += H[u]; if (sumH >= MOD) sumH -= MOD; } long long ans = sumH * invfact[N - 1] % MOD; cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; long long mod_pow(long long a, long long e) { long long r = 1; while (e > 0) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); const int MAXN = 500000 + 5; vector<long long> inv(MAXN), fact(MAXN), invfact(MAXN); inv[1] = 1; for (int i = 2; i < MAXN; i++) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; } fact[0] = 1; for (int i = 1; i < MAXN; i++) { fact[i] = fact[i - 1] * i % MOD; } invfact[MAXN - 1] = mod_pow(fact[MAXN - 1], MOD - 2); for (int i = MAXN - 1; i >= 1; i--) { invfact[i - 1] = invfact[i] * i % MOD; } int T; cin >> T; while (T--) { int n; cin >> n; vector<vector<int>> adj(n + 1); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } vector<int> parent(n + 1, 0); vector<int> order; order.reserve(n); vector<int> st; st.reserve(n); st.push_back(1); parent[1] = 0; while (!st.empty()) { int v = st.back(); st.pop_back(); order.push_back(v); for (int to : adj[v]) { if (to == parent[v]) continue; parent[to] = v; st.push_back(to); } } vector<int> sub(n + 1, 1); for (int i = n - 1; i >= 0; i--) { int v = order[i]; if (parent[v] != 0) { sub[parent[v]] += sub[v]; } } long long F1 = 1; for (int v = 2; v <= n; v++) { F1 = F1 * sub[v] % MOD; } long long invF1 = mod_pow(F1, MOD - 2); vector<long long> invF(n + 1); invF[1] = invF1; long long sum = invF1; for (int i = 1; i < n; i++) { int v = order[i]; int p = parent[v]; invF[v] = invF[p] * sub[v] % MOD * inv[n - sub[v]] % MOD; sum += invF[v]; if (sum >= MOD) sum -= MOD; } long long ans = sum * invfact[n] % MOD; cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int64 MOD = 1'000'000'007; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<vector<int>> adj(N); for (int i = 0; i < N - 1; ++i) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u); } // Root the tree at vertex 0 and construct a parent-before-child order. vector<int> parent(N, -1), order; order.reserve(N); parent[0] = 0; order.push_back(0); for (int i = 0; i < (int)order.size(); ++i) { int v = order[i]; for (int to : adj[v]) { if (parent[to] != -1) continue; parent[to] = v; order.push_back(to); } } // Subtree sizes for the initial root. vector<int> sub(N, 1); for (int i = N - 1; i >= 1; --i) { int v = order[i]; sub[parent[v]] += sub[v]; } // Modular inverses of 1, 2, ..., N. vector<int64> inv(N + 1); inv[1] = 1; for (int i = 2; i <= N; ++i) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; } // reciprocalF[v] = 1 / F_v, where // F_v = product of subtree sizes when the tree is rooted at v. vector<int64> reciprocalF(N); reciprocalF[0] = 1; for (int v = 0; v < N; ++v) { reciprocalF[0] = reciprocalF[0] * inv[sub[v]] % MOD; } int64 sum = reciprocalF[0]; // Reroot from each parent u to its child v. for (int i = 1; i < N; ++i) { int v = order[i]; int u = parent[v]; int s = sub[v]; reciprocalF[v] = reciprocalF[u] * s % MOD * inv[N - s] % MOD; sum += reciprocalF[v]; if (sum >= MOD) sum -= MOD; } // Inverse of (N-1)!. int64 invFactorial = 1; for (int i = 1; i < N; ++i) { invFactorial = invFactorial * inv[i] % MOD; } cout << sum * invFactorial % MOD << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int MOD = 1'000'000'007; constexpr int MAXN = 200'000; int64 modPow(int64 a, int64 e) { int64 result = 1; while (e > 0) { if (e & 1) result = result * a % MOD; a = a * a % MOD; e >>= 1; } return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); vector<int64> fact(MAXN + 1), invFact(MAXN + 1), invNum(MAXN + 1); fact[0] = 1; for (int i = 1; i <= MAXN; ++i) { fact[i] = fact[i - 1] * i % MOD; } invFact[MAXN] = modPow(fact[MAXN], MOD - 2); for (int i = MAXN; i >= 1; --i) { invFact[i - 1] = invFact[i] * i % MOD; } for (int i = 1; i <= MAXN; ++i) { invNum[i] = fact[i - 1] * invFact[i] % MOD; } int T; cin >> T; while (T--) { int n; cin >> n; vector<vector<int>> adj(n); for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u); } // Root the tree at vertex 0 and construct a parent-before-child order. vector<int> parent(n, -1), order; order.reserve(n); order.push_back(0); for (int i = 0; i < (int)order.size(); ++i) { int u = order[i]; for (int v : adj[u]) { if (v == parent[u]) continue; parent[v] = u; order.push_back(v); } } // Subtree sizes for the initial root. vector<int> subtree(n, 1); for (int i = n - 1; i >= 1; --i) { int v = order[i]; subtree[parent[v]] += subtree[v]; } // ways[r] = number of orders in which every parent precedes its // children, when the tree is rooted at r. vector<int64> ways(n); ways[0] = fact[n]; for (int v = 0; v < n; ++v) { ways[0] = ways[0] * invNum[subtree[v]] % MOD; } // Reroot from each parent u to its child v. for (int i = 1; i < n; ++i) { int v = order[i]; int u = parent[v]; ways[v] = ways[u] * subtree[v] % MOD * invNum[n - subtree[v]] % MOD; } int64 total = 0; for (int r = 0; r < n; ++r) { total = (total + ways[r]) % MOD; } // Probability = total / (n! * (n-1)!). int64 answer = total * invFact[n] % MOD * invFact[n - 1] % MOD; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int64 MOD = 1'000'000'007LL; int64 modPow(int64 base, int64 exponent) { int64 result = 1; while (exponent > 0) { if (exponent & 1) result = result * base % MOD; base = base * base % MOD; exponent >>= 1; } return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<vector<int>> adj(N + 1); for (int i = 0; i < N - 1; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } // Root the tree at vertex 1 and construct a parent-before-child order. vector<int> parent(N + 1, 0); vector<int> order; order.reserve(N); order.push_back(1); for (int i = 0; i < (int)order.size(); ++i) { int v = order[i]; for (int u : adj[v]) { if (u == parent[v]) continue; parent[u] = v; order.push_back(u); } } // Subtree sizes when rooted at vertex 1. vector<int> subtree(N + 1, 1); for (int i = N - 1; i >= 1; --i) { int v = order[i]; subtree[parent[v]] += subtree[v]; } // Modular inverses of 1, 2, ..., N. vector<int64> inv(N + 1); inv[1] = 1; for (int i = 2; i <= N; ++i) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; } // q[v] = 1 / P_v, where P_v is the product of the non-root // subtree sizes when the entire tree is rooted at v. vector<int64> q(N + 1); int64 rootQ = 1; for (int v = 2; v <= N; ++v) { rootQ = rootQ * inv[subtree[v]] % MOD; } q[1] = rootQ; int64 sumQ = rootQ; // Reroot from each parent to its children. for (int i = 1; i < N; ++i) { int v = order[i]; int u = parent[v]; q[v] = q[u] * subtree[v] % MOD; q[v] = q[v] * inv[N - subtree[v]] % MOD; sumQ += q[v]; if (sumQ >= MOD) sumQ -= MOD; } int64 factorial = 1; for (int i = 2; i <= N; ++i) { factorial = factorial * i % MOD; } cout << sumQ * modPow(factorial, MOD - 2) % MOD << '\n'; } return 0; }