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));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值