Bloodsucker
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.
Sample Input
1 2 1
Sample Output
1.000
题意:
开始有一个吸血鬼,n-1个平民百姓。每天一个百姓被感染的概率可求,问每个人都变成吸血鬼的天数期望。
思路:
一般期望题逆推,设dp[i]是目前已经有i个吸血鬼,所有人变成吸血鬼的期望。则dp[n]=0;答案是dp[1];每一个dp[i]的感染概率可求是p[]=2.0*(n-i)*i/(n-1)/n*p;
则可得递推公式: dp[i]=dp[i+1]+1/p[];
代码:
1 #include"bits/stdc++.h" 2 3 #define db double 4 #define ll long long 5 #define vl vector<ll> 6 #define ci(x) scanf("%d",&x) 7 #define cd(x) scanf("%lf",&x) 8 #define cl(x) scanf("%lld",&x) 9 #define pi(x) printf("%d ",x) 10 #define pd(x) printf("%f ",x) 11 #define pl(x) printf("%lld ",x) 12 #define rep(i, n) for(int i=0;i<n;i++) 13 using namespace std; 14 const int N = 1e6 + 5; 15 const int mod = 1e9 + 7; 16 const int MOD = 998244353; 17 const db PI = acos(-1.0); 18 const db eps = 1e-10; 19 const ll INF = 0x3fffffffffffffff; 20 21 db dp[N],P[N]; 22 int t,n,m; 23 db p; 24 int main() 25 { 26 ci(t); 27 while(t--){ 28 ci(n),cd(p); 29 memset(dp,0, sizeof(dp)); 30 for(int i=1;i<n;i++) P[i]=2.0*p*i*(n-i)/n/(n-1);//感染概率 31 for(int i=n-1;i>=1;i--) dp[i]=dp[i+1]+1/P[i];//感染期望 32 printf("%.3f ",dp[1]); 33 } 34 return 0; 35 }