题解:CF1210F2 Marek and Matching (hard version)

· · 题解

考虑利用 Hall 定理容斥。

若不存在完美匹配,那么一定存在一个 A,使 |A|>|G(A)|(G(A) 表示 A 的邻居集合)。

考虑钦定代表元,对于一个不合法方案,我们取 |A|-|G(A)| 最大的 A,若这个值相同则取 |A| 最小的,来代表它。下面说明这样的 A 是唯一的:

若不唯一,则存在 B\ne A 使 |B|-|G(B)|=|A|-|G(A)| 且 |B|=|A|。考虑 B 和 A 的关系:

考虑计数。令 f_{S,T} 表示只考虑 S,T 之间的边(S 为左部点,T 为右部点,T 就是 G(S)),(S,T) 为不合法的代表元的概率,g_{S,T} 表示只考虑 S,T 之间的边,不存在不合法代表元的概率。

对于 f_{S,T} 需满足 |S|>|T|,这是不合法的代表元必须满足的条件;对于 g_{S,T} 需要满足 |S|\leq |T|,否则 (S,T) 就会是一对不合法的元。

我们预处理 to_{S,T} 表示 T\subseteq G(S) 的概率(也就是只考虑 S,T 之间的边时,G(S)=T 的概率),no_{S,T} 表示 S,T 之间没有连边的概率,那么:

f_{S,T}=to_{S,T}-\sum_{S_1\subseteq S}\sum_{T_1\subseteq T} [|S_1|-|T_1|\geq |S|-|T|]f_{S_1,T_1}\times no_{S_1,T-T_1}\times g_{S-S_1,T-T_1}\\ g_{S,T}=to_{S,T}-\sum_{S_1\subseteq S}\sum_{T_1\subseteq T} f_{S_1,T_1}\times no_{S_1,T-T_1}\times g_{S-S_1,T-T_1}

其中 (S_1,T_1) 是枚举的更优的代表元,我们要减掉这些方案;no_{S_1,T-T_1} 是为了保证在 (S,T) 的视角下来看,G(S_1)=T_1,避免有额外的边导致 G(S_1) 扩大;g_{S-S_1,T-T_1} 是为了保证外部合法,不然外部也不合法的话,拼起来就更优了。

时间复杂度 O(3^{2n})。

const int N = 7, M = 1 << N, mod = 1e9 + 7, inv = 570000004;

int n, a[N][N], f[M][M], g[M][M], G[M][M], no[M][M], cnt[M];

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> a[i][j], a[i][j] = 1ll * a[i][j] * inv % mod;
    for (int i = 0; i < 1 << n; i++)
        cnt[i] = __builtin_popcount(i);
    for (int s = 0; s < 1 << n; s++) // s 与 t 没连概率
        for (int t = 0; t < 1 << n; t++)
        {
            no[s][t] = 1;
            for (int i = 0; i < n; i++)
                if (s >> i & 1)
                    for (int j = 0; j < n; j++)
                        if (t >> j & 1)
                            no[s][t] = no[s][t] * (1ll - a[i][j]) % mod;
        }
    for (int s = 0; s < 1 << n; s++) // t ⊆ G(s) 概率
        for (int t = 0; t < 1 << n; t++)
        {
            G[s][t] = 1;
            for (int j = 0; j < n; j++)
                if (t >> j & 1)
                {
                    int bad = 1;
                    for (int i = 0; i < n; i++)
                        if (s >> i & 1)
                            bad = bad * (1ll - a[i][j]) % mod;
                    G[s][t] = G[s][t] * (1ll - bad) % mod;
                }
        }
    for (int s = 0; s < 1 << n; s++)
        for (int t = 0; t < 1 << n; t++)
        {
            if (cnt[s] > cnt[t])
            {
                f[s][t] = G[s][t];
                for (int s1 = s;; s1 = s1 - 1 & s)
                {
                    for (int t1 = t;; t1 = t1 - 1 & t)
                    {
                        if ((s ^ s1 || t ^ t1) && cnt[s1] - cnt[t1] >= cnt[s] - cnt[t])
                            f[s][t] = (f[s][t] - 1ll * f[s1][t1] * no[s1][t ^ t1] % mod * g[s ^ s1][t ^ t1]) % mod;
                        if (!t1)
                            break;
                    }
                    if (!s1)
                        break;
                }
            }
            else
            {
                g[s][t] = G[s][t];
                for (int s1 = s;; s1 = s1 - 1 & s)
                {
                    for (int t1 = t;; t1 = t1 - 1 & t)
                    {
                        g[s][t] = (g[s][t] - 1ll * f[s1][t1] * no[s1][t ^ t1] % mod * g[s ^ s1][t ^ t1]) % mod;
                        if (!t1)
                            break;
                    }
                    if (!s1)
                        break;
                }
            }
        }
    cout << (g[(1 << n) - 1][(1 << n) - 1] + mod) % mod << "\n";
    return 0;
}