prithivMLmods/Coder-Stat huggingface dataset:
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define N 1005
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define piii pair<int, pii>
#define print(x) cout << #x << "=" << x << "\t";
#define endl "\n"
#define newline cout << endl;

int n;
double p[N];
double dp[N][N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  dp[1][0] = 1 - p[1];
  dp[1][1] = p[1];
  for (int i = 2; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1 - p[i]);
    for (int j = 1; j <= i; j++)
      dp[i][j] = dp[i - 1][j - 1] * p[i] + dp[i - 1][j] * (1 - p[i]);
  }
  double ans = 0;
  for (int j = (n + 1) / 2; j <= n; j++)
    ans += dp[n][j];
  cout << fixed << setprecision(12) << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define N 3005
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define piii pair<int, pii>
#define print(x) cout << #x << "=" << x << "\t";
#define endl "\n"
#define newline cout << endl;

int n;
double p[N];
double dp[N][N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  dp[1][0] = 1 - p[1];
  dp[1][1] = p[1];
  for (int i = 2; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1 - p[i]);
    for (int j = 1; j <= i; j++)
      dp[i][j] = dp[i - 1][j - 1] * p[i] + dp[i - 1][j] * (1 - p[i]);
  }
  double ans = 0;
  for (int j = (n + 1) / 2; j <= n; j++)
    ans += dp[n][j];
  cout << fixed << setprecision(12) << ans << endl;
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<double> p;
  p.resize(n + 1);
  int i, j, k;
  for (i = 1; i <= n; i++)
    cin >> p[i];
  vector<vector<double>> prob;
  prob.resize(n + 1);
  for (i = 0; i <= n; i++)
    prob[i].resize(n);
  prob[1][1] = p[1];
  prob[1][0] = 1.0 - p[1];

  for (i = 2; i <= n; i++) {
    prob[i][0] = (1.0 - p[i]) * prob[i - 1][0];
    for (j = 1; j <= i; j++) {
      prob[i][j] = prob[i - 1][j] * (1.0 - p[i]) + prob[i - 1][j - 1] * p[i];
    }
  }
  double ans = 0.0;
  int lim = (n + 1) / 2;
  for (i = lim; i <= n; i++)
    ans = ans + prob[n][i];
  cout << setprecision(12) << ans << endl;
  return 0;
}

Fixed submission:
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<double> p;
  p.resize(n + 1);
  int i, j, k;
  for (i = 1; i <= n; i++)
    cin >> p[i];
  vector<vector<double>> prob;
  prob.resize(n + 1);
  for (i = 0; i <= n; i++)
    prob[i].resize(n + 1);
  prob[1][1] = p[1];
  prob[1][0] = 1.0 - p[1];

  for (i = 2; i <= n; i++) {
    prob[i][0] = (1.0 - p[i]) * prob[i - 1][0];
    for (j = 1; j <= i; j++) {
      prob[i][j] = prob[i - 1][j] * (1.0 - p[i]) + prob[i - 1][j - 1] * p[i];
    }
  }
  double ans = 0.0;
  int lim = (n + 1) / 2;
  for (i = lim; i <= n; i++)
    ans = ans + prob[n][i];
  cout << setprecision(12) << ans << endl;
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define fio()                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define MOD 1000000007
#define nl '\n'
#define pb push_back
#define mp make_pair

using namespace std;
const int N = 1005;
ld dp[N][N];
int32_t main() {
  fio();
  int n;
  cin >> n;
  ld p[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1 - p[i]);
    dp[0][i] = 0;
  }
  cout << fixed << setprecision(15);
  ld ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      dp[i][j] += dp[i - 1][j] * (1 - p[i]);
      dp[i][j] += dp[i - 1][j - 1] * p[i];
    }
  }
  /*for(int i=0;i<=n;i++)
  {
          for(int j=0;j<=n;j++)
                  cout<<dp[i][j]<<" ";
          cout<<endl;
  }*/
  for (int i = n / 2 + 1; i <= n; i++)
    ans += dp[n][i];
  cout << ans << endl;
  // cout<<dp[n][m];
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define fio()                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define MOD 1000000007
#define nl '\n'
#define pb push_back
#define mp make_pair

using namespace std;
const int N = 3005;
ld dp[N][N];
int32_t main() {
  fio();
  int n;
  cin >> n;
  ld p[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1 - p[i]);
    dp[0][i] = 0;
  }
  cout << fixed << setprecision(15);
  ld ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      dp[i][j] += dp[i - 1][j] * (1 - p[i]);
      dp[i][j] += dp[i - 1][j - 1] * p[i];
    }
  }
  /*for(int i=0;i<=n;i++)
  {
          for(int j=0;j<=n;j++)
                  cout<<dp[i][j]<<" ";
          cout<<endl;
  }*/
  for (int i = n / 2 + 1; i <= n; i++)
    ans += dp[n][i];
  cout << ans << endl;
  // cout<<dp[n][m];
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 INF = 1LL << 59;
const int mod = 1e9 + 7;

int N;
double P[3000];
double dp[6002][3000];
bool memo[6002][3000];

