D. Good Trip

D. Good Trip

There are $n$ children in a class, $m$ pairs among them are friends. The $i$-th pair who are friends have a friendship value of $f_i$.

The teacher has to go for $k$ excursions, and for each of the excursions she chooses a pair of children randomly, equiprobably and independently. If a pair of children who are friends is chosen, their friendship value increases by $1$ for all subsequent excursions (the teacher can choose a pair of children more than once). The friendship value of a pair who are not friends is considered $0$, and it does not change for subsequent excursions.

Find the expected value of the sum of friendship values of all $k$ pairs chosen for the excursions (at the time of being chosen). It can be shown that this answer can always be expressed as a fraction $\dfrac{p}{q}$ where $p$ and $q$ are coprime integers. Calculate $p\cdot q^{-1} \bmod (10^9+7)$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5 \cdot 10^4$). Description of the test cases follows.

The first line of each test case contains $3$ integers $n$, $m$ and $k$ ($2 \le n \le 10^5$, $0 \le m \le \min \Big(10^5$, $ \frac{n(n-1)}{2} \Big)$, $1 \le k \le 2 \cdot 10^5$) — the number of children, pairs of friends and excursions respectively.

The next $m$ lines contain three integers each — $a_i$, $b_i$, $f_i$ — the indices of the pair of children who are friends and their friendship value. ($a_i \neq b_i$, $1 \le a_i,b_i \le n$, $1 \le f_i \le 10^9$). It is guaranteed that all pairs of friends are distinct.

It is guaranteed that the sum of $n$ and sum $m$ over all test cases does not exceed $10^5$ and the sum of $k$ over all test cases does not exceed $2 \cdot 10^5$.

Output

For each test case, print one integer — the answer to the problem.

Example

input

4
100 0 24
2 1 10
1 2 1
3 1 2
2 1 1
5 2 4
1 2 25
3 2 24

output

0
55
777777784
40000020

Note

For the first test case, there are no pairs of friends, so the friendship value of all pairs is $0$ and stays $0$ for subsequent rounds, hence the friendship value for all excursions is $0$.

For the second test case, there is only one pair possible $(1, 2)$ and its friendship value is initially $1$, so each turn they are picked and their friendship value increases by $1$. Therefore, the total sum is $1+2+3+\ldots+10 = 55$.

For the third test case, the final answer is $\frac{7}{9} = 777\,777\,784\bmod (10^9+7)$.

 

解题思路

  纯数学题,看到期望题不要总想着用概率 dp 去做,可以尝试结合贡献法与最原始求期望的做法去思考。

  由于期望具有线性性,可以单独考虑每一对好友对期望的贡献是多少(因为非好友的 $f_i$ 始终为 $0$ 因此不用考虑)。对于第 $i$ 对好友的 $f_i$,由于 $k$ 次选择是相互独立的,因此有可能会被选择了 $0, \, 1, \, \ldots, \, k$ 次。由于每次被选择后 $f_i$ 都会加 $1$,因此每种情况贡献的期望是不同的,要分别计算。

  所以第 $i$ 对好友对期望的总贡献是 $\sum\limits_{j=0}^{k}{\left( f_i + f_i+1 + \cdots + f_i+j-1 \right) \cdot p_{i,j}}$,其中 $p_{i,j}$ 表示 $k$ 次选择中第 $i$ 对好友被选择了 $j$ 次概率。

  考虑如何求出 $p_{i,j}$。我们知道概率的本质是该事件出现的总次数除以所有可能发生的事件的总数,而所有可能发生的事件的总数很容易知道,因为 $k$ 次选择是相互独立的,每一次选择是从 $n$ 个人中任意选择两个组成一对,共 $C_{n}^{2}$ 种选法,根据乘法原理 $k$ 次选择一共会有 $\left( C_{n}^{2} \right)^k$ 种选法。那么在这 $k$ 次的选择中,其中有 $j$ 次是第 $i$ 对好友的选法有多少种?先从 $k$ 次选择中选出 $j$ 个表示第 $i$ 对好友,有 $C_{k}^{j}$ 种选法,那么剩下的 $k-j$ 个只能选其他的数对,有 $\left( C_{n}^{2} - 1 \right)^{k-j}$ 种选法。根据乘法原理一共就有 $C_{k}^{j} \cdot \left( C_{n}^{2} - 1 \right)^{k-j}$ 种选法,因此 $p_{i,j}$ 就等于:$$p_{i,j} = \frac{C_{k}^{j} \cdot \left( C_{n}^{2} - 1 \right)^{k-j}}{\left( C_{n}^{2} \right)^k}$$

  因此第 $i$ 对好友对期望的总贡献就是:

$$\begin{align*}
&\sum\limits_{j=0}^{k}{\left( f_i + f_i+1 + \cdots + f_i+j-1 \right) \cdot \frac{C_{k}^{j} \cdot \left( C_{n}^{2} - 1 \right)^{k-j}}{\left( C_{n}^{2} \right)^k}} \\
=& \frac{1}{\left( C_{n}^{2} \right)^k} \cdot \sum\limits_{j=0}^{k}{\left( f_i + f_i+1 + \cdots + f_i+j-1 \right) \cdot C_{k}^{j} \cdot \left( C_{n}^{2} - 1 \right)^{k-j}}
\end{align*}$$

  因此 $m$ 对好友对期望的贡献就是:$$\frac{1}{\left( C_{n}^{2} \right)^k} \cdot \sum\limits_{i=1}^{m}{\sum\limits_{j=0}^{k}{\left( f_i + f_i+1 + \cdots + f_i+j-1 \right) \cdot C_{k}^{j} \cdot \left( C_{n}^{2} - 1 \right)^{k-j}}}$$

  如果直接按公式算的话那么时间复杂度至少为 $O(m \cdot k)$,可以发现对于每个 $j$,$\left( C_{n}^{2} - 1 \right)^{k-j}$ 这部分都是一样的,只有 $\left( f_i + \cdots + f_i+j-1 \right)$ 不同,并且随着 $j$ 的增加,每次都会增加一项 $f_i+j-1$,这启发我们可以动态维护这部分的和从而避免枚举 $i$。具体做法是初始设 $\text{sum} = 0$,枚举到 $j$ 时令 $\text{sum} \gets \text{sum} + s + m \cdot (j-1)$,其中 $s = \sum\limits_{i=1}^{m}{f_i}$。

  AC 代码如下,时间复杂度为 $O\left(n \cdot\left( \log{k} + \log{\text{mod}} \right) \right)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int mod = 1e9 + 7;

int qmi(int a, int k) {
    int ret = 1;
    while (k) {
        if (k & 1) ret = 1ll * ret * a % mod;
        a = 1ll * a * a % mod;
        k >>= 1;
    }
    return ret;
}

