跳到正文

题解

P7105 题解

Harry27182约 4 分钟
原文
文章信息与版本

文章信息

发布
2022年9月13日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 4 分钟
获赞
4
题目
P7105

版本

  1. v1当前

数据范围很小,可以很自然地想到状压 dp。

我们考虑一个比较套路的状态设计(我第一次学),设 fsf_s 表示 ss 中的点内部相互连通的概率。

下面考虑如何 dp。我们发现很难由已知状态转移到当前状态,所以正难则反吗,考虑容斥,我们处理 ss 集合内部不连通的概率,考虑钦定一个元素让 ss 的子集 tt 一定包含,那么可以有转移式:

fs=1−∑tft×P(s⊕t,t)f_s=1-\sum_{t} f_{t}\times P(s\oplus t,t)

其中 P(s⊕t,t)P(s\oplus t,t) 表示这两个集合之间互不连通的概率。

稍微手模一下可以发现钦定一个元素可以让转移做到不重不漏,这里我们钦定了编号最小的元素,方便代码实现。

于是我们得到了一个 O(3n×n2)O(3^n\times n^2) 的 dp,已经可以通过本题。

考虑进一步优化,我们发现 3n3^n 的枚举子集是无法优化的,所以需要考虑优化转移过程,可以比较自然的想到预处理 P(s⊕t,t)P(s\oplus t,t)。

直接处理显然是 4n×n24^n\times n^2 的,所以我们发挥人类智慧,考虑 meet in middle。

分前 n2\frac {n}{2} 和后 n2\frac {n}{2} 来处理,如果两个集合 ss 与 tt ,它们的:

  • ss 的前 n2\frac {n}{2} 与 tt 的前 n2\frac {n}{2} 不连通

  • ss 的前 n2\frac {n}{2} 与 tt 的后 n2\frac {n}{2} 不连通

  • ss 的后 n2\frac {n}{2} 与 tt 的前 n2\frac {n}{2} 不连通

  • ss 的后 n2\frac {n}{2} 与 tt 的后 n2\frac {n}{2} 不连通

他们就是不连通的。

所以我们可以在 2n×n22^n\times n^2 的时间内预处理,时间复杂度为 O(2n×n2+3n)O(2^n\times n^2+3^n)。

实现细节可以参考代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n,m;double p[20][20],g[2][2][1005][1005],f[1000005],ans;
double get(int i,int j){return g[0][0][i&((1<<m)-1)][j&((1<<m)-1)]*g[1][0][i>>m][j&((1<<m)-1)]*g[0][1][i&((1<<m)-1)][j>>m]*g[1][1][i>>m][j>>m];}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)scanf("%lf",&p[i][j]);
	} 
	m=(n+1)>>1;
	for(int p1=0;p1<2;p1++)
	{
		for(int p2=0;p2<2;p2++)
		{
			for(int i=0;i<(1<<m);i++)
			{
				for(int j=0;j<(1<<m);j++)
				{
					g[p1][p2][i][j]=1;
					for(int k=0;k<m;k++)
					{
						for(int l=0;l<m;l++)
						{
							if((i&(1<<k))&&(j&(1<<l)))
							{
								g[p1][p2][i][j]*=1-p[k+p1*m][l+p2*m];
							}
						}
					}
				}
			}
		}
	}
	for(int s=1;s<(1<<n);s++)
	{
		int now=s^(s&(-s));
		f[s]=1;
		for(int t=now;t;t=now&(t-1))f[s]-=f[s^t]*get(t,s^t);
	}
	for(int i=1;i<(1<<n);i++)ans+=f[i]*get(i,((1<<n)-1)^i);
	printf("%.6lf",ans);
	return 0;
}
原文
存档评论1 条抓取于