题解:CF1210F2 Marek and Matching (hard version)
CF1210F2
吐槽:NOIP 模拟赛出了此题的加强版,其数据范围是
对于二分图完美(完备)匹配,套路地考虑 Hall 定理,令
令
首先
我们还需要求出
最终答案就是
:::info[代码]
#include <bits/stdc++.h>
#define popc(x) __builtin_popcount(x)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
inline int Mod(int x) { return x<0 ? x+mod : (x>=mod ? x-mod : x); }
inline void adm(int &x,int y) { x=Mod(x+y); }
inline void dec(int &x,int y) { x=Mod(x-y); }
inline void pro(int &x,int y) { x=(ll)x*y%mod; }
inline int qmi(ll a,int b)
{
ll res=1;
for (;b;b>>=1,a=a*a%mod) if (b&1) res=res*a%mod;
return res;
}
const int N=9;
int n;
int p[N][N];
int G[1<<N][1<<N],no[1<<N][1<<N];
int g[1<<N][1<<N],f[1<<N][1<<N];
int main()
{
freopen("magician.in","r",stdin);
freopen("magician.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin >> n;
int inv=qmi(100,mod-2);
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
cin >> p[i][j];
pro(p[i][j],inv);
}
}
for (int S=0;S<(1<<n);S++)
{
for (int T=0;T<(1<<n);T++)
{
G[S][T]=1;
for (int y=0;y<n;y++)
{
if (!(T>>y&1)) continue; int pr=1;
for (int x=0;x<n;x++) if (S>>x&1) pro(pr,Mod(1-p[x][y]));
pro(G[S][T],Mod(1-pr));
}
no[S][T]=1;
for (int x=0;x<n;x++) if (S>>x&1) for (int y=0;y<n;y++) if (T>>y&1) pro(no[S][T],Mod(1-p[x][y]));
}
}
for (int S=0;S<(1<<n);S++)
{
for (int T=0;T<(1<<n);T++)
{
if (popc(S)<=popc(T))
{
f[S][T]=G[S][T];
for (int s=S;;s=(s-1)&S)
{
for (int t=T;;t=(t-1)&T)
{
if (s || t)
if (g[s][t] && no[s][T^t] && f[S^s][T^t])
dec(f[S][T],(ll)g[s][t]*no[s][T^t]%mod*f[S^s][T^t]%mod);
if (!t) break;
}
if (!s) break;
}
}
else
{
g[S][T]=G[S][T];
for (int s=S;;s=(s-1)&S)
{
for (int t=T;;t=(t-1)&T)
{
if (s!=S || t!=T)
if (popc(s)-popc(t)>=popc(S)-popc(T))
if (g[s][t] && no[s][T^t] && f[S^s][T^t])
dec(g[S][T],(ll)g[s][t]*no[s][T^t]%mod*f[S^s][T^t]%mod);
if (!t) break;
}
if (!s) break;
}
}
}
}
cout << f[(1<<n)-1][(1<<n)-1] << "\n";
return 0;
}
:::