2020杭电多校第六场 1010-Expectation 生成树计数

1010-Expectation

题意

给一个\(n\)个点\(m\)条边的无向图,定义生成树的权值为生成树上所有边权的 $ AND $ 和,问随机选择一个生成树的权值的期望是多少。

\(n \le100,m \le 10^4,w\le 10^9\)

分析

利用基尔霍夫矩阵可以在\(O(n^3)\)的时间内计算出生成树的个数,先计算出总的生成树个数\(tot\),因为\(AND\)运算是二进制当前位全部为\(1\)才为\(1\),枚举二进制每一位,将边权的二进制中这一位为\(1\)的边加进图中,计算一遍生成树的个数\(num\),二进制第\(i\)位对期望的贡献即为\(2^i \times \frac{num}{tot}\),将二进制每一位的贡献加起来就是总的期望值了。

总时间复杂度为\(O(Tn^3log(w))\)

code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<sstream>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
#define sz(a) int(a.size())
#define rson mid+1,r,p<<1|1
#define pii pair<int,int>
#define lson l,mid,p<<1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps = 1e-8;
const ll mod = 998244353;
const int N = 1e5 + 10;
const int inf = 1e9;
struct ppo {
    int x, y, w;
} e[N];
int T;
ll K[110][110];
ll ksm(ll a, ll b) {
    ll ret = 1;
    while (b) {
        if (b & 1) ret = ret * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ret;
}
ll gauss(int n) {
    ll res = 1;
    for (int i = 1; i <= n - 1; i++) {
        for (int j = i + 1; j <= n - 1; j++) {
            while (K[j][i]) {
                int t = K[i][i] / K[j][i];
                for (int k = i; k <= n - 1; k++)
                    K[i][k] = (K[i][k] - 1ll * t * K[j][k] % mod + mod) % mod;
                swap(K[i], K[j]);
                res = -res;
            }
        }
        res = (res * K[i][i]) % mod;
        res = (res + mod) % mod;
    }
    return (res + mod) % mod;
}
int main() {
    scanf("%d", &T);
    while (T--) {
        int n, m;
        scanf("%d%d", &n, &m);
        memset(K, 0, sizeof(K));
        for (int i = 1; i <= m; i++) {
            int x, y, w;
            scanf("%d%d%d", &x, &y, &w);
            K[x - 1][x - 1]++;
            K[y - 1][y - 1]++;
            K[x - 1][y - 1]--;
            K[y - 1][x - 1]--;
            e[i] = ppo{x, y, w};
        }
        ll base = gauss(n);
        base = ksm(base, mod - 2);
        ll ans = 0;
        rep(i, 0, 30) {
            memset(K, 0, sizeof(K));
            rep(j, 1, m) if (e[j].w >> i & 1) {
                ppo it = e[j];
                K[it.x - 1][it.x - 1]++;
                K[it.y - 1][it.y - 1]++;
                K[it.x - 1][it.y - 1]--;
                K[it.y - 1][it.x - 1]--;
            }
            ans += (1ll << i) % mod * gauss(n) % mod ;
            ans %= mod;
        }
        ans = (ans * base) % mod;
        printf("%lld\n", ans);
    }

    return 0;
}
posted @ 2020-08-06 22:57  xyq0220  阅读(164)  评论(0编辑  收藏  举报