无名概率题1

题目大意:n个点构成一个环,相邻两点之间有一条边,每次随机选出两个点,l,r,从l走到r,(若l==r则绕环一整圈),问期望多少次走完所有的边。(n<=10)

(保留10位小数,不四舍五入,得分是与标答相等的位数)

思路:

数据范围感人啊,骗分的话直接随机跑个几十亿次,这样几乎能保证每个点3-4分,但是既然数据都这么小了,为什么不记忆化状态压缩搜索呢?

我们设f[i]为从i状态走完所有边的期望步数,f[(1<<n)-1] = 0,然后对于每一个状态,我们枚举所有l,r的组合对于该状态的影响,即会变成什么状态,对于每一个状态解一个方程就行了,设t(s,i)为状态s在i转移下变成的状态。


其中t为不同于s的状态。

    F[0]就是答案,比较坑的是不四舍五入,那么我们就先打一张取到11位的表,再把第11位去掉就行了。(捂脸..)。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;
int n,tot = 0,num[100010];
double f[100010];
double dfs(int x) {
	if(x == (1 << n) -1 ) { f[x] = 0;return f[x];}
	double K = 1,D = 0;
	for(int i = 1;i <= tot;i ++) {
		if((x | num[i]) == x) K -= double(1) / tot;
		else {
			if(f[x | num[i]] == 0) 
			    f[x | num[i]] = dfs(x | num[i]);
			D += (double(1) / tot) * f[x | num[i]];
		}
	}
	return (D + double(1)) / K;
}
int main() {
	for(int w = 1;w <= 10;w ++) {
		n = w;tot = 0;
		memset(f,0,sizeof(f));
		memset(num,0,sizeof(num));
		for(int i = 1;i <= n;i ++) {
			for(int j = 1;j <= n;j ++) {
				int l = i,r = j;
				tot ++ ;
				if(l < r) {
					for(int k = l;k <= r - 1;k ++) {
						num[tot] += 1 << (k - 1);
					}
				}
				if(l >= r) {
					for(int k = 1;k <= r - 1;k ++) {
						num[tot] += 1 << (k - 1);
					}
					for(int k = l;k <= n;k ++) {
						num[tot] += 1 << (k - 1);
					}
				}
			}
		}
	    printf("%0.11f\n",dfs(0));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值