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 fi.

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 pq where p and q are coprime integers. Calculate pq1mod(109+7).

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t5104). Description of the test cases follows.

The first line of each test case contains 3 integers n, m and k (2n105, 0mmin(105, n(n1)2), 1k2105) — the number of children, pairs of friends and excursions respectively.

The next m lines contain three integers each — ai, bi, fi — the indices of the pair of children who are friends and their friendship value. (aibi, 1ai,bin, 1fi109). 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 105 and the sum of k over all test cases does not exceed 2105.

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++10=55.

For the third test case, the final answer is 79=777777784mod(109+7).

 

解题思路

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

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

  所以第 i 对好友对期望的总贡献是 j=0k(fi+fi+1++fi+j1)pi,j,其中 pi,j 表示 k 次选择中第 i 对好友被选择了 j 次概率。

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

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

j=0k(fi+fi+1++fi+j1)Ckj(Cn21)kj(Cn2)k=1(Cn2)kj=0k(fi+fi+1++fi+j1)Ckj(Cn21)kj

  因此 m 对好友对期望的贡献就是:1(Cn2)ki=1mj=0k(fi+fi+1++fi+j1)Ckj(Cn21)kj

  如果直接按公式算的话那么时间复杂度至少为 O(mk),可以发现对于每个 j(Cn21)kj 这部分都是一样的,只有 (fi++fi+j1) 不同,并且随着 j 的增加,每次都会增加一项 fi+j1,这启发我们可以动态维护这部分的和从而避免枚举 i。具体做法是初始设 sum=0,枚举到 j 时令 sumsum+s+m(j1),其中 s=i=1mfi

  AC 代码如下,时间复杂度为 O(n(logk+logmod))

#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;
}

  再给出官方的做法,也是贡献法求期望,不过分成了两步,即分别求不变和变化两部分的期望。先求 fi 固定不变的情况,即无论第 i 对好友被选择多少次 fi 都不会加 1,与上面的推导一样这部分对期望的贡献为 fij=0kjCkj(Cn21)kj(Cn2)k,化简一下就会变对成二项分布求期望的公式:

fij=0kjCkj(Cn21)kj(Cn2)k=fij=0kjCkj(Cn21)kj(Cn2)j(Cn2)kj=fij=0kjCkj(1Cn2)j(11Cn2)kj

  其中 1Cn2 是在一次选择中选到第 i 对好友的概率。

  根据二项分布的期望公式,若 XB(n,p),则 E(X)=np,因此第 i 对好友这部分对期望的贡献就是 kCn2fi。因此不变的部分中,m 对好友的贡献就是 kCn2i=1mfi

  对于变化的部分,m 对好友都是一样的,因此只需求一次,最后乘上 m 即可。对于变化的部分就不再考虑 fi 了,而是统计增加的数值的期望,即如果 k 次选择中某对朋友被选了 j 次,那么增加的数值就是 0+1++j1=j(j1)2,因此变化部分的期望就是:mj=0kj(j1)2Ckj(1Cn2)j(11Cn2)kj

  因此总的期望就是:kCn2i=1mfi+mj=0kj(j1)2Ckj(1Cn2)j(11Cn2)kj

  AC 代码如下,时间复杂度为 O(n(logk+logmod))

#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 @   onlyblues  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示