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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pi acos(-1)
#define ll long long
#define pii pair<ll, ll>
#define debug(a) cout << a << '\n'
#define maxn 3009 /// Still trying to be normal
#define MOD 1000000007
#define F first
#define S second
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define per(i, b, a) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
using namespace std;
const ll INF = 1e9 + 15;
double dp[maxn][maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  ll n;
  cin >> n;
  rep(i, 0, maxn) {
    rep(j, 0, maxn) { dp[i][j] = 0; }
  }
  dp[0][0] = 1;
  double pr[maxn];
  rep(i, 0, n) cin >> pr[i];
  rep(i, 0, n + 1) {
    rep(j, 0, n + 1) {
      if (i == 0 && j == 0)
        continue;
      if (i > 0)
        dp[i][j] += dp[i - 1][j] * pr[i + j - 1];
      if (j > 0)
        dp[i][j] += dp[i][j - 1] * (1 - pr[i + j - 1]);
    }
  }
  double ans = 0;
  rep(i, 0, n + 1) {
    rep(j, 0, n + 1) {
      if (i + j == n && i > j)
        ans += dp[i][j];
    }
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pi acos(-1)
#define ll long long
#define pii pair<ll, ll>
#define debug(a) cout << a << '\n'
#define maxn 3009 /// Still trying to be normal
#define MOD 1000000007
#define F first
#define S second
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define per(i, b, a) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
using namespace std;
const ll INF = 1e9 + 15;
double dp[maxn][maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  ll n;
  cin >> n;
  rep(i, 0, maxn) {
    rep(j, 0, maxn) { dp[i][j] = 0; }
  }
  dp[0][0] = 1;
  double pr[maxn];
  rep(i, 0, n) cin >> pr[i];
  rep(i, 0, n + 1) {
    rep(j, 0, n + 1) {
      if (i + j > n)
        continue;
      if (i > 0)
        dp[i][j] += dp[i - 1][j] * pr[i + j - 1];
      if (j > 0)
        dp[i][j] += dp[i][j - 1] * (1 - pr[i + j - 1]);
    }
  }
  double ans = 0;
  rep(i, 0, n + 1) {
    rep(j, 0, n + 1) {
      if (i + j == n && i > j)
        ans += dp[i][j];
    }
  }
  cout << ans << "\n";
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
const int INF = 1e9;
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}

double dp[3000][3000];
double dat[3000];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> dat[i];
  }
  dp[0][0] = 1.0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i + 1][j] += dp[i][j] * (1.0 - dat[i + 1]);
      dp[i + 1][j + 1] += dp[i][j] * (dat[i + 1]);
    }
  }
  double sum = 0.0;
  for (int i = (n + 1) / 2; i <= n; i++) {
    sum += dp[n][i];
  }
  printf("%.9f", sum);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
const int INF = 1e9;
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}

double dp[3005][3005];
double dat[3005];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> dat[i];
  }
  dp[0][0] = 1.0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i + 1][j] += dp[i][j] * (1.0 - dat[i + 1]);
      dp[i + 1][j + 1] += dp[i][j] * (dat[i + 1]);
    }
  }
  double sum = 0.0;
  for (int i = (n + 1) / 2; i <= n; i++) {
    sum += dp[n][i];
  }
  printf("%.9f", sum);
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int n;
vector<double> prob;
double dp[3001][3001];
double rec(long long ntail, long long nhead) {
  if (ntail + nhead == n)
    return (nhead > ntail);
  if (dp[ntail + nhead][nhead] != -1.0)
    return dp[ntail][nhead];
  double a = prob[ntail + nhead] * rec(ntail, nhead + 1);
  double b = (1 - prob[ntail + nhead]) * rec(ntail + 1, nhead);
  return dp[ntail][nhead] = a + b;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  for (int i = 0; i < 3001; i++)
    for (int j = 0; j < 3001; j++)
      dp[i][j] = -1.0;
  cin >> n;
  prob.resize(n);
  for (int i = 0; i < n; i++)
    cin >> prob[i];
  cout << fixed << setprecision(9) << rec(0, 0);
}


Fixed submission:
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int n;
vector<double> prob;
double dp[3001][3001];
double rec(long long ntail, long long nhead) {
  if (ntail + nhead == n)
    return (nhead > ntail);
  if (dp[ntail][nhead] != -1.0)
    return dp[ntail][nhead];
  double a = prob[ntail + nhead] * rec(ntail, nhead + 1);
  double b = (1 - prob[ntail + nhead]) * rec(ntail + 1, nhead);
  return dp[ntail][nhead] = a + b;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  for (int i = 0; i < 3001; i++)
    for (int j = 0; j < 3001; j++)
      dp[i][j] = -1.0;
  cin >> n;
  prob.resize(n);
  for (int i = 0; i < n; i++)
    cin >> prob[i];
  cout << fixed << setprecision(9) << rec(0, 0);
}
Problem p03168 (C++)

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

const int maxS = 1e3 + 7;
long double probs[maxS];
long double dp[maxS][maxS];
long double ONE = 1.000000000;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> probs[i];
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      dp[i][j] = 0.000000000;
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= i; j++) {
      if (i == 1 && j == 0) {
        dp[i][j] = ONE - probs[i];
        continue;
      }
      if (i == 1 && j == 1) {
        dp[i][j] = probs[i];
        continue;
      }
      if (j == 0) {
        dp[i][j] = dp[i - 1][j] * (ONE - probs[i]);
        continue;
      }
      dp[i][j] =
          dp[i - 1][j] * (ONE - probs[i]) + (dp[i - 1][j - 1] * probs[i]);

      // cout << dp[i][j] << " ";
    }
    // cout << "\n";
  }
  long double probOfMoreHeadsThanTails = 0.000000000;
  for (int i = N / 2 + 1; i <= N; i++) {
    probOfMoreHeadsThanTails += dp[N][i];
    // cout << i << " " << dp[N][i] << "\n";
  }
  cout << setprecision(9) << fixed;
  cout << probOfMoreHeadsThanTails << "\n";
}

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

const int maxS = 3e3 + 7;
long double probs[maxS];
long double dp[maxS][maxS];
long double ONE = 1.000000000;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> probs[i];
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      dp[i][j] = 0.000000000;
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= i; j++) {
      if (i == 1 && j == 0) {
        dp[i][j] = ONE - probs[i];
        continue;
      }
      if (i == 1 && j == 1) {
        dp[i][j] = probs[i];
        continue;
      }
      if (j == 0) {
        dp[i][j] = dp[i - 1][j] * (ONE - probs[i]);
        continue;
      }
      dp[i][j] =
          dp[i - 1][j] * (ONE - probs[i]) + (dp[i - 1][j - 1] * probs[i]);

      // cout << dp[i][j] << " ";
    }
    // cout << "\n";
  }
  long double probOfMoreHeadsThanTails = 0.000000000;
  for (int i = N / 2 + 1; i <= N; i++) {
    probOfMoreHeadsThanTails += dp[N][i];
    // cout << i << " " << dp[N][i] << "\n";
  }
  cout << setprecision(9) << fixed;
  cout << probOfMoreHeadsThanTails << "\n";
}
Problem p03168 (C++)

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

using namespace std;

long long n;
double h[3010], t[3010], memo[3010][3010], ret = 0;

double dp(int idx, int head) {
  if (idx == 0 && head == 0) {
    return (double)1.00;
  }
  if (memo[idx][head] != -1) {
    return memo[idx][head];
  }
  memo[idx][head] = dp(idx - 1, head - 1) * h[idx] + dp(idx - 1, head) * t[idx];
  return memo[idx][head];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  for (int i = 0; i < 3010; i++) {
    for (int j = 0; j < 3010; j++) {
      memo[i][j] = -1;
    }
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    t[i] = 1 - h[i];
  }
  for (int i = (n + 2) / 2; i <= n; i++) {
    ret = ret + dp(n, i);
  }
  cout << fixed << setprecision(10) << ret << endl;
}


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

using namespace std;

long long n;
double h[3010], t[3010], memo[3010][3010], ret = 0;

double dp(int idx, int head) {
  if (idx == 0 && head == 0) {
    return (double)1.00;
  } else if (idx == 0 && head != 0) {
    return (double)0.00;
  }
  if (memo[idx][head] != -1) {
    return memo[idx][head];
  }
  memo[idx][head] = dp(idx - 1, head - 1) * h[idx] + dp(idx - 1, head) * t[idx];
  return memo[idx][head];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  for (int i = 0; i < 3010; i++) {
    for (int j = 0; j < 3010; j++) {
      memo[i][j] = -1;
    }
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    t[i] = 1 - h[i];
  }
  for (int i = (n + 2) / 2; i <= n; i++) {
    ret = ret + dp(n, i);
  }
  cout << fixed << setprecision(10) << ret << endl;
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
const ll mod = 1e9 + 7;
template <typename T> T pow(T a, T b, long long m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans % m;
}
const ll N = 1e6 + 8;
const ll INF = 1e18;
ll n;
ld a[3001];
ld dp[3001][3001];
bool vis[3001][3001];
ld func(ll size, ll req) {
  if (req < 0)
    return 0.0;
  if (size == -1) {
    if (req == 0)
      return 1.0;
    else
      return 0.0;
  }
  if (vis[size][req])
    return dp[size][req];
  ld res =
      a[size] * func(size - 1, req - 1) + (1 - a[size]) * func(size - 1, req);
  return dp[size][req] = res;
}
int main() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ld ans = 0.0;
  memset(vis, 0, sizeof(vis));
  for (ll i = (n / 2) + 1; i <= n; i++) {
    ans += func(n - 1, i);
  }
  cout << fixed << setprecision(12) << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
const ll mod = 1e9 + 7;
template <typename T> T pow(T a, T b, long long m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans % m;
}
const ll N = 1e6 + 8;
const ll INF = 1e18;
ll n;
ld a[3001];
ld dp[3001][3001];
bool vis[3001][3001];
ld func(ll size, ll req) {
  if (req < 0)
    return 0.0;
  if (size == -1) {
    if (req == 0)
      return 1.0;
    else
      return 0.0;
  }
  if (vis[size][req])
    return dp[size][req];
  ld res =
      a[size] * func(size - 1, req - 1) + (1 - a[size]) * func(size - 1, req);
  vis[size][req] = 1;
  return dp[size][req] = res;
}
int main() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ld ans = 0.0;
  memset(vis, 0, sizeof(vis));
  for (ll i = (n / 2) + 1; i <= n; i++) {
    ans += func(n - 1, i);
  }
  cout << fixed << setprecision(12) << ans << endl;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

// まずは状態を定義。寿司の順番に意味が無いことに気づくので、状態としては、残りの寿司の個数がk枚の皿がそれぞれ何枚あるかということになる。
// 状態遷移は、サイコロを一回降った時にどうなるかを考える。遷移式はそのままだと0個の皿を引いたときに無限ループになるので式変形する必要あり。
// dp[x][y][z] 残り一個のものがx, 残り二個のものがy,
// 残り三個のものがzのときの期待値
const int maxn = 7;
long double dp[maxn][maxn][maxn];
bool done[maxn][maxn][maxn];
int n;

long double dfs(int x, int y, int z) {
  if ((x == 0 && y == 0 && z == 0) || (x < 0 || y < 0 || z < 0))
    return 0.0;
  if (done[x][y][z])
    return dp[x][y][z];

  long double s = x + y + z;
  dp[x][y][z] = dfs(x - 1, y, z) * x / s + dfs(x + 1, y - 1, z) * y / s +
                dfs(x, y + 1, z - 1) * z / s + n / s;
  done[x][y][z] = true;
  return dp[x][y][z];
}

int main() {
  cin >> n;
  int one = 0, two = 0, three = 0, tmp;
  for (int i = 1; i <= n; i++) {
    cin >> tmp;
    if (tmp == 1)
      one++;
    if (tmp == 2)
      two++;
    if (tmp == 3)
      three++;
  }
  for (int i = 0; i < maxn; i++) {
    for (int j = 0; j < maxn; j++) {
      for (int k = 0; k < maxn; k++) {
        done[i][j][k] = false;
      }
    }
  }
  long double res = 0.0;
  res = dfs(one, two, three);
  cout << fixed << setprecision(10);
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

// まずは状態を定義。寿司の順番に意味が無いことに気づくので、状態としては、残りの寿司の個数がk枚の皿がそれぞれ何枚あるかということになる。
// 状態遷移は、サイコロを一回降った時にどうなるかを考える。遷移式はそのままだと0個の皿を引いたときに無限ループになるので式変形する必要あり。
// dp[x][y][z] 残り一個のものがx, 残り二個のものがy,
// 残り三個のものがzのときの期待値
const int maxn = 301;
long double dp[maxn][maxn][maxn];
bool done[maxn][maxn][maxn];
int n;

long double dfs(int x, int y, int z) {
  if ((x == 0 && y == 0 && z == 0) || (x < 0 || y < 0 || z < 0))
    return 0.0;
  if (done[x][y][z])
    return dp[x][y][z];

  long double s = x + y + z;
  dp[x][y][z] = dfs(x - 1, y, z) * x / s + dfs(x + 1, y - 1, z) * y / s +
                dfs(x, y + 1, z - 1) * z / s + n / s;
  done[x][y][z] = true;
  return dp[x][y][z];
}

int main() {
  cin >> n;
  int one = 0, two = 0, three = 0, tmp;
  for (int i = 1; i <= n; i++) {
    cin >> tmp;
    if (tmp == 1)
      one++;
    if (tmp == 2)
      two++;
    if (tmp == 3)
      three++;
  }
  for (int i = 0; i < maxn; i++) {
    for (int j = 0; j < maxn; j++) {
      for (int k = 0; k < maxn; k++) {
        done[i][j][k] = false;
      }
    }
  }
  long double res = 0.0;
  res = dfs(one, two, three);
  cout << fixed << setprecision(10);
  cout << res << endl;
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vll = vector<ll>;
using vii = vector<int>;
using pii = pair<int, int>;

const ll INF = numeric_limits<ll>::max(), MOD = 1e9 + 7;

const int INF_i = numeric_limits<int>::max(), MAX = 2e2 + 1;

double dp[MAX][MAX][MAX], n;

double f(int i, int j, int k) {
  if (i == 0 and j == 0 and k == 0)
    return 0;
  double &res = dp[i][j][k];
  if (res != -1)
    return res;
  double m = i + j + k;
  res = n / m;
  if (i > 0)
    res += i * f(i - 1, j, k) / m;
  if (j > 0)
    res += j * f(i + 1, j - 1, k) / m;
  if (k > 0)
    res += k * f(i, j + 1, k - 1) / m;
  return res;
}

int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  // freopen("in.in", "r", stdin);
  cin >> n;
  int arr[3] = {};
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    arr[x - 1]++;
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  cout << fixed << setprecision(9) << f(arr[0], arr[1], arr[2]) << '\n';
}

Fixed submission:
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vll = vector<ll>;
using vii = vector<int>;
using pii = pair<int, int>;

const ll INF = numeric_limits<ll>::max(), MOD = 1e9 + 7;

const int INF_i = numeric_limits<int>::max(), MAX = 3e2 + 1;

double dp[MAX][MAX][MAX], n;

double f(int i, int j, int k) {
  if (i == 0 and j == 0 and k == 0)
    return 0;
  double &res = dp[i][j][k];
  if (res != -1)
    return res;
  double m = i + j + k;
  res = n / m;
  if (i > 0)
    res += i * f(i - 1, j, k) / m;
  if (j > 0)
    res += j * f(i + 1, j - 1, k) / m;
  if (k > 0)
    res += k * f(i, j + 1, k - 1) / m;
  return res;
}

int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  // freopen("in.in", "r", stdin);
  cin >> n;
  int arr[3] = {};
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    arr[x - 1]++;
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  cout << fixed << setprecision(9) << f(arr[0], arr[1], arr[2]) << '\n';
}
Problem p03169 (C++)

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

#define dbl long double
const int siz = 307;

int n, J, D, T, sum;
dbl dp[3][siz][siz];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    sum += x;
    if (x == 1)
      J++;
    if (x == 2)
      D++;
    if (x == 3)
      T++;
  }
  dp[0][0][0] = 0.0;

  int akt = 1, po = 0;
  for (int s = 1; s <= sum; s++) {
    for (int j = 0; j <= min(n, s); j++) {
      for (int d = 0; d <= n; d++) {
        if ((s - j - 2 * d) % 3 != 0)
          continue;

        int t = ((s - j - 2 * d) / 3), z = n - t - j - d;
        if ((d + t + z + j != n) or (d > D + T) or (t > T) or
            (j + 2 * d + 3 * t != s))
          continue;

        dbl jeden, dwa, trzy;
        jeden = ((dbl)j / (dbl)n) * dp[po][j - 1][d];
        dwa = ((dbl)d / (dbl)n) * dp[po][j + 1][d - 1];
        trzy = ((dbl)t / (dbl)n) * dp[po][j][d + 1];

        dp[akt][j][d] = ((dbl)n / (dbl)(n - z)) * ((dbl)1 + jeden + dwa + trzy);
        // printf("s = %d, jedynek jest %d, dwojek %d -> dp =
        // %lf\n",s,j,d,dp[s][j][d]);
      }
    }
    if (s == sum)
      break;
    if (akt == 1) {
      akt = 0;
      po = 1;
    } else {
      akt = 1;
      po = 0;
    }
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++)
        dp[akt][i][j] = (dbl)0;
  }
  printf("%.17Lf\n", dp[akt][J][D]);
  return 0;
}
// https://codeforces.com/contest/1245/problem/E


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

