fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long dp[6005], ndp[6005];
  5.  
  6. class BeatTheStar {
  7. public:
  8. double doesItMatter( int n, int g )
  9. {
  10. long long sum = n*(n+1)/2;
  11.  
  12. for(int j=0;j<6005;j++)
  13. dp[j] = ndp[j] = 0;
  14.  
  15. dp[0] = 1;
  16. for(int num=1;num<=n;num++)
  17. {
  18. // only consider elements other than g
  19. if( num == g )
  20. continue;
  21.  
  22. // transitions ( knapsack )
  23. for(int s=0;s+num<=sum;s++)
  24. {
  25. // don't take num
  26. ndp[s] += dp[s];
  27.  
  28. // take num
  29. ndp[num+s] += dp[s];
  30. }
  31.  
  32. // copy new layer to old layer, and reset new layer
  33. for(int j=0;j<6005;j++)
  34. {
  35. dp[j] = ndp[j];
  36. ndp[j] = 0;
  37. }
  38. }
  39.  
  40. long double ans = 0;
  41. for(int s=0;s<=sum;s++)
  42. {
  43. // if s is the sum of a good subset
  44. if( 2*s < sum && 2*(s + g) > sum )
  45. {
  46. // count ways of making s, without using g
  47. ans += dp[s];
  48. }
  49. }
  50.  
  51. // divide by total outcomes ( number of subsets of 1..n without g )
  52. ans /= pow(2,n-1);
  53.  
  54. return ans;
  55. }
  56. };
  57.  
  58. int main()
  59. {
  60. BeatTheStar b;
  61.  
  62. int test;
  63. cin>>test;
  64. for(int i=0;i<test;i++)
  65. {
  66. int n,g;
  67. cin>>n>>g;
  68. cout<<fixed<<setprecision(4)<<b.doesItMatter(n,g)<<"\n";
  69. }
  70.  
  71. return 0;
  72. }
Success #stdin #stdout 0s 4368KB
stdin
6
2 1
5 5
5 1
5 2
9 7
98 98
stdout
0.0000
0.6250
0.1250
0.1250
0.3281
0.0000