转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

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 (TT ≤ 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.

Sample Input

1
2 1

Sample Output

1.000

题意:已知有n-1个人,1个吸血鬼,每天n个中会随机有两个碰面,若是人和吸血鬼相遇,则人有p的概率变成吸血鬼,问最终全部变吸血鬼的期望天数

很明显,这是个概率dp题,dp[i]表示还剩i个人,那么dp[i] = dp[i+1] + 1 / P[i+1]

其中P[i] = p*C(i,1)*C(n-i ,1) / C(n,2)

/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype> using namespace std;
#define dep(X, R, L) for(int X=R;X>=L;X--) double dp[]; class TaskC {
public:
void solve(std::istream &in, std::ostream &out) {
int t;
in >> t;
while (t--) {
int n;
double p;
in >> n >> p;
dp[n - ] = ;
double tot = (double) n * (n - ) / / p;
dep(i, n - , ) {
dp[i] = dp[i + ] + tot / (i + ) / (n - i - );
}
out << fixed << setprecision() << dp[] << endl;
}
}
}; int main() {
std::ios::sync_with_stdio(false);
std::cin.tie();
TaskC solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return ;
}

ZOJ3551 Bloodsucker(概率dp)的更多相关文章

  1. zoj3551 Bloodsucker ——概率DP

    Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4530 A[i]数组表示当吸血鬼有 I 个的时候,还需要的天数.可以 ...

  2. zoj 3351 Bloodsucker(概率 dp)

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4530 dp[i]表示现在存在i个吸血鬼要达成目标(全为吸血鬼)天数的数学 ...

  3. ZOJ 3551 Bloodsucker <概率DP>

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3551 题意:开始有N-1个人和一个吸血鬼, 每天有两个生物见面,当人 ...

  4. Codeforces 28C [概率DP]

    /* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...

  5. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

  6. POJ 2096 Collecting Bugs (概率DP)

    题意:给定 n 类bug,和 s 个子系统,每天可以找出一个bug,求找出 n 类型的bug,并且 s 个都至少有一个的期望是多少. 析:应该是一个很简单的概率DP,dp[i][j] 表示已经从 j ...

  7. POJ 2151 Check the difficulty of problems (概率DP)

    题意:ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 ,求每队至少解出一题且冠军队至少解出N道题的概率. 析:概率DP,dp[i][j][k] 表示第 i 个队伍,前 j 个题,解出 ...

  8. 概率DP light oj 1030

    t组数据 n块黄金 到这里就捡起来 出发点1 到n结束  点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6  如果满6个的话 否则 ...

  9. hdu 4050 2011北京赛区网络赛K 概率dp ***

    题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...

随机推荐

  1. PHP几个防SQL注入攻击自带函数区别

    SQL注入攻击是黑客攻击网站最常用的手段.如果你的站点没有使用严格的用户输入检验,那么常容易遭到SQL注入攻击.SQL注入攻击通常通过给站点数据库提交不良的数据或查询语句来实现,很可能使数据库中的纪录 ...

  2. Codeforces 704D Captain America

    题意:平面上有n个点,每个点必须涂成红色和蓝色中的一种,花费各为r和b(对所有的点花费都一样).m条限制,每条限制形如"y=b这条直线上两种颜色的点的数目之差的绝对值不能超过c"或 ...

  3. 【C#】委托与事件

    一.委托 1.概念:用来存放 方法 指针(地址)的容器. 为什么要有委托?当有的业务代码总体已经实现,但有部分需要调用者来决定,就可以使用委托的方式,让调用者把一段代码以 方法的方式 传入. [例子] ...

  4. 关于Bayes网络新解

    经典贝叶斯网络 贝叶斯分类器的分类原理是通过某对象的先验概率,利用贝叶斯公式计算出其后验概率,即该对象属于某一类的概率,选择具有最大后验概率的类作为该对象所属的类.目前研究较多的贝叶斯分类器主要有四种 ...

  5. Storm测试报告

    http://blog.csdn.net/jmppok/article/details/17614431

  6. QT的文本加密方法(寒山居士)

    http://blog.csdn.net/esonpo/article/details/12746315http://blog.csdn.net/esonpo/article/details/1174 ...

  7. 几种开源SIP协议栈对比OPAL,VOCAL,sipX,ReSIProcate,oSIP

    随着VoIP和NGN技术的发展,H.323时代即将过渡到SIP时代,在H.323的开源协议栈中,Openh323占统治地位,它把一个复杂而又先进 的H.323协议栈展现在普通程序员的眼前,为H.323 ...

  8. 排序功能实现 jQuery实现排序 上移 下移

    效果 思路, 跟相邻元素,互换sort. 前提是每一个元素都有自己的sort值,不为零. <tr id="{sh:$vo.id}"> <td> <sp ...

  9. cf298F:状压dp+剪枝

    div2的F题,只想到了一个复杂度略高的dp,T了几次,后来加了剪枝减掉一些无用的状态终于过了.. 题意: 一个n*m的矩阵 (n<=5,m<=20),对格子进行黑白染色,已经给出了每行每 ...

  10. cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

      http://codeforces.com/contest/348/problem/B   B. Apple Tree time limit per test 2 seconds memory l ...