double rec(int up, int idx) {
  if (idx == N)
    return up > 3000 ? 1.0 : 0.0;
  if (exchange(memo[up][idx], true))
    return dp[up][idx];
  double ret = 0.0;
  ret += rec(up + 1, idx + 1) * P[idx];
  ret += rec(up - 1, idx + 1) * (1 - P[idx]);
  return dp[up][idx] = ret;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> P[i];
  cout << fixed << setprecision(10) << rec(2000, 0) << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 INF = 1LL << 59;
const int mod = 1e9 + 7;

int N;
double P[3000];
double dp[6002][3000];
bool memo[6002][3000];

double rec(int up, int idx) {
  if (idx == N)
    return up > 3000 ? 1.0 : 0.0;
  if (exchange(memo[up][idx], true))
    return dp[up][idx];
  double ret = 0.0;
  ret += rec(up + 1, idx + 1) * P[idx];
  ret += rec(up - 1, idx + 1) * (1 - P[idx]);
  return dp[up][idx] = ret;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> P[i];
  cout << fixed << setprecision(10) << rec(3000, 0) << endl;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
// I - Coins
#include <bits/stdc++.h>
using namespace std;
using dl = double;

int main() {
  int N;
  cin >> N;

  vector<dl> P(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> P[i];

  int C = (N + 1) / 2; // ceil
  vector<dl> dp(N + 2);
  dp[1] = 1.;
  for (int lv = 1; lv <= N; lv++) {
    dl f = P[lv], b = 1. - f;
    for (int i = lv + 1; i >= lv + 2 - C; i--)
      dp[i] = b * dp[i] + f * dp[i - 1];
  }

  dl a = 0;
  for (int i = N + 1; i > C; i--)
    a += dp[i];
  printf("%.10g\n", a);
}

Fixed submission:
// I - Coins
#include <bits/stdc++.h>
using namespace std;
using dl = double;

int main() {
  int N;
  cin >> N;

  vector<dl> P(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> P[i];

  int C = (N + 1) / 2; // ceil
  vector<dl> dp(N + 2);
  dp[1] = 1.;
  for (int lv = 1; lv <= N; lv++) {
    dl f = P[lv], b = 1. - f;
    int e = max(1, lv - C + 2);
    for (int i = lv + 1; i >= e; i--)
      dp[i] = b * dp[i] + f * dp[i - 1];
  }

  dl a = 0;
  for (int i = N + 1; i > C; i--)
    a += dp[i];
  printf("%.10g\n", a);
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define debug1(x) cout << #x << " " << x << endl;
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \
       << endl;
#define pii pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define ll long long int
int main() {

  int n;
  cin >> n;
  double a[3000];
  int i, j;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  double dp[1000][1000];
  dp[1][0] = 1 - a[1];
  dp[1][1] = a[1];
  for (i = 2; i <= n; i++)
    dp[i][0] = dp[i - 1][0] * (1 - a[i]);
  for (i = 2; i <= n; i++)
    dp[i][i] = dp[i - 1][i - 1] * a[i];
  for (i = 2; i <= n; i++)
    for (j = 1; j < i; j++) {
      dp[i][j] = dp[i - 1][j - 1] * a[i] + dp[i - 1][j] * (1 - a[i]);
    }
  long double ans = 0;
  for (j = n / 2 + 1; j <= n; j++)
    ans += dp[n][j];
  cout << fixed << setprecision(9);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define debug1(x) cout << #x << " " << x << endl;
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \
       << endl;
#define pii pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define ll long long int
int main() {

  int n;
  cin >> n;
  double a[3000];
  int i, j;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  double dp[3000][3000];
  dp[1][0] = 1 - a[1];
  dp[1][1] = a[1];
  for (i = 2; i <= n; i++)
    dp[i][0] = dp[i - 1][0] * (1 - a[i]);
  for (i = 2; i <= n; i++)
    dp[i][i] = dp[i - 1][i - 1] * a[i];
  for (i = 2; i <= n; i++)
    for (j = 1; j < i; j++) {
      dp[i][j] = dp[i - 1][j - 1] * a[i] + dp[i - 1][j] * (1 - a[i]);
    }
  long double ans = 0;
  for (j = n / 2 + 1; j <= n; j++)
    ans += dp[n][j];
  cout << fixed << setprecision(9);
  cout << ans << endl;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;
#define REP(i, s, n) for (int i = s, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T gcd(T a, T b) {
  if (a < b)
    gcd(b, a);
  if (b == 1)
    return 1;
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

#define MOD (1'000'000'000 + 7)

bool _less(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }

double dp[3000][3000];

int main() {
  int N;
  cin >> N;
  vector<double> p(N);
  REP(i, 1, N + 1) { cin >> p[i]; }

  dp[0][0] = 1.0;
  REP(i, 1, N + 1) {
    dp[i][0] = dp[i - 1][0] * (1 - p[i]);
    REP(j, 1, i + 1) {
      dp[i][j] = dp[i - 1][j - 1] * p[i] + dp[i - 1][j] * (1 - p[i]);
    }
  }

  double sum = 0.0;
  REP(i, N / 2 + 1, N + 1) { sum += dp[N][i]; }
  cout << setprecision(10) << sum << endl;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;
#define REP(i, s, n) for (int i = s, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T gcd(T a, T b) {
  if (a < b)
    gcd(b, a);
  if (b == 1)
    return 1;
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

#define MOD (1'000'000'000 + 7)

bool _less(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }

double dp[3000][3000];

int main() {
  int N;
  cin >> N;
  vector<double> p(N + 1);
  REP(i, 1, N + 1) { cin >> p[i]; }

  dp[0][0] = 1.0;
  REP(i, 1, N + 1) {
    dp[i][0] = dp[i - 1][0] * (1 - p[i]);
    REP(j, 1, i + 1) {
      dp[i][j] = dp[i - 1][j - 1] * p[i] + dp[i - 1][j] * (1 - p[i]);
    }
  }

  double sum = 0.0;
  REP(i, N / 2 + 1, N + 1) { sum += dp[N][i]; }
  cout << setprecision(10) << sum << endl;
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, n) for (ll i = x; i < n; i++)
#define pb push_back
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define all(s) s.begin(), s.end()
#define fuck_you_scanf                                                         \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define si size()

const int N = 1e6 + 5;
double arr[N];
double DP[3005][3005][2];

double f(int idx, int reqd, int n) {
  if (reqd <= 0)
    return 1;
  if (idx == n) {
    return 0;
  }
  int type = 0;
  if (reqd < 0)
    type = 1;
  if (DP[idx][abs(reqd)][type] > 0)
    return DP[idx][abs(reqd)][type];
  double ans1 = (arr[idx]) * f(idx + 1, reqd - 1, n) * 1.0;
  double ans2 = (1.0 - arr[idx]) * f(idx + 1, reqd, n) * 1.0;
  return DP[idx][abs(reqd)][type] = ans1 + ans2;
}

int32_t main() {
  int n;
  cin >> n;
  memset(DP, -1, sizeof(DP));
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int reqd = (n + 1) / 2;
  if (n % 2 == 0)
    reqd++;
  cout << setprecision(10) << fixed;
  cout << f(0, reqd, n);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, n) for (ll i = x; i < n; i++)
#define pb push_back
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define all(s) s.begin(), s.end()
#define fuck_you_scanf                                                         \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define si size()

const int N = 1e6 + 5;
double arr[N];
double DP[3005][3005][2];

double f(int idx, int reqd, int n) {
  if (reqd <= 0)
    return 1;
  if (idx == n) {
    return 0;
  }
  int type = 0;
  if (reqd < 0)
    type = 1;
  if (DP[idx][abs(reqd)][type] >= 0)
    return DP[idx][abs(reqd)][type];
  double ans1 = (arr[idx]) * f(idx + 1, reqd - 1, n) * 1.0;
  double ans2 = (1.0 - arr[idx]) * f(idx + 1, reqd, n) * 1.0;
  return DP[idx][abs(reqd)][type] = ans1 + ans2;
}

int32_t main() {
  int n;
  cin >> n;
  memset(DP, -1, sizeof(DP));
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int reqd = (n + 1) / 2;
  if (n % 2 == 0)
    reqd++;
  cout << setprecision(10) << fixed;
  cout << f(0, reqd, n);
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cerr << #v << ":";                                                           \
  rep(i, v.size()) cerr << " " << v[i];                                        \
  cerr << endl
using namespace std;

int MOD = 1000000007;
double dp[3010][3010];
double p[3010];
signed main() {
  ll n;
  cin >> n;
  rep(i, n) cin >> p[i];

  rep(i, n) dp[0][i] = 0;
  dp[0][0] = 1.0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (j == 0) {
        dp[i][j] = dp[i - 1][j] * (1 - p[i - 1]);
      } else {
        dp[i][j] = dp[i - 1][j] * (1 - p[i - 1]) + dp[i - 1][j - 1] * p[i - 1];
      }
    }
  }
  double ans = 0.0;

  rep(i, n / 2 + 1) ans += dp[n][i];
  printf("%0.10lf\n", 1.0 - ans);

  rep(i, n) {
    rep(j, n) { cerr << dp[i][j] << "\t"; }
    cerr << endl;
  }

  return 0;
}

// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
// https://cf17-final-open.contest.atcoder.jp/tasks/cf17_final_a rm -r -f
// test;oj dl http://dp.contest.atcoder.jp/tasks/dp_f

Fixed submission:
#include "bits/stdc++.h"

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cerr << #v << ":";                                                           \
  rep(i, v.size()) cerr << " " << v[i];                                        \
  cerr << endl
using namespace std;

int MOD = 1000000007;
double dp[3010][3010];
double p[3010];
signed main() {
  ll n;
  cin >> n;
  rep(i, n) cin >> p[i];

  rep(i, n) dp[0][i] = 0;
  dp[0][0] = 1.0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (j == 0) {
        dp[i][j] = dp[i - 1][j] * (1 - p[i - 1]);
      } else {
        dp[i][j] = dp[i - 1][j] * (1 - p[i - 1]) + dp[i - 1][j - 1] * p[i - 1];
      }
    }
  }
  double ans = 0.0;

  rep(i, n / 2 + 1) ans += dp[n][i];
  printf("%0.10lf\n", 1.0 - ans);

  // rep(i,n){
  //     rep(j,n){
  //         cerr << dp[i][j] << "\t";
  //     }cerr << endl;
  // }

  return 0;
}

// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
// https://cf17-final-open.contest.atcoder.jp/tasks/cf17_final_a rm -r -f
// test;oj dl http://dp.contest.atcoder.jp/tasks/dp_f
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>
using namespace std;

double dp[3003][3003];

void solve() {
  int n;
  cin >> n;
  double data[n + 5], ans = 0;
  for (int i = 1; i <= n; i++)
    cin >> data[i];

  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1.0 - data[i]);
    for (int j = 1; j <= n; j++) {
      dp[i][j] = dp[i - 1][j - 1] * data[i] + dp[i - 1][j] * (1 - data[i]);
    }
  }

  for (int i = 1; i <= n; i++) {
    if (2 * i < n)
      continue;
    ans += dp[n][i];
  }
  cout << setprecision(12) << ans << '\n';
}

signed main() {
  int t = 1; // cin >> t;
  while (t--) {
    solve();
  }
  // cout << "Time-Taken: " << ((t2 - t1) / (double)CLOCKS_PER_SEC) << endl;
}


Fixed submission:
// #pragma GCC optimize("O3", "unroll-loops")
// #pragma GCC target("avx2")

#include <bits/stdc++.h>
using namespace std;

double dp[3003][3003];

void solve() {
  int n;
  cin >> n;
  double data[n + 5], ans = 0;
  for (int i = 1; i <= n; i++)
    cin >> data[i];

  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1.0 - data[i]);
    for (int j = 1; j <= n; j++) {
      dp[i][j] = dp[i - 1][j - 1] * data[i] + dp[i - 1][j] * (1 - data[i]);
    }
  }

  for (int i = 1; i <= n; i++) {
    if (2 * i < n)
      continue;
    ans += dp[n][i];
  }
  cout << setprecision(12) << ans << '\n';
}

signed main() {
  int t = 1; // cin >> t;
  while (t--) {
    solve();
  }
  // cout << "Time-Taken: " << ((t2 - t1) / (double)CLOCKS_PER_SEC) << endl;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define MOD 1000000007
typedef long long int ll;

long double dp[3001][3001] = {0};
long double p[3001] = {0};

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cout << setprecision(20) << fixed;

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }

  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * ((long double)1 - p[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] =
          dp[i - 1][j] * ((long double)1 - p[i]) + dp[i - 1][j - 1] * p[i];
    }
  }
  long double ans = 0;
  for (int i = n / 2 + 1; i <= n; i++) {
    ans += dp[n][i];
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define MOD 1000000007
typedef long long int ll;

long double dp[3001][3001] = {0};
long double p[3001] = {0};

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cout << setprecision(20) << fixed;

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }

  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * ((long double)1 - p[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] =
          dp[i - 1][j] * ((long double)1 - p[i]) + dp[i - 1][j - 1] * p[i];
    }
  }
  long double ans = 0;
  for (int i = n / 2 + 1; i <= n; i++) {
    ans += dp[n][i];
  }
  cout << ans << endl;

  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long int

double dp[3002][3002];

double solve(vector<double> &v, int n, int x) {
  // 0 heads required always 1
  if (x == 0)
    return 1;
  // for (0,0) it will be 1 only

  // Can't get the desired no. of heads in 0 tosses
  if (n == 0)
    return 0;

  if (dp[n][x] > -0.9)
    return dp[n][x];

  dp[n][x] = v[n] * solve(v, n - 1, x - 1) + (1 - v[n]) * solve(v, n - 1, x);
  // cout<<dp[n][x]<<endl;
  return dp[n][x];
}

int main() {

  FASTIO;

  int n;
  cin >> n;
  vector<double> v(n);

  memset(dp, -1, sizeof dp);

  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }

  // out of n tosses we want atleast (n+1)/2 heads
  double ans = solve(v, n, (n + 1) / 2);

  cout << fixed << setprecision(10) << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long int

double dp[3002][3002];

double solve(vector<double> &v, int n, int x) {
  // 0 heads required always 1
  if (x == 0)
    return 1;
  // for (0,0) it will be 1 only

  // Can't get the desired no. of heads in 0 tosses
  if (n == 0)
    return 0;

  if (dp[n][x] > -0.9)
    return dp[n][x];

  dp[n][x] = v[n] * solve(v, n - 1, x - 1) + (1 - v[n]) * solve(v, n - 1, x);
  // cout<<dp[n][x]<<endl;
  return dp[n][x];
}

int main() {

  FASTIO;

  int n;
  cin >> n;
  vector<double> v(n + 1);

  memset(dp, -1, sizeof dp);

  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }

  // out of n tosses we want atleast (n+1)/2 heads
  double ans = solve(v, n, (n + 1) / 2);

  cout << fixed << setprecision(10) << ans;
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

double mem[3000][3000];
int vis[3000][3000];
int n;
double arr[3000];

double go(int x, int y) {
  if (x >= n)
    return (y > n - y);
  if (vis[x][y])
    return mem[x][y];
  return mem[x][y] = (arr[x] * go(x + 1, y + 1) + (1 - arr[x]) * go(x + 1, y));
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << fixed << setprecision(15) << go(0, 0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

double mem[3000][3000];
int vis[3000][3000];
int n;
double arr[3000];

double go(int x, int y) {
  if (x >= n)
    return (y > n - y);
  if (vis[x][y])
    return mem[x][y];
  vis[x][y] = 1;
  return mem[x][y] = (arr[x] * go(x + 1, y + 1) + (1 - arr[x]) * go(x + 1, y));
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << fixed << setprecision(15) << go(0, 0) << endl;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
using namespace std;
ll h;
ll w;
double dp[1000][1000];
int main() {
  ll n;
  cin >> n;
  double a[40000];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  dp[0][0] = 1.0;

  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j <= i; j++) {
      if (j == 0) {
        dp[i][j] = dp[i - 1][j] * (1 - a[i]);
      }
      dp[i][j] = dp[i - 1][j] * (1 - a[i]) + dp[i - 1][j - 1] * a[i];
    }
  }
  double ans = 0;
  for (ll i = (n + 1) / 2; i <= n; i++) {
    ans += dp[n][i];
  }
  cout << fixed << setprecision(10) << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
using namespace std;
ll h;
ll w;
double dp[3000][3000];
int main() {
  ll n;
  cin >> n;
  double a[40000];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  dp[0][0] = 1.0;

  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j <= i; j++) {
      if (j == 0) {
        dp[i][j] = dp[i - 1][j] * (1 - a[i]);
      }
      dp[i][j] = dp[i - 1][j] * (1 - a[i]) + dp[i - 1][j - 1] * a[i];
    }
  }
  double ans = 0;
  for (ll i = (n + 1) / 2; i <= n; i++) {
    ans += dp[n][i];
  }
  cout << fixed << setprecision(10) << ans;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, l, r) for (int i = (int)l; i <= (int)r; ++i)