#define dbl long double
const int siz = 307;

int n, J, D, T, sum;
dbl dp[3][siz][siz];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    sum += x;
    if (x == 1)
      J++;
    if (x == 2)
      D++;
    if (x == 3)
      T++;
  }
  dp[0][0][0] = 0.0;

  int akt = 1, po = 0;
  for (int s = 1; s <= sum; s++) {
    for (int j = 0; j <= min(n, s); j++) {
      for (int d = 0; d <= n; d++) {
        if ((s - j - 2 * d) % 3 != 0)
          continue;

        int t = ((s - j - 2 * d) / 3), z = n - t - j - d;
        if ((d + t + z + j != n) or (d > D + T) or (t > T) or
            (j + 2 * d + 3 * t != s))
          continue;

        dbl jeden, dwa, trzy;
        jeden = ((dbl)j / (dbl)n) * dp[po][max(0, j - 1)][d];
        dwa = ((dbl)d / (dbl)n) * dp[po][j + 1][max(0, d - 1)];
        trzy = ((dbl)t / (dbl)n) * dp[po][j][d + 1];

        dp[akt][j][d] = ((dbl)n / (dbl)(n - z)) * ((dbl)1 + jeden + dwa + trzy);
        // printf("s = %d, jedynek jest %d, dwojek %d -> dp =
        // %lf\n",s,j,d,dp[s][j][d]);
      }
    }
    if (s == sum)
      break;
    if (akt == 1) {
      akt = 0;
      po = 1;
    } else {
      akt = 1;
      po = 0;
    }
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++)
        dp[akt][i][j] = (dbl)0;
  }
  printf("%.17Lf\n", dp[akt][J][D]);
  return 0;
}
// https://codeforces.com/contest/1245/problem/E
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

ld dp[301][301][301];
int cnt[3];

bool valid(int x, int y, int z) { return (x >= 0 && y >= 0 && z >= 0); }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    cnt[x]++;
  }
  dp[0][0][0] = 0;
  for (int k = 0; k <= cnt[2]; k++) {
    for (int j = 0; j <= n; j++) {
      for (int i = 0; i <= n; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        dp[i][j][k] = 1;
        if (i > 0)
          dp[i][j][k] += ld(i) / ld(n) * dp[i - 1][j][k];
        if (j > 0)
          dp[i][j][k] += ld(j) / ld(n) * dp[i + 1][j - 1][k];
        if (k > 0)
          dp[i][j][k] += ld(k) / ld(n) * dp[i][j + 1][k - 1];
        dp[i][j][k] *= ld(n) / ld(i + j + k);
        // cerr<<i<<' '<<j<<' '<<k<<' '<<dp[i][j][k]<<'\n';
      }
    }
  }
  cout << fixed << setprecision(15) << dp[cnt[0]][cnt[1]][cnt[2]] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

ld dp[311][311][311];
int cnt[3];

bool valid(int x, int y, int z) { return (x >= 0 && y >= 0 && z >= 0); }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    cnt[x]++;
  }
  dp[0][0][0] = 0;
  for (int k = 0; k <= cnt[2]; k++) {
    for (int j = 0; j <= n; j++) {
      for (int i = 0; i <= n; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        dp[i][j][k] = 1;
        if (i > 0)
          dp[i][j][k] += ld(i) / ld(n) * dp[i - 1][j][k];
        if (j > 0)
          dp[i][j][k] += ld(j) / ld(n) * dp[i + 1][j - 1][k];
        if (k > 0)
          dp[i][j][k] += ld(k) / ld(n) * dp[i][j + 1][k - 1];
        dp[i][j][k] *= ld(n) / ld(i + j + k);
        // cerr<<i<<' '<<j<<' '<<k<<' '<<dp[i][j][k]<<'\n';
      }
    }
  }
  cout << fixed << setprecision(15) << dp[cnt[0]][cnt[1]][cnt[2]] << '\n';
}
Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
typedef long long ll;

