一共有n个人,每天都会有一个人又一定的几率变成吸血鬼,求所有人变成吸血鬼的期望
这题我只能说呵呵。。。。
推导公式dp[i]=(dp[i+1]+1)*p1+(dp[i]+1)*p2 变为:p1*dp[i]=dp[i+1]*p1+1 (p2=1-p1);
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define B(x) (1<<(x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned ui;
const int oo = 0x3f3f3f3f;
//const ll OO = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define lson rt<<1
#define rson rt<<1|1
void cmax(int& a, int b){ if (b > a)a = b; }
void cmin(int& a, int b){ if (b < a)a = b; }
void cmax(ll& a, ll b){ if (b > a)a = b; }
void cmin(ll& a, ll b){ if (b < a)a = b; }
void cmax(double& a, double b){ if (a - b < eps) a = b; }
void cmin(double& a, double b){ if (b - a < eps) a = b; }
void add(int& a, int b, int mod){ a = (a + b) % mod; }
void add(ll& a, ll b, ll mod){ a = (a + b) % mod; }
const ll MOD = 1000000007;
const int maxn = 100000 + 5;
double dp[maxn];
int main(){
int n, T;
double p, a, b, P;
scanf("%d", &T);
while (T--){
scanf("%d %lf", &n, &p);
dp[n] = 0.0;
for (int i = n - 1; i >= 1; i--){
b = 1.0 * n * (n - 1) / 2.0;
a = 1.0 * i * (n - i);
P = p * a / b;
dp[i] = (dp[i + 1] * P + 1) / P;
}
printf("%.3f\n", dp[1]);
}
return 0;
}