BZOJ 3925: [Zjoi2015] 地震后的幻想乡(概率DP)

这里有一篇很好很强的博客%%%
YouSiki大佬的博客
多理解一会就行了…

代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 10;
const int MAXM = 46;
const int MAXS = 1<<10;
int n, m, e[MAXN], bitcnt[MAXS], cnt[MAXS];
LL f[MAXS][MAXM], g[MAXS][MAXM], c[MAXM][MAXM];
inline void Pre() {
	c[0][0] = 1;
	for(int i = 1; i < MAXM; ++i) {
		c[i][0] = c[i][i] = 1;
		for(int j = 1; j < i; ++j)
			c[i][j] = c[i-1][j-1] + c[i-1][j];
	}
}
int main () {
	Pre();
	scanf("%d%d", &n, &m);
	for(int i = 0, x, y; i < m; ++i)
		scanf("%d%d", &x, &y), --x, --y, e[x] |= 1<<y, e[y] |= 1<<x;
	for(int s = 1; s < (1<<n); ++s) {
		if((bitcnt[s] = bitcnt[s>>1] + (s&1)) == 1) { g[s][0] = 1; continue; }
		for(int i = 0; i <= n; ++i)
			if(s&(1<<i)) cnt[s] += bitcnt[e[i]&s];
		cnt[s]>>=1;
		int one = s&-s, outside = s^one;
		for(int sub = outside; sub; sub = (sub-1)&outside)
			for(int i = 0; i <= cnt[sub]; ++i)
				for(int j = 0; j <= cnt[s^sub]; ++j)
					f[s][i+j] += c[cnt[sub]][i] * g[s^sub][j];
		for(int i = 0; i <= cnt[s]; ++i) g[s][i] = c[cnt[s]][i] - f[s][i];
	}
	double ans = 0;
	for(int i = 0; i < m; ++i)
		ans += (double)f[(1<<n)-1][i]/c[cnt[(1<<n)-1]][i]; //注意转型为double
	printf("%.6f\n", ans/(m+1));
}
posted @ 2019-12-14 14:51  _Ark  阅读(79)  评论(0编辑  收藏  举报