#define REP(i, r) for (int i = (int)0; i < (int)r; ++i)

using namespace std;
typedef long long ll;
const int Nmax = 1e3 + 11;

int n;
double p[Nmax], f[Nmax][Nmax], ans;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  FOR(i, 1, n) { cin >> p[i]; }
  f[1][1] = p[1];
  f[1][0] = 1.0 - p[1];
  if (n == 1) {
    printf("%0.9f", p[1]);
    return 0;
  }
  FOR(i, 2, n) FOR(j, 0, i) {
    f[i][j] = f[i - 1][j] * (1.0 - p[i]);
    if (j > 0)
      f[i][j] += f[i - 1][j - 1] * p[i];
    if (i == n && j > n / 2) {
      ans += f[i][j];
    }
  }
  printf("%0.10f", ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, l, r) for (int i = (int)l; i <= (int)r; ++i)
#define REP(i, r) for (int i = (int)0; i < (int)r; ++i)

using namespace std;
typedef long long ll;
const int Nmax = 3e3 + 11;

int n;
double p[Nmax], f[Nmax][Nmax], ans;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  FOR(i, 1, n) { cin >> p[i]; }
  f[1][1] = p[1];
  f[1][0] = 1.0 - p[1];
  if (n == 1) {
    printf("%0.9f", p[1]);
    return 0;
  }
  FOR(i, 2, n) FOR(j, 0, i) {
    f[i][j] = f[i - 1][j] * (1.0 - p[i]);
    if (j > 0)
      f[i][j] += f[i - 1][j - 1] * p[i];
    if (i == n && j > n / 2) {
      ans += f[i][j];
    }
  }
  printf("%0.10f", ans);
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
double dp[3000][3000];
int main() {
  int n;
  scanf("%d", &n);
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    double p;
    scanf("%lf", &p);
    dp[i][0] = dp[i - 1][0] * (1 - p);
    for (int j = 1; j < i; j++)
      dp[i][j] = dp[i - 1][j - 1] * p + dp[i - 1][j] * (1 - p);
    dp[i][i] = dp[i - 1][i - 1] * p;
  }
  double ans = 0;
  for (int i = n; i > n / 2; i++)
    ans += dp[n][i];
  printf("%.10lf", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
double dp[3000][3000];
int main() {
  int n;
  scanf("%d", &n);
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    double p;
    scanf("%lf", &p);
    dp[i][0] = dp[i - 1][0] * (1 - p);
    for (int j = 1; j < i; j++)
      dp[i][j] = dp[i - 1][j - 1] * p + dp[i - 1][j] * (1 - p);
    dp[i][i] = dp[i - 1][i - 1] * p;
  }
  double ans = 0;
  for (int i = n; i > n / 2; i--)
    ans += dp[n][i];
  printf("%.10lf", ans);
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
  int i, n, j;
  cin >> n;
  vector<double> p(n + 1);
  for (i = 1; i <= n; ++i)
    cin >> p[i];
  vector<double> dp(n + 1, 0);
  dp[0] = 1;
  for (i = 1; i <= n; ++i) {
    for (j = i; j >= 0; --j) {
      dp[j] = dp[j] * (1 - p[i]);
      if (j > 0)
        dp[j] += dp[j - 1] * p[i];
    }
    for (int k = 0; k <= n; k++)
      cerr << dp[k] << " ";
    cerr << "\n";
  }
  double ans = 0;
  for (i = n / 2 + 1; i <= n; ++i)
    ans += dp[i];
  cout << setprecision(10) << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
  int i, n, j;
  cin >> n;
  vector<double> p(n + 1);
  for (i = 1; i <= n; ++i)
    cin >> p[i];
  vector<double> dp(n + 1, 0);
  dp[0] = 1;
  for (i = 1; i <= n; ++i) {
    for (j = i; j >= 0; --j) {
      dp[j] = dp[j] * (1 - p[i]);
      if (j > 0)
        dp[j] += dp[j - 1] * p[i];
    }
    // for(int k=0; k<=n; k++)
    // cerr<<dp[k]<<" ";
    // cerr<<"\n";
  }
  double ans = 0;
  for (i = n / 2 + 1; i <= n; ++i)
    ans += dp[i];
  cout << setprecision(10) << ans << "\n";
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define ll long long
int n;
double a[3000];
double dp[3000][3000];

double func(int i, int kitne) {
  // base case
  if (kitne == 0) {
    return 1;
  }
  if (i == n) {
    return 0;
  }
  // recursive case
  if (dp[i][kitne] > -1.0) {
    return dp[i][kitne];
  }

  double case1 = a[i] * func(i + 1, kitne - 1);
  double case2 = (1 - a[i]) * func(i + 1, kitne);

  dp[i][kitne] = case1 + case2;

  return dp[i][kitne];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  memset(dp, -1.0, sizeof(dp));
  double ans = 0;
  cout << setprecision(10) << func(0, (n + 1) / 2);
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define ll long long
int n;
double a[3000];
double dp[3000][3000];

double func(int i, int kitne) {
  // base case
  if (kitne == 0) {
    return 1;
  }
  if (i == n) {
    return 0;
  }
  // recursive case
  if (dp[i][kitne] > -1.0) {
    return dp[i][kitne];
  }

  double case1 = a[i] * func(i + 1, kitne - 1);
  double case2 = (1 - a[i]) * func(i + 1, kitne);

  dp[i][kitne] = case1 + case2;

  return dp[i][kitne];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  memset(dp, -1.0, sizeof(dp));
  double ans = 0;
  cout << setprecision(10) << func(0, (n + 1) / 2);
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<double> p(N);
  for (int i = 0; i < N; ++i)
    cin >> p[i];

  // dp[i][head]: i-1 番目までのコインを投げたときに表が head 回出る確率
  double dp[301][301] = {};
  dp[0][0] = 1;
  for (int i = 0; i < N; ++i) {
    for (int head = 0; head < N; ++head) {
      dp[i + 1][head + 1] += dp[i][head] * p[i];
      dp[i + 1][head] += dp[i][head] * (1 - p[i]);
    }
  }

  double ans = 0;
  for (int i = (N + 1) / 2; i <= N; ++i)
    ans += dp[N][i];
  cout << fixed << setprecision(9) << ans << endl;

  return 0;
}

Fixed submission:
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<double> p(N);
  for (int i = 0; i < N; ++i)
    cin >> p[i];

  // dp[i][head]: i-1 番目までのコインを投げたときに表が head 回出る確率
  double dp[3001][3001] = {};
  dp[0][0] = 1;
  for (int i = 0; i < N; ++i) {
    for (int head = 0; head < N; ++head) {
      dp[i + 1][head + 1] += dp[i][head] * p[i];
      dp[i + 1][head] += dp[i][head] * (1 - p[i]);
    }
  }

  double ans = 0;
  for (int i = (N + 1) / 2; i <= N; ++i)
    ans += dp[N][i];
  cout << fixed << setprecision(9) << ans << endl;

  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define EPS 10e-9
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define st first
#define nd second
#define sz(v) int(v.size())
#define all(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = A; I < B; I++)
#define RFOR(I, A, B) for (int I = A; I >= B; I--)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<iii> viii;
typedef vector<ll> vll;

int n;
long double p[3000];
long double PD[3000][3000];
bool visited[3000][3000] = {false};

long double pd(int i, int heads) {
  if (i < 0) {
    return heads > n / 2 ? 1 : 0;
  }
  long double &ans = PD[i][heads];
  if (visited[i][heads]) {
    return ans;
  }
  ans = pd(i - 1, heads + 1) * p[i] + pd(i - 1, heads) * (1 - p[i]);
  return ans;
}

int main() {
  scanf("%d", &n);
  FOR(i, 0, n) { scanf("%Lf", p + i); }
  printf("%.10Lf\n", pd(n - 1, 0));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define EPS 10e-9
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define st first
#define nd second
#define sz(v) int(v.size())
#define all(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = A; I < B; I++)
#define RFOR(I, A, B) for (int I = A; I >= B; I--)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<iii> viii;
typedef vector<ll> vll;

int n;
long double p[3000];
long double PD[3000][3000];
bool visited[3000][3000] = {false};

long double pd(int i, int heads) {
  if (i < 0) {
    return heads > n / 2 ? 1 : 0;
  }
  long double &ans = PD[i][heads];
  if (visited[i][heads]) {
    return ans;
  }
  ans = pd(i - 1, heads + 1) * p[i] + pd(i - 1, heads) * (1 - p[i]);
  visited[i][heads] = true;
  return ans;
}

int main() {
  scanf("%d", &n);
  FOR(i, 0, n) { scanf("%Lf", p + i); }
  printf("%.10Lf\n", pd(n - 1, 0));
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int INF = 1145141919;
const long long INFL = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

double dp[3000][3000] = {};
vector<double> P;
int n;

double rec(int d, int x) {
  if (dp[d][x]) {
    return dp[d][x];
  }
  if (d == n) {
    if (n / 2 + n % 2 > x) {
      return 0;
    } else {
      return 1;
    }
  }
  double ret = 1;
  ret = rec(d + 1, x + 1) * P[d] + rec(d + 1, x) * (1 - P[d]);
  return dp[d][x] = ret;
}

int main() {
  cin >> n;
  double in;
  for (int i = 0; i < n; ++i) {
    cin >> in;
    P.push_back(in);
  }
  printf("%0.15lf\n", rec(0, 0));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int INF = 1145141919;
const long long INFL = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

double dp[3000][3000] = {};
vector<double> P;
int n;

double rec(int d, int x) {
  if (dp[d][x]) {
    return dp[d][x];
  }
  if (d == n) {
    if (n / 2 + n % 2 > x) {
      return 0;
    } else {
      return 1;
    }
  }
  double ret = 1;
  ret = rec(d + 1, x + 1) * P[d] + rec(d + 1, x) * (1 - P[d]);
  return dp[d][x] = ret;
}

int main() {
  cin >> n;
  double in;
  for (int i = 0; i < n; ++i) {
    cin >> in;
    P.push_back(in);
  }
  for (int i = n / 2 + 1; i <= n; ++i) {
    dp[n][i] = 1;
  }
  for (int i = n; i > 0; --i) {
    for (int j = n; j > 0; --j) {
      dp[i - 1][j - 1] = dp[i][j] * P[i - 1] + dp[i][j - 1] * (1 - P[i - 1]);
    }
  }
  printf("%0.15lf\n", dp[0][0]);
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <vector>

#define ll long long
const ll MOD = 1e9 + 7;

int main() {
  std::ios::sync_with_stdio(false);

  int N;
  std::cin >> N;
  std::vector<double> p(N);

  double dp[N][N];
  memset(dp, 0, sizeof(dp));

  for (int i = 0; i < N; i++) {
    std::cin >> p[i];
  }

  dp[0][0] = 1;
  // i番目まで投げて
  for (int i = 0; i < N; ++i) {
    // j枚表が出てる場合
    for (int j = 0; j <= i; ++j) {
      // i番目が表の場合
      dp[i + 1][j + 1] += dp[i][j] * p[i];

      // 裏の場合
      dp[i + 1][j] += dp[i][j] * (1 - p[i]);
    }
  }

  // N枚まで投げた時 半分以上表の確率の合計
  double result = 0;
  for (int i = (N / 2 + 1); i <= N; ++i) {
    result += dp[N][i];
    // std::cout << "表が" << i << "枚出る確率" << dp[N][i] << std::endl;
  }
  std::cout << std::fixed << std::setprecision(10) << result;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <vector>

#define ll long long
const ll MOD = 1e9 + 7;

int main() {
  std::ios::sync_with_stdio(false);

  int N;
  std::cin >> N;
  std::vector<double> p(N);

  double dp[N + 1][N + 1];
  memset(dp, 0, sizeof(dp));

  for (int i = 0; i < N; i++) {
    std::cin >> p[i];
  }

  dp[0][0] = 1;
  // i番目まで投げて
  for (int i = 0; i < N; ++i) {
    // j枚表が出てる場合
    for (int j = 0; j <= i; ++j) {
      // i番目が表の場合
      dp[i + 1][j + 1] += dp[i][j] * p[i];

      // 裏の場合
      dp[i + 1][j] += dp[i][j] * (1 - p[i]);
    }
  }

  // N枚まで投げた時 半分以上表の確率の合計
  double result = 0;
  for (int i = (N / 2 + 1); i <= N; ++i) {
    result += dp[N][i];
    // std::cout << "表が" << i << "枚出る確率" << dp[N][i] << std::endl;
  }
  std::cout << std::fixed << std::setprecision(10) << result;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sc second
#define fr first
#define mp make_pair
const int N = 2e3 + 10, Max = 50 + 10;
const int MOD = 1e9 + 7;
double a[N];
double dp[N][N];
int n, m;
double solve(int i, int s) {
  if (i == n + 1) {
    return s > n / 2 ? 1.0 : 0.0;
  }
  double &ret = dp[i][s];
  if (ret == ret)
    return ret;
  double c1 = solve(i + 1, s) * (1.0 - a[i]);
  double c2 = solve(i + 1, s + 1) * a[i];
  return ret = c1 + c2;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));
  cout << fixed << setprecision(10) << solve(1, 0) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sc second
#define fr first
#define mp make_pair
const int N = 3e3 + 10, Max = 50 + 10;
const int MOD = 1e9 + 7;
double a[N];
double dp[N][N];
int n, m;
double solve(int i, int s) {
  if (i == n + 1) {
    return s > n / 2 ? 1.0 : 0.0;
  }
  double &ret = dp[i][s];
  if (ret == ret)
    return ret;
  double c1 = solve(i + 1, s) * (1.0 - a[i]);
  double c2 = solve(i + 1, s + 1) * a[i];
  return ret = c1 + c2;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));
  cout << fixed << setprecision(10) << solve(1, 0) << endl;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f first
#define s second
#define int long long

using namespace std;

const int N = 3005;

double p[N];
double dp[N][N];

int32_t main() {
  ios::sync_with_stdio(false);
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> p[i];
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j)
      dp[i][j] = 1;
  }
  dp[1][0] = double(1 - p[1]);
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= i; ++j) {
      if (j == 0 && i == 1)
        continue;
      if (j == 0) {
        dp[i][j] = dp[i - 1][j] * (1 - p[i]);
      }
      if (i == j) {
        dp[i][j] = dp[i - 1][j - 1] * p[i];
      } else
        dp[i][j] = dp[i - 1][j] * (1 - p[i]) + dp[i - 1][j - 1] * (p[i]);
    }
  }
  int mid = n / 2 + n % 2;
  double ans = 0;
  for (int i = mid; i <= n; ++i) {
    ans += dp[n][i];
  }
  cout << fixed << setprecision(10) << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
#define int long long

using namespace std;

const int N = 3005;

double p[N];
double dp[N][N];

int32_t main() {
  ios::sync_with_stdio(false);
  //    freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> p[i];
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j)
      dp[i][j] = 1;
  }
  dp[1][0] = double(1 - p[1]);
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= i; ++j) {
      if (j == 0 && i == 1)
        continue;
      if (j == 0) {
        dp[i][j] = dp[i - 1][j] * (1 - p[i]);
      }
      if (i == j) {
        dp[i][j] = dp[i - 1][j - 1] * p[i];
      } else
        dp[i][j] = dp[i - 1][j] * (1 - p[i]) + dp[i - 1][j - 1] * (p[i]);
    }
  }
  int mid = n / 2 + n % 2;
  double ans = 0;
  for (int i = mid; i <= n; ++i) {
    ans += dp[n][i];
  }
  cout << fixed << setprecision(10) << ans;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>

