zoj3551---Bloodsucker

本文探讨了一个关于吸血鬼转变人群的概率问题,并提供了一种基于动态规划的解决方案。通过数学期望计算,在特定概率下,所有人转变为吸血鬼所需的平均天数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bloodsucker

Time Limit: 2 Seconds      Memory Limit: 65536 KB

In 0th day, there are n-1 people and 1 bloodsucker.Every day, two and only two of them meet. Nothing will happen if they are of the same species, that is, a people meets a people or a bloodsucker meets a bloodsucker. Otherwise, people may be transformed into bloodsucker with probability p.Sooner or later(D days), all people will be turned into bloodsucker.Calculate the mathematical expectation of D.

Input

The number of test cases (T, T ≤ 100) is given in the first line of the input.Each case consists of an integer n and a float number p (1 ≤ n < 100000, 0 < p ≤ 1, accurate to 3 digits after decimal point), separated by spaces.

Output

For each case, you should output the expectation(3 digits after the decimal point) in a single line.


这题应该有2个思路,我是按吸血鬼数目来dp的,设dp[i] 表示 当前有i个吸血鬼,达到目标状态的期望值


则 dp[i] = C(1, i) * C(1, n - i) / C(2, n) * (p * dp[i+1] + (1-p)*dp[i]) + (1 - C(1, i) * C(1, n - i) / C(2, n)) * dp[i] + 1;


化简以后得 dp[i] = dp[i + 1] + (n - 1) * n / (2 * p * i * (n - i))

/*************************************************************************
    > File Name: zoj3551.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2014年12月22日 星期一 20时44分55秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 100010;
double dp[N];
double p;
int n;

double dfs(int i)
{
	if (i == n)
	{
		return 0;
	}
	if (dp[i] != -1)
	{
		return dp[i];
	}
	double pr = (double)n * (n - 1);
	pr /= (double)2 * p * i * (n - i);
//	printf("%f\n", pr);
	dp[i] = dfs(i + 1) + pr;
	return dp[i];
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%lf", &n, &p);
		for (int i = 0; i<= n; ++i)
		{
			dp[i] = -1;
		}
		dp[n] = 0.0;
		printf("%.3f\n", dfs(1));
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值