void solve() {
    int n, m, k;
    scanf("%d %d %d", &n, &m, &k);
    int s = 0;
    for (int i = 0; i < m; i++) {
        int x;
        scanf("%*d %*d %d", &x);
        s = (s + x) % mod;
    }
    int ret = 0, p = n * (n - 1ll) % mod * qmi(2, mod - 2) % mod;    // p=C(n,2)
    for (int i = 1, t = 1, sum = 0; i <= k; i++) {    // t是动态维护组合数C(k,j)
        t = t * (k - i + 1ll) % mod * qmi(i, mod - 2) % mod;
        sum = (sum + s + m * (i - 1ll)) % mod;
        ret = (ret + 1ll * sum * t % mod * qmi(p - 1 + mod, k - i)) % mod;
    }
    printf("%d\n", 1ll * ret * qmi(qmi(p, k), mod - 2) % mod);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

  再给出官方的做法,也是贡献法求期望,不过分成了两步,即分别求不变和变化两部分的期望。先求 $f_i$ 固定不变的情况,即无论第 $i$ 对好友被选择多少次 $f_i$ 都不会加 $1$,与上面的推导一样这部分对期望的贡献为 $f_i \cdot \sum\limits_{j=0}^{k}{j \cdot \frac{C_{k}^{j} \cdot \left( C_{n}^{2} - 1 \right)^{k-j}}{\left( C_{n}^{2} \right)^k}}$,化简一下就会变对成二项分布求期望的公式:

$$\begin{align*}
& f_i \cdot \sum\limits_{j=0}^{k}{j \cdot \frac{C_{k}^{j} \cdot \left( C_{n}^{2} - 1 \right)^{k-j}}{\left( C_{n}^{2} \right)^k}} \\
=& f_i \cdot \sum\limits_{j=0}^{k}{j \cdot C_{k}^{j} \cdot \frac{\left( C_{n}^{2} - 1 \right)^{k-j}}{\left( C_{n}^{2} \right)^{j} \cdot \left( C_{n}^{2} \right)^{k-j}}} \\
=& f_i \cdot \sum\limits_{j=0}^{k}{j \cdot C_{k}^{j} \cdot \left( \frac{1}{C_{n}^{2}} \right)^{j} \cdot \left( 1 - \frac{1}{C_{n}^{2}} \right)^{k-j}}
\end{align*}$$

  其中 $\frac{1}{C_{n}^{2}}$ 是在一次选择中选到第 $i$ 对好友的概率。

  根据二项分布的期望公式,若 $X \sim B(n,p)$,则 $E(X)=np$,因此第 $i$ 对好友这部分对期望的贡献就是 $\frac{k}{C_{n}^{2}} \cdot f_i$。因此不变的部分中,$m$ 对好友的贡献就是 $\frac{k}{C_{n}^{2}} \sum\limits_{i=1}^{m}{f_i}$。

  对于变化的部分,$m$ 对好友都是一样的,因此只需求一次,最后乘上 $m$ 即可。对于变化的部分就不再考虑 $f_i$ 了,而是统计增加的数值的期望,即如果 $k$ 次选择中某对朋友被选了 $j$ 次,那么增加的数值就是 $0 + 1 + \cdots + j-1 = \frac{j(j-1)}{2}$,因此变化部分的期望就是:$$m \cdot \sum\limits_{j=0}^{k}{\frac{j(j-1)}{2} \cdot C_{k}^{j} \cdot \left( \frac{1}{C_{n}^{2}} \right)^{j} \cdot \left( 1 - \frac{1}{C_{n}^{2}} \right)^{k-j}}$$

  因此总的期望就是:$$\frac{k}{C_{n}^{2}} \sum\limits_{i=1}^{m}{f_i} + m \cdot \sum\limits_{j=0}^{k}{\frac{j(j-1)}{2} \cdot C_{k}^{j} \cdot \left( \frac{1}{C_{n}^{2}} \right)^{j} \cdot \left( 1 - \frac{1}{C_{n}^{2}} \right)^{k-j}}$$

  AC 代码如下,时间复杂度为 $O\left(n \cdot\left( \log{k} + \log{\text{mod}} \right) \right)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int mod = 1e9 + 7;

int qmi(int a, int k) {
    int ret = 1;
    while (k) {
        if (k & 1) ret = 1ll * ret * a % mod;
        a = 1ll * a * a % mod;
        k >>= 1;
    }
    return ret;
}

void solve() {
    int n, m, k;
    scanf("%d %d %d", &n, &m, &k);
    int s = 0;
    for (int i = 0; i < m; i++) {
        int x;
        scanf("%*d %*d %d", &x);
        s = (s + x) % mod;
    }
    int ret = 0, p = 2ll * qmi(n * (n - 1ll) % mod, mod - 2) % mod;
    for (int i = 1, t = 1; i <= k; i++) {
        t = t * (k - i + 1ll) % mod * qmi(i, mod - 2) % mod;
        ret = (ret + i * (i - 1ll) % mod * t % mod * qmi(p, i) % mod * qmi(1 - p + mod, k - i)) % mod;
    }
    ret = (1ll * ret * m % mod * qmi(2, mod - 2) + 1ll * k * s % mod * p) % mod;
    printf("%d\n", ret);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 921 (Div. 1, Div. 2) Editorial:https://codeforces.com/blog/entry/125137

  Codeforces Round 921 (Div. 2) A-D:https://zhuanlan.zhihu.com/p/680178758

  Codeforces Round 921 (Div. 2) A - D讲解:https://www.bilibili.com/video/BV1ep4y1m7rJ/

posted @ 2024-02-03 14:22  onlyblues  阅读(13)  评论(0编辑  收藏  举报
Web Analytics