int main() {
  int n;
  cin >> n;
  int k = 0, l = 0, m = 0;
  rep(i, n) {
    int a;
    cin >> a;
    if (a == 1)
      k++;
    else if (a == 2)
      l++;
    else
      m++;
  }
  vector<vector<vector<double>>> dp(
      n + 2, vector<vector<double>>(n + 2, vector<double>(n + 2, 0.0)));
  rep(z, n + 1) {
    rep(y, n + 1) {
      rep(x, n + 1) {
        if (x + y + z != 0) {
          double a = (double)x, b = (double)y, c = (double)z, d = (double)n;
          if (x)
            dp[x][y][z] += a * dp[x - 1][y][z];
          if (y)
            dp[x][y][z] += b * dp[x + 1][y - 1][z];
          if (z)
            dp[x][y][z] += c * dp[x][y + 1][z - 1];
          dp[x][y][z] += d;
          dp[x][y][z] /= (a + b + c);
        }
      }
    }
  }
  printf("%.12f\n", dp[k][l][m]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
typedef long long ll;

int main() {
  int n;
  cin >> n;
  int k = 0, l = 0, m = 0;
  rep(i, n) {
    int a;
    cin >> a;
    if (a == 1)
      k++;
    else if (a == 2)
      l++;
    else
      m++;
  }
  vector<vector<vector<double>>> dp(
      n + 2, vector<vector<double>>(n + 2, vector<double>(n + 2, 0.0)));
  rep(z, n + 1) {
    rep(y, n + 1) {
      rep(x, n + 1) {
        if (x + y + z > n)
          break;
        if (x + y + z != 0) {
          double a = (double)x, b = (double)y, c = (double)z, d = (double)n;
          if (x)
            dp[x][y][z] += a * dp[x - 1][y][z];
          if (y)
            dp[x][y][z] += b * dp[x + 1][y - 1][z];
          if (z)
            dp[x][y][z] += c * dp[x][y + 1][z - 1];
          dp[x][y][z] += d;
          dp[x][y][z] /= (a + b + c);
        }
      }
    }
  }
  printf("%.12f\n", dp[k][l][m]);
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl '\n'
using namespace std;
const ll mod = (ll)1e9 + 7;
const int INF = 0x3f3f3f3f;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }

int n, x;
vector<int> v(4);
double dp[303][303][303];
double ev[303][303][303];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("C.txt", "r", stdin);
#endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v[x]++;
  }
  dp[v[1]][v[2]][v[3]] = 1;
  for (int c = n; c >= 0; c--) {
    for (int b = n; b >= 0; b--) {
      for (int a = n; a >= 0; a--) {
        if (a + b + c > n || a + b + c == 0)
          continue;
        ev[a][b][c] += (double)n / (a + b + c) * dp[a][b][c];
        if (a != 0) {
          dp[a - 1][b][c] += dp[a][b][c] * (double)a / (a + b + c);
          ev[a - 1][b][c] += ev[a][b][c] * (double)a / (a + b + c);
        }
        if (b != 0) {
          dp[a + 1][b - 1][c] += dp[a][b][c] * (double)b / (a + b + c);
          ev[a + 1][b - 1][c] += ev[a][b][c] * (double)b / (a + b + c);
        }
        if (c != 0) {
          dp[a][b + 1][c - 1] += dp[a][b][c] * (double)c / (a + b + c);
          ev[a][b + 1][c - 1] += ev[a][b][c] * (double)c / (a + b + c);
        }
      }
    }
  }
  cout << fixed << setprecision(9) << ev[0][0][0];
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl '\n'
using namespace std;
const ll mod = (ll)1e9 + 7;
const int INF = 0x3f3f3f3f;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }

int n, x;
vector<int> v(4);
double dp[303][303][303];
double ev[303][303][303];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
// freopen("C.txt", "r", stdin);
#endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v[x]++;
  }
  dp[v[1]][v[2]][v[3]] = 1;
  for (int c = n; c >= 0; c--) {
    for (int b = n; b >= 0; b--) {
      for (int a = n; a >= 0; a--) {
        if (a + b + c > n || a + b + c == 0)
          continue;
        ev[a][b][c] += (double)n / (a + b + c) * dp[a][b][c];
        if (a != 0) {
          dp[a - 1][b][c] += dp[a][b][c] * (double)a / (a + b + c);
          ev[a - 1][b][c] += ev[a][b][c] * (double)a / (a + b + c);
        }
        if (b != 0) {
          dp[a + 1][b - 1][c] += dp[a][b][c] * (double)b / (a + b + c);
          ev[a + 1][b - 1][c] += ev[a][b][c] * (double)b / (a + b + c);
        }
        if (c != 0) {
          dp[a][b + 1][c - 1] += dp[a][b][c] * (double)c / (a + b + c);
          ev[a][b + 1][c - 1] += ev[a][b][c] * (double)c / (a + b + c);
        }
      }
    }
  }
  cout << fixed << setprecision(9) << ev[0][0][0];
}
Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)

using namespace std;

double dp[310][310][310];
ll N;

double rec(ll one, ll two, ll three) {
  if (dp[one][two][three] != -1)
    return dp[one][two][three];
  double sum = 0;
  sum += one;
  sum += two;
  sum += three;

  double ans = N;
  ans /= sum;
  if (one > 0)
    ans += rec(one - 1, two, three) * one / sum;
  if (two > 0)
    ans += rec(one + 1, two - 1, three) * two / sum;
  if (three > 0)
    ans += rec(one, two + 1, three - 1) * three / sum;

  return ans;
}

void solve() {
  rep(i, 310) rep(j, 310) rep(k, 310) dp[i][j][k] = -1;
  dp[0][0][0] = 0;

  cin >> N;
  vector<ll> input(4);
  rep(i, N) {
    ll a;
    cin >> a;
    input[a]++;
  }

  cout << rec(input[1], input[2], input[3]) << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  cout << fixed;
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)

using namespace std;

double dp[310][310][310];
ll N;

double rec(ll one, ll two, ll three) {
  if (dp[one][two][three] != -1)
    return dp[one][two][three];
  double sum = 0;
  sum += one;
  sum += two;
  sum += three;

  double ans = N;
  ans /= sum;
  if (one > 0)
    ans += rec(one - 1, two, three) * one / sum;
  if (two > 0)
    ans += rec(one + 1, two - 1, three) * two / sum;
  if (three > 0)
    ans += rec(one, two + 1, three - 1) * three / sum;

  dp[one][two][three] = ans;
  return ans;
}

void solve() {
  rep(i, 310) rep(j, 310) rep(k, 310) dp[i][j][k] = -1;
  dp[0][0][0] = 0;

  cin >> N;
  vector<ll> input(4);
  rep(i, N) {
    ll a;
    cin >> a;
    input[a]++;
  }

  cout << rec(input[1], input[2], input[3]) << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  cout << fixed;
  solve();
  return 0;
}
Problem p03169 (Python)

