# ZOJ 3551 Bloodsucker [概率DP]

Description

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.

N<=10w

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
template <class T>
bool scanff(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

double dp[100100];
int main(){
tdata{
int n;
double p;
scanff(n);
scanff(p);
dp[1]=0.0;
double tot=double(n)*double(n-1)/2.0;
rep(i,2,n){
double b=double(i-1);
double a=double(n-i+1);
double tp=p*a*b/tot;
dp[i]=dp[i-1]+1/tp;
}
printf("%.3f\n",dp[n]);
}
return 0;
}


• 本文已收录于以下专栏：

## ZOJ3551:Bloodsucker(概率DP)

n 0th day, there are n-1 people and 1 bloodsucker. Every day, two and only two of them meet. Nothing...

## zoj3551---Bloodsucker

Bloodsucker Time Limit: 2 Seconds      Memory Limit: 65536 KB In 0th day, there are n-1 peo...

## ZOJ 2949 Coins of Luck（概率dp）

【链接】 【题意】给一个图，图中数值为炸弹的威力k（为0则该格没有炸弹），每个炸弹爆炸会引爆其【左侧k个格子】和【上方全部格子】，问最少手动引爆几个炸弹能使所有炸弹都炸 【思路】 一个炸弹只能引...

## zoj 3329 One Person Game（概率dp，期望）

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There i...

## zoj 3735 概率dp

Josephina and RPG Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge ...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)