转载请注明出处: 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. 自己动手开发编译器(四)利用DFA转换表建立扫描器

    上回我们介绍了两种有穷自动机模型——确定性有穷自动机DFA和非确定性有穷自动机,以及从正则表达式经过NFA最终转化为DFA的算法.有些同学表示还是难以理解NFA到底怎么转化为DFA.所以本篇开头时我想 ...

  2. css、js的相互阻塞

    先决条件:脚本前面存在外部样式 以下试验虽然是在chrome下,但是对于IE8+以及其他浏览器也适用. 1.内联脚本(http://jsbin.com/mudab/1) <!DOCTYPE ht ...

  3. 转:MFC 的程序中GetAt()的理解

    BYTE 是 1字节长度 的 整型, int 是 4 字节长度 的 整型. CString::GetAt(0); 就是把 一个 CString 对象里 的字符串 中的第一个字符,把它的ASCII 值 ...

  4. C#的Reflection总结

    什么是反射 在.NET中的反射也可以实现从对象的外部来了解对象(或程序集)内部结构的功能,哪怕你不知道这个对象(或程序集)是个什么东西,另外.NET中的反射还可以运态创建出对象并执行它其中的方法. 反 ...

  5. LED限流电阻的大小计算

    很多时候电路中都用LED做指示,这就涉及到限流电阻,这个怎么选取呢? 可以举个例子,贴片蓝色LED datasheet上参数如下 要注意理解LED Datasheet上的参数.最重要的三个参数如下: ...

  6. IBM Cognos 10 启动报错

    报错信息: 15:35:02, 'LogService', 'StartService', 'Success'. 15:35:03, CAF input validation enabled. 15: ...

  7. HDOJ 1070 Milk(水题,考英文的)

    Problem Description Ignatius drinks milk everyday, now he is in the supermarket and he wants to choo ...

  8. HDOJ 1236 排名(练耐心题)

    Problem Description 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 每题的分值,所以并不是最后的排名.给定录取分数线,请你写程序找出最后通 ...

  9. 探讨c#中的unchecked是什么意思,起什么作用?

    Checked与Unchecked     对于因为整数类型参与算术操作和类型转换时产生的“溢出异常”——System.OverflowException,在某些算法来讲不算真正的“异常”,相反这种溢 ...

  10. css链接

    css code: a:link{ color:#FF0000; } a:visited{ color:#00FF00; } a:hover { color:#0000FF; } a:active{ ...