Buggy submission (status: Runtime Error):
import numpy as np
from numba import njit


@njit("(f8[:,:,:],i4,i4,i4,i4,)", cache=True)
def solve(dp, N, n1, n2, n3):
    if dp[n1][n2][n3] >= 0.0:
        return dp[n1][n2][n3]
    if n1 == n2 == n3 == 0:
        return 0.0

    cnt = n1 + n2 + n3
    ret = 0
    if n3 >= 1:
        ret += solve(dp, N, n1, n2 + 1, n3 - 1) * n3
    if n2 >= 1:
        ret += solve(dp, N, n1 + 1, n2 - 1, n3) * n2
    if n1 >= 1:
        ret += solve(dp, N, n1 - 1, n2, n3) * n1

    ret = (ret + N) / cnt
    dp[n1][n2][n3] = ret
    return ret


def main():
    N = int(input())
    A = list(map(int, input().split()))

    dp = np.full((N + 1, N + 1, N + 1), -1, dtype=np.float64)

    a1, a2, a3 = 0, 0, 0
    for i in range(N):
        if A[i] == 1:
            a1 += 1
        elif A[i] == 2:
            a2 += 1
        elif A[i] == 3:
            a3 += 1

    print(solve(dp, N, a1, a2, a3))


if __name__ == "__main__":
    main()


Fixed submission:
import numpy as np
from numba import njit


@njit("(f8[:,:,:],i4,i4,i4,i4,)")
def solve(dp, N, n1, n2, n3):
    if dp[n1][n2][n3] >= 0.0:
        return dp[n1][n2][n3]
    if n1 == n2 == n3 == 0:
        return 0.0

    cnt = n1 + n2 + n3
    ret = 0
    if n3 >= 1:
        ret += solve(dp, N, n1, n2 + 1, n3 - 1) * n3
    if n2 >= 1:
        ret += solve(dp, N, n1 + 1, n2 - 1, n3) * n2
    if n1 >= 1:
        ret += solve(dp, N, n1 - 1, n2, n3) * n1

    ret = (ret + N) / cnt
    dp[n1][n2][n3] = ret
    return ret


def main():
    N = int(input())
    A = list(map(int, input().split()))

    dp = np.full((N + 1, N + 1, N + 1), -1, dtype=np.float64)

    a1, a2, a3 = 0, 0, 0
    for i in range(N):
        if A[i] == 1:
            a1 += 1
        elif A[i] == 2:
            a2 += 1
        elif A[i] == 3:
            a3 += 1

    print(solve(dp, N, a1, a2, a3))


if __name__ == "__main__":
    main()
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
        ll ans = 1; while (b) {
                while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
                ans = (ans * a) % MOD, --b;
        } return ans;
}
*/

const int MAXN = 310;

int n;
int a[MAXN];
int cc[4];
ld dp[301][301][301];

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  cout.precision(20);
  cout.setf(ios::fixed);
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    ++cc[a[i]];
  for (int k = 0; k <= cc[3]; ++k)
    for (int j = 0; j <= cc[2] + cc[3]; ++j)
      for (int i = 0; i <= cc[1] + cc[2] + cc[3]; ++i) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        if (i)
          dp[i][j][k] += i * dp[i - 1][j][k];
        if (j)
          dp[i][j][k] += j * dp[i + 1][j - 1][k];
        if (k)
          dp[i][j][k] += k * dp[i][j + 1][k - 1];
        dp[i][j][k] /= n;
        dp[i][j][k] += 1;
        dp[i][j][k] /= ld(i + j + k) / n;
      }
  cout << dp[cc[1]][cc[2]][cc[3]] << "\n";
  return 0;
}


Fixed submission:
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
        ll ans = 1; while (b) {
                while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
                ans = (ans * a) % MOD, --b;
        } return ans;
}
*/

const int MAXN = 310;

int n;
int a[MAXN];
int cc[4];
ld dp[305][305][305];

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  cout.precision(20);
  cout.setf(ios::fixed);
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    ++cc[a[i]];
  for (int k = 0; k <= cc[3]; ++k)
    for (int j = 0; j <= cc[2] + cc[3]; ++j)
      for (int i = 0; i <= cc[1] + cc[2] + cc[3]; ++i) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        if (i)
          dp[i][j][k] += i * dp[i - 1][j][k];
        if (j)
          dp[i][j][k] += j * dp[i + 1][j - 1][k];
        if (k)
          dp[i][j][k] += k * dp[i][j + 1][k - 1];
        dp[i][j][k] /= n;
        dp[i][j][k] += 1;
        dp[i][j][k] /= ld(i + j + k) / n;
      }
  cout << dp[cc[1]][cc[2]][cc[3]] << "\n";
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
double dp[301][301][301];
int main() {
  int n;
  cin >> n;
  int n1 = 0, n2 = 0, n3 = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (x == 1)
      n1++;
    else if (x == 2)
      n2++;
    else
      n3++;
  }
  for (int k = 0; k <= n3; k++)
    for (int j = 0; j <= n3 + n2; j++)
      for (int i = 0; i <= n3 + n2 + n1; i++) {
        if (i == 0 && j == 0 && k == 0) {
          dp[i][j][k] = 0;
          continue;
        }
        dp[i][j][k] = 1.0 * (n) / (i + j + k);
        if (i)
          dp[i][j][k] += (1.0 * i / (i + j + k)) * dp[i - 1][j][k];
        if (j)
          dp[i][j][k] += (1.0 * j / (i + j + k)) * dp[i + 1][j - 1][k];
        if (k)
          dp[i][j][k] += (1.0 * k / (i + j + k)) * dp[i][j + 1][k - 1];
      }
  printf("%.10lf", dp[n1][n2][n3]);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
