您的位置:首页 > 其它

zoj 3551 Bloodsucker

2011-11-10 10:53 190 查看
In0thday,therearen-1peopleand1bloodsucker.Everyday,twoandonlytwoofthemmeet.Nothingwillhappeniftheyareofthesamespecies,thatis,apeoplemeetsapeopleorabloodsuckermeetsabloodsucker.Otherwise,peoplemaybe
transformedintobloodsuckerwithprobabilityp.Soonerorlater(Ddays),allpeoplewillbeturnedintobloodsucker.CalculatethemathematicalexpectationofD.

Input

Thenumberoftestcases(T,T≤100)isgiveninthefirstlineoftheinput.Eachcaseconsistsofanintegernandafloatnumberp(1≤n<100000,0<p≤1,accurateto3digitsafterdecimal
point),separatedbyspaces.

Output

Foreachcase,youshouldoutputtheexpectation(3digitsafterthedecimalpoint)inasingleline.

SampleInput

1

21

SampleOutput

1.000



一道概率题,可以这样考虑:

根据题意,每天只能增加一个Bloodsucker,也就是说n-1个正常人是一个接着一个变成Bloodsucker的,那么所有正常人变成Bloodsucker的天数期望等于所有正常人变成Bloodsucker的天数期望之和。考虑n-1个正常人中第i个变成Bloodsucker的正常人的转变概率为:

P[i]=i*(n-i)/(n*(n-1)/2)

则天数期望为(直接从概率的定义考虑):

D[i]=1/P[i]

于是:

[align=center]D=sum(D[i])[/align][align=center][/align][align=center][/align][align=center][/align]

代码如下

#include<stdio.h>

intmain(){

intT;

intn;

doublep;

scanf("%d",&T);

while(T--){

scanf("%d%lf",&n,&p);

doubleres=0;

doubletmp=n*0.5*(n-1)/p;

inti;

for(i=1;i<=(n-1)/2;i++){

res+=tmp/i/(n-i);

}

res*=2;

res+=(n%2==0)?(4*tmp/n/n):0;

printf("%.3lf\n",res);

}

return0;

}

[/code]
原文链接http://blog.chinaunix.net/space.php?uid=26372629&do=blog&id=2996052


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: