fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. double f(int a, int b){
  5. // (a+b)Ca / 2^(a+b)
  6. double z = 1;
  7. a += b;
  8. b = min(b,a-b);
  9. for(int i=0; i<b; i++){
  10. z *= a-i;
  11. z /= i+1;
  12. z /= 4;
  13. }
  14. return z/pow(2,a-2*b);
  15. }
  16.  
  17. double f(int l, int d, int h){
  18. // ldh == urw
  19. // (sum for all r 0 -> x-1) (l+d-2)C(d+r)
  20. // (divided by ) 2^(l+d-2)
  21. // (where ) x = min(h-d,l-1)
  22. double ans = 0;
  23. h = min(h-d,l-1);
  24. if(h<=0) return ans;
  25. double x = f(d,l-2);
  26. ans += x;
  27. double c = 0; // running error
  28. for(int i=1; i<h; i++){
  29. x *= l-1-i;
  30. x /= d+i;
  31. // ans += x; // following piece of code does the same, but with error correction
  32. double t = ans + x;
  33. if(abs(ans)>abs(x)) c += (ans-t) + x;
  34. else c += (x-t) + ans;
  35. ans = t;
  36. }
  37. return ans+c;
  38. }
  39.  
  40. int main() {
  41. int t,w,h,l,u,r,d;
  42. double x;
  43. cin>>t;
  44. for(int i=1; i<=t; i++){
  45. cin>>w>>h>>l>>u>>r>>d;
  46. x = f(l,d,h) + f(u,r,w);
  47. cout<<"Case #"<<i<<": "<<x<<endl;
  48. }
  49. return 0;
  50. }
Success #stdin #stdout 0s 4348KB
stdin
4
3 3 2 2 2 2
5 3 1 2 4 2
1 10 1 3 1 5
6 4 1 3 3 4
stdout
Case #1: 0.5
Case #2: 0.0625
Case #3: 0
Case #4: 0.3125