double dp[305][305][305];
int main() {
  int n;
  cin >> n;
  int n1 = 0, n2 = 0, n3 = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (x == 1)
      n1++;
    else if (x == 2)
      n2++;
    else
      n3++;
  }
  for (int k = 0; k <= n3; k++)
    for (int j = 0; j <= n3 + n2; j++)
      for (int i = 0; i <= n3 + n2 + n1; i++) {
        if (i == 0 && j == 0 && k == 0) {
          dp[i][j][k] = 0;
          continue;
        }
        dp[i][j][k] = 1.0 * (n) / (i + j + k);
        if (i)
          dp[i][j][k] += (1.0 * i / (i + j + k)) * dp[i - 1][j][k];
        if (j)
          dp[i][j][k] += (1.0 * j / (i + j + k)) * dp[i + 1][j - 1][k];
        if (k)
          dp[i][j][k] += (1.0 * k / (i + j + k)) * dp[i][j + 1][k - 1];
      }
  printf("%.10lf", dp[n1][n2][n3]);
  return 0;
}
Problem p03169 (C++)

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, inp, cnt1 = 0, cnt2 = 0, cnt3 = 0;
  vector<vector<vector<double>>> dp(
      305, vector<vector<double>>(305, vector<double>(305)));
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> inp;
    if (inp == 1)
      cnt1++;
    else if (inp == 2)
      cnt2++;
    else
      cnt3++;
  }
  dp[0][0][0] = 0;
  for (int k = 0; k <= cnt3; k++) {
    for (int j = 0; j <= cnt2 + cnt3; j++) {
      for (int i = 0; i <= cnt1 + cnt2 + cnt3; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        int s = i + j + k;
        if (k)
          dp[i][j][k] += 1.0 * k / s * dp[i][j + 1][k - 1];
        if (j)
          dp[i][j][k] += 1.0 * j / s * dp[i + 1][j - 1][k];
        if (i)
          dp[i][j][k] += 1.0 * i / s * dp[i - 1][j][k];
        dp[i][j][k] += 1.0 * n / s;
      }
    }
  }
  printf("%.9f", dp[cnt1][cnt2][cnt3]);
}

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, inp, cnt1 = 0, cnt2 = 0, cnt3 = 0;
  double dp[305][305][305];
  // vector<vector<vector<double>>> dp (305, vector<vector<double>> (305 ,
  // vector<double> (305)));
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> inp;
    if (inp == 1)
      cnt1++;
    else if (inp == 2)
      cnt2++;
    else
      cnt3++;
  }
  dp[0][0][0] = 0;
  for (int k = 0; k <= cnt3; k++) {
    for (int j = 0; j <= cnt2 + cnt3; j++) {
      for (int i = 0; i <= cnt1 + cnt2 + cnt3; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        int s = i + j + k;
        if (k)
          dp[i][j][k] += 1.0 * k / s * dp[i][j + 1][k - 1];
        if (j)
          dp[i][j][k] += 1.0 * j / s * dp[i + 1][j - 1][k];
        if (i)
          dp[i][j][k] += 1.0 * i / s * dp[i - 1][j][k];
        dp[i][j][k] += 1.0 * n / s;
      }
    }
  }
  printf("%.9f", dp[cnt1][cnt2][cnt3]);
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

