Ref : http://www.clanfei.com/2012/04/772.html
可推出公式:
ans=sigma[C(N1-1,i)*0.5^(N1+i)] (0<=i<=N2-1)
注意double相乘会掉精度导致WA,所以要用黑科技:
log(a*b)=log(a)+log(b)
10^(log(a))=a
这样就把乘法转成加法辣~
1 #include<iostream> 2 #include<cmath> 3 #include<cstdio> 4 using namespace std; 5 6 double ans,tmp; 7 int N1,N2,HP1,HP2,AP1,AP2; 8 9 int main() 10 { 11 while(cin>>HP1>>HP2>>AP1>>AP2) 12 { 13 N2=HP1/AP2; if(HP1%AP2!=0) N2++; 14 N1=HP2/AP1; if(HP2%AP1!=0) N1++; 15 16 ans=pow(0.5,N1); 17 //tmp=1.00; 18 tmp=0; 19 for (int i=1;i<=N2-1;i++) 20 { 21 //tmp=tmp*(N1-i)/i; 22 //ans+=tmp*pow(0.5,N1+i); 23 tmp+=log10(N1-1.0+i)-log10(i*1.0); 24 ans+=pow(10,tmp+(N1+i)*log10(0.5)); 25 } 26 ans=ans*100; 27 printf("%.4f ",ans); 28 } 29 }