fork download
  1. // Coins
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. ios_base::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8.  
  9. int N;
  10. cin >> N;
  11.  
  12. vector<double> p(N);
  13. for (int i = 0; i < N; i++) {
  14. cin >> p[i];
  15. }
  16.  
  17.  
  18. vector<double> dp(N + 1, 0.0);
  19. dp[0] = 1.0; // 0 coins, 0 heads: probability 1
  20.  
  21. for (int i = 0; i < N; i++) {
  22.  
  23. for (int j = i + 1; j >= 1; j--) {
  24.  
  25. dp[j] = dp[j - 1] * p[i] + dp[j] * (1.0 - p[i]);
  26. }
  27. // j = 0: no heads means all tails
  28. dp[0] = dp[0] * (1.0 - p[i]);
  29. }
  30.  
  31.  
  32. int threshold = (N + 1) / 2;
  33. double ans = 0.0;
  34. for (int j = threshold; j <= N; j++) {
  35. ans += dp[j];
  36. }
  37.  
  38.  
  39. cout << fixed << setprecision(12) << ans << "\n";
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
0.000000000000