double dp[301][301][301];
int a[4];
int main() {
  int n, ai;
  cin >> n;
  rep(i, n) {
    cin >> ai;
    ++a[ai - 1];
  }
  rep(i, n + 1) rep(j, n + 1) rep(k, n + 1) {
    int c = i + j + k;
    if (c == 0)
      continue;
    if (k != 0)
      dp[k][j][i] += dp[k - 1][j][i] * k;
    if (j != 0)
      dp[k][j][i] += dp[k + 1][j - 1][i] * j;
    if (i != 0)
      dp[k][j][i] += dp[k][j + 1][i - 1] * i;
    dp[k][j][i] += n;
    dp[k][j][i] /= c;
  }
  cout << setprecision(10) << fixed << dp[a[0]][a[1]][a[2]] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

double dp[304][304][304];
int a[4];
int main() {
  int n, ai;
  cin >> n;
  rep(i, n) {
    cin >> ai;
    ++a[ai - 1];
  }
  rep(i, n + 1) rep(j, n + 1) rep(k, n + 1) {
    int c = i + j + k;
    if (c == 0)
      continue;
    if (k != 0)
      dp[k][j][i] += dp[k - 1][j][i] * k;
    if (j != 0)
      dp[k][j][i] += dp[k + 1][j - 1][i] * j;
    if (i != 0)
      dp[k][j][i] += dp[k][j + 1][i - 1] * i;
    dp[k][j][i] += n;
    dp[k][j][i] /= c;
  }
  cout << setprecision(10) << fixed << dp[a[0]][a[1]][a[2]] << endl;
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef long double ld;

int n;
ld dp[31][31][31];

ld func(int a, int b, int c) {
  int i, j, l;
  if (a == 0 && b == 0 && c == 0)
    return 0;
  if (dp[a][b][c] != -1)
    return dp[a][b][c];
  ld x, y, z;
  j = a + b + c;
  x = (ld)n / (ld)j;
  y = 0.0;

  if (j == 0)
    return 0;

  if (a > 0)
    y = y + a * func(a - 1, b, c);
  if (b > 0)
    y = y + b * func(a + 1, b - 1, c);
  if (c > 0)
    y = y + c * func(a, b + 1, c - 1);

  y = (ld)y / (ld)j;
  x = x + y;
  return dp[a][b][c] = x;
}

int main() {
  cin >> n;
  int i, j, l;
  int a = 0, b = 0, c = 0;
  // memset(dp,-1.0,sizeof(dp));
  for (i = 0; i <= n; i++)
    for (j = 0; j <= n; j++)
      for (l = 0; l <= n; l++)
        dp[i][j][l] = -1;
  for (i = 0; i < n; i++) {
    cin >> j;
    if (j == 1)
      a++;
    else if (j == 2)
      b++;
    else
      c++;
  }
  ld ans = func(a, b, c);
  cout << fixed << setprecision(20) << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef long double ld;

int n;
ld dp[301][301][301];

ld func(int a, int b, int c) {
  int i, j, l;
  if (a == 0 && b == 0 && c == 0)
    return 0;
  if (dp[a][b][c] != -1)
    return dp[a][b][c];
  ld x, y, z;
  j = a + b + c;
  x = (ld)n / (ld)j;
  y = 0.0;

  if (j == 0)
    return 0;

  if (a > 0)
    y = y + a * func(a - 1, b, c);
  if (b > 0)
    y = y + b * func(a + 1, b - 1, c);
  if (c > 0)
    y = y + c * func(a, b + 1, c - 1);

  y = (ld)y / (ld)j;
  x = x + y;
  return dp[a][b][c] = x;
}

int main() {
  cin >> n;
  int i, j, l;
  int a = 0, b = 0, c = 0;
  // memset(dp,-1.0,sizeof(dp));
  for (i = 0; i <= n; i++)
    for (j = 0; j <= n; j++)
      for (l = 0; l <= n; l++)
        dp[i][j][l] = -1;
  for (i = 0; i < n; i++) {
    cin >> j;
    if (j == 1)
      a++;
    else if (j == 2)
      b++;
    else
      c++;
  }
  ld ans = func(a, b, c);
  cout << fixed << setprecision(20) << ans << endl;
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ld v[301][301][301] = {0.0};
int main() {
  int n;
  cin >> n;
  v[0][0][0] = 0;
  for (int z = 0; z <= n; z++) {
    for (int y = 0; y <= n; y++) {
      for (int x = 0; x <= n; x++) {
        if ((x + y + z) != 0) {
          v[x][y][z] = (ld)n / (x + y + z);
        }
        if (x != 0) {
          v[x][y][z] += x * v[x - 1][y][z] / (x + y + z);
        }
        if (y != 0) {
          v[x][y][z] += y * v[x + 1][y - 1][z] / (x + y + z);
        }
        if (z != 0) {
          v[x][y][z] += z * v[x][y + 1][z - 1] / (x + y + z);
        }
      }
    }
  }
  int a[4] = {0};
  int x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    a[x]++;
  }
  cout << fixed << setprecision(10) << v[a[1]][a[2]][a[3]] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ld v[303][303][303] = {0.0};
int main() {
  int n;
  cin >> n;
  v[0][0][0] = 0;
  for (int z = 0; z <= n; z++) {
    for (int y = 0; y <= n; y++) {
      for (int x = 0; x <= n; x++) {
        if ((x + y + z) != 0) {
          v[x][y][z] = (ld)n / (x + y + z);
        }
        if (x != 0) {
          v[x][y][z] += x * v[x - 1][y][z] / (x + y + z);
        }
        if (y != 0) {
          v[x][y][z] += y * v[x + 1][y - 1][z] / (x + y + z);
        }
        if (z != 0) {
          v[x][y][z] += z * v[x][y + 1][z - 1] / (x + y + z);
        }
      }
    }
  }
  int a[4] = {0};
  int x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    a[x]++;
  }
  cout << fixed << setprecision(10) << v[a[1]][a[2]][a[3]] << endl;

  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define ff first
#define endl "\n"
#define ss second
#define li list<int>
#define vi vector<int>
#define ll long long int
#define pii pair<int, int>
#define vii vector<pair<int, int>>
#define mp make_pair
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define custom pair<int, pair<int, int>>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
#define mod 1000000007
double dp[301][301][301];
double solve(int x, int y, int z, int n) {
  if (x < 0 || y < 0 || z < 0) {
    return 0;
  }
  if (x == 0 && y == 0 && z == 0)
    return 0;
  double exp = n + x * solve(x - 1, y, z, n) + y * solve(x + 1, y - 1, z, n) +
               z * solve(x, y + 1, z - 1, n);
  return dp[x][y][z] = exp / (x + y + z);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int one = 0;
  int two = 0;
  int three = 0;
  int ex;
  for (int i = 0; i < n; i++) {
    cin >> ex;
    if (ex == 1) {
      one++;
    } else if (ex == 2) {
      two++;
    } else {
      three++;
    }
  }
  memset(dp, -1, sizeof dp);
  cout << fixed << setprecision(9) << solve(one, two, three, n);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define ff first
#define endl "\n"
#define ss second
#define li list<int>
#define vi vector<int>
#define ll long long int
#define pii pair<int, int>
#define vii vector<pair<int, int>>
#define mp make_pair
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define custom pair<int, pair<int, int>>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
#define mod 1000000007
double dp[301][301][301];
double solve(int x, int y, int z, int n) {
  if (x < 0 || y < 0 || z < 0) {
    return 0;
  }
  if (x == 0 && y == 0 && z == 0)
    return 0;
  if (dp[x][y][z] > -0.5) {
    return dp[x][y][z];
  }
  double exp = n + x * solve(x - 1, y, z, n) + y * solve(x + 1, y - 1, z, n) +
               z * solve(x, y + 1, z - 1, n);
  return dp[x][y][z] = exp / (x + y + z);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int one = 0;
  int two = 0;
  int three = 0;
  int ex;
  for (int i = 0; i < n; i++) {
    cin >> ex;
    if (ex == 1) {
      one++;
    } else if (ex == 2) {
      two++;
    } else {
      three++;
    }
  }
  memset(dp, -1, sizeof dp);
  cout << fixed << setprecision(9) << solve(one, two, three, n);
  return 0;
}