using namespace std;
typedef long double ll;

const ll MAX = 400;
vector<ll> ar(3000);
vector<vector<ll>> dp(MAX, vector<ll>(MAX, -1));

ll func(ll n, ll k) {
  if (n == 0 && k == 0) {
    return 1;
  }
  if (k > n) {
    return 0;
  }
  if (k < 0 || n < 0) {
    return 0;
  }
  if (dp[n][k] != -1) {
    return dp[n][k];
  } else {
    return dp[n][k] = func(n - 1, k) * (1 - ar[n]) + func(n - 1, k - 1) * ar[n];
  }
}

int main() {
  int n;
  cin >> n;
  for (int x = 1; x <= n; x++) {
    cin >> ar[x];
  }
  ll a = 0;
  ll max = 0;
  while (max <= n) {
    if (max > n / 2)
      a += func(n, max);
    max++;
  }
  cout << std::setprecision(10) << a << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>

using namespace std;
typedef long double ll;

const ll MAX = 4000;
vector<ll> ar(3000);
vector<vector<ll>> dp(MAX, vector<ll>(MAX, -1));

ll func(ll n, ll k) {
  if (n == 0 && k == 0) {
    return 1;
  }
  if (k > n) {
    return 0;
  }
  if (k < 0 || n < 0) {
    return 0;
  }
  if (dp[n][k] != -1) {
    return dp[n][k];
  } else {
    return dp[n][k] = func(n - 1, k) * (1 - ar[n]) + func(n - 1, k - 1) * ar[n];
  }
}

int main() {
  int n;
  cin >> n;
  for (int x = 1; x <= n; x++) {
    cin >> ar[x];
  }
  ll a = 0;
  ll max = 0;
  while (max <= n) {
    if (max > n / 2)
      a += func(n, max);
    max++;
  }
  cout << std::setprecision(10) << a << endl;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long double P[1003], dp[1003][1003];

int main() {

  ios::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);

  int N;
  cin >> N;

  for (int i = 1; i <= N; i++)
    cin >> P[i];

  dp[0][0] = 1;

  for (int i = 1; i <= N; i++)
    for (int j = 0; j <= N; j++) {
      dp[i][j] = dp[i - 1][j] * (1 - P[i]);

      if (j >= 1)
        dp[i][j] += dp[i - 1][j - 1] * P[i];
    }

  long double ans = 0;

  for (int tail = 0, head = N; head > tail; tail++, head--)
    ans += dp[N][head];

  cout << fixed << setprecision(12) << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

long double P[3003], dp[3003][3003];

int main() {

  ios::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);

  int N;
  cin >> N;

  for (int i = 1; i <= N; i++)
    cin >> P[i];

  dp[0][0] = 1;

  for (int i = 1; i <= N; i++)
    for (int j = 0; j <= N; j++) {
      dp[i][j] = dp[i - 1][j] * (1 - P[i]);

      if (j >= 1)
        dp[i][j] += dp[i - 1][j - 1] * P[i];
    }

  long double ans = 0;

  for (int tail = 0, head = N; head > tail; tail++, head--)
    ans += dp[N][head];

  cout << fixed << setprecision(12) << ans << "\n";
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define pf push_front
#define ll long long
using namespace std;
void TxtRead() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
void TheTime() {
  ios_base::sync_with_stdio(false);
  cin.tie();
}
const int N = 1e3 + 5;
const ll INF = 1e9 + 5;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793116;

int n;
double ar[N], dp[N][N];

int main() {
  // TxtRead();
  TheTime();

  scanf("%d", &n);
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%lf", &ar[i]);
    dp[i][0] = dp[i - 1][0] * (1 - ar[i]);
  }
  dp[1][1] = ar[1];

  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      dp[j][i] = dp[j - 1][i - 1] * ar[j] + dp[j - 1][i] * (1 - ar[j]);
    }
  }

  double ans = 0;
  for (int i = n / 2 + n % 2; i <= n; i++) {
    ans += dp[n][i];
  }
  printf("%.9lf\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define pf push_front
#define ll long long
using namespace std;
void TxtRead() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
void TheTime() {
  ios_base::sync_with_stdio(false);
  cin.tie();
}
const int N = 3e3 + 5;
const ll INF = 1e9 + 5;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793116;

int n;
double ar[N], dp[N][N];

int main() {
  // TxtRead();
  TheTime();

  scanf("%d", &n);
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%lf", &ar[i]);
    dp[i][0] = dp[i - 1][0] * (1 - ar[i]);
  }
  dp[1][1] = ar[1];

  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      dp[j][i] = dp[j - 1][i - 1] * ar[j] + dp[j - 1][i] * (1 - ar[j]);
    }
  }

  double ans = 0;
  for (int i = n / 2 + n % 2; i <= n; i++) {
    ans += dp[n][i];
  }
  printf("%.9lf\n", ans);
}