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

Buggy submission (status: Time Limit Exceeded):


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define int long long

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);
}

int32_t main() {
  int n, x;
  cin >> n;
  memset(dp, -1, sizeof dp);
  int one = 0, two = 0, three = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (x == 1) {
      one++;
    } else if (x == 2) {
      two++;
    } else
      three++;
  }
  cout << fixed << setprecision(9) << solve(one, two, three, n);
}


Fixed submission:


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define int long long

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.9) {
    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);
}

int32_t main() {
  int n, x;
  cin >> n;
  memset(dp, -1, sizeof dp);
  int one = 0, two = 0, three = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (x == 1) {
      one++;
    } else if (x == 2) {
      two++;
    } else
      three++;
  }
  cout << fixed << setprecision(9) << solve(one, two, three, n);
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#ifndef BZ
#pragma GCC optimize "-Ofast"
#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 double ld;
typedef unsigned long long ull;

using namespace std;

const int MX = 307;

ld f[MX + 5][MX + 5][MX + 5];

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  int n;
  int c[4] = {0};
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    c[x]++;
  }

  for (int k = 0; k < MX; k++) {
    for (int j = 0; j + k < MX; j++) {
      for (int i = 0; i + j + k < MX; i++) {
        if (i == 0 && j == 0 && k == 0) {
          continue;
        }
        if (i + j + k > MX) {
          continue;
        }
        ld wait = 1.0 * n / (i + j + k);
        ld pi = 1.0 * i / (i + j + k);
        ld pj = 1.0 * j / (i + j + k);
        ld pk = 1.0 * k / (i + j + k);

        f[i][j][k] = wait + pk * f[i][j + 1][k - 1] + pj * f[i + 1][j - 1][k] +
                     pi * f[i - 1][j][k];
      }
    }
  }

  cout.precision(10);
  cout << fixed << f[c[1]][c[2]][c[3]] << "\n";
  return 0;
}


Fixed submission:
#ifndef BZ
#pragma GCC optimize "-Ofast"
#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 double ld;
typedef unsigned long long ull;

using namespace std;

const int MX = 307;

ld f[MX + 5][MX + 5][MX + 5];

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  int n;
  int c[4] = {0};
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    c[x]++;
  }

  for (int k = 0; k < MX; k++) {
    for (int j = 0; j + k < MX; j++) {
      for (int i = 0; i + j + k < MX; i++) {
        if (i == 0 && j == 0 && k == 0) {
          continue;
        }
        if (i + j + k > MX) {
          continue;
        }
        ld wait = 1.0 * n / (i + j + k);
        ld pi = 1.0 * i / (i + j + k);
        ld pj = 1.0 * j / (i + j + k);
        ld pk = 1.0 * k / (i + j + k);

        if (i)
          f[i][j][k] += pi * f[i - 1][j][k];
        if (j)
          f[i][j][k] += pj * f[i + 1][j - 1][k];
        if (k)
          f[i][j][k] += pk * f[i][j + 1][k - 1];

        f[i][j][k] += wait;
      }
    }
  }

  cout.precision(10);
  cout << fixed << f[c[1]][c[2]][c[3]] << "\n";
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int n;
double dp[310][310][310];

double dfs(int x3, int x2, int x1) {
  if (x3 == 0 && x2 == 0 && x1 == 0)
    return 0.0;
  else {
    double sum = (double)(x3 + x2 + x1);
    double ret = (double)n / sum;
    if (x3 > 0)
      ret += dfs(x3 - 1, x2 + 1, x1) * (double)x3 / sum;
    if (x2 > 0)
      ret += dfs(x3, x2 - 1, x1 + 1) * (double)x2 / sum;
    if (x1 > 0)
      ret += dfs(x3, x2, x1 - 1) * (double)x1 / sum;
    return dp[x3][x2][x1] = ret;
  }
}

int main() {
  cin >> n;
  int a[n], x1 = 0, x2 = 0, x3 = 0, x;
  for (size_t i = 0; i < n; i++) {
    cin >> x;
    if (x == 3)
      x3++;
    if (x == 2)
      x2++;
    if (x == 1)
      x1++;
  }
  dp[0][0][0] = 0;
  int i, j, k, h;
  printf("%.12f\n", dfs(x3, x2, x1));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int n;
double dp[310][310][310];

double dfs(int x3, int x2, int x1) {
  if (x3 == 0 && x2 == 0 && x1 == 0)
    return 0.0;
  else if (dp[x3][x2][x1] > 0)
    return dp[x3][x2][x1];
  else {
    double sum = (double)(x3 + x2 + x1);
    double ret = (double)n / sum;
    if (x3 > 0)
      ret += dfs(x3 - 1, x2 + 1, x1) * (double)x3 / sum;
    if (x2 > 0)
      ret += dfs(x3, x2 - 1, x1 + 1) * (double)x2 / sum;
    if (x1 > 0)
      ret += dfs(x3, x2, x1 - 1) * (double)x1 / sum;
    return dp[x3][x2][x1] = ret;
  }
}

int main() {
  cin >> n;
  int a[n], x1 = 0, x2 = 0, x3 = 0, x;
  for (size_t i = 0; i < n; i++) {
    cin >> x;
    if (x == 3)
      x3++;
    if (x == 2)
      x2++;
    if (x == 1)
      x1++;
  }
  dp[0][0][0] = 0;
  int i, j, k, h;
  printf("%.12f\n", dfs(x3, x2, x1));
  return 0;
}
Problem p03169 (C++)

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

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

typedef vector<ld> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

#define INF INT_MAX
#define MOD 1000000007
#define all(x) x.begin(), x.end()

int N;
vi A(4);
vector<vvi> dp(300, vvi(300, vi(300, -1)));

ld f(int a, int b, int c) {
  if (a < 0 or b < 0 or c < 0)
    return 0;
  if (dp[a][b][c] != -1)
    return dp[a][b][c];

  int v = (a + b + c);

  dp[a][b][c] =
      N + a * f(a - 1, b, c) + b * f(a + 1, b - 1, c) + c * f(a, b + 1, c - 1);
  dp[a][b][c] /= ld(v);
  return dp[a][b][c];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N;
  for (int x = 0; x < N; x++) {
    int i;
    cin >> i;
    A[i]++;
  }
  dp[0][0][0] = 0;
  cout << fixed << setprecision(10) << f(A[1], A[2], A[3]) << endl;

  return 0;
}

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

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

typedef vector<ld> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

#define INF INT_MAX
#define MOD 1000000007
#define all(x) x.begin(), x.end()

int N;
vi A(4);
vector<vvi> dp(305, vvi(305, vi(305, -1)));

ld f(int a, int b, int c) {
  if (a < 0 or b < 0 or c < 0)
    return 0;
  if (dp[a][b][c] != -1)
    return dp[a][b][c];

  int v = (a + b + c);

  dp[a][b][c] =
      N + a * f(a - 1, b, c) + b * f(a + 1, b - 1, c) + c * f(a, b + 1, c - 1);
  dp[a][b][c] /= ld(v);
  return dp[a][b][c];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N;
  for (int x = 0; x < N; x++) {
    int i;
    cin >> i;
    A[i]++;
  }
  dp[0][0][0] = 0;
  cout << fixed << setprecision(10) << f(A[1], A[2], A[3]) << endl;

  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
double dp[301][301][301];
int n;
// double dfs(int a, int b, int c){
//     if(a+b+c == 0)
//     return 0.0;
//     if(dp[a][b][c] > -0.5)
//     return dp[a][b][c];
//     double ks = (double)n/ (a+b+c);
//     double ret=0;
//     if(a){
//         ret += (dfs(a-1,b,c) + ks)*(a/(a+b+c));
//     }
//     if(b){
//         ret += (dfs(a+1,b-1,c) + ks)*(b/(a+b+c));
//     }
//     if(c){
//         ret += (dfs(a,b+1,c-1) + ks)*(c/(a+b+c));
//     }
//     return dp[a][b][c]=ret;
// }
double rec(int i, int j, int k) {
  if (!i && !j && !k)
    return 0.0;
  if (dp[i][j][k] > 0.0)
    return dp[i][j][k];

  double sum = i + j + k;
  double ret = n / sum;
  if (i > 0)
    ret += (double)(i / sum) * rec(i - 1, j, k);
  if (j > 0)
    ret += (double)(j / sum) * rec(i + 1, j - 1, k);
  if (k > 0)
    ret += (double)(k / sum) * rec(i, j + 1, k - 1);

  return dp[i][j][k] = ret;
}

int main() {
  // memset(dp, -1, sizeof(dp));
  for (int i = 0; i < 310; i++) {
    for (int j = 0; j < 310; j++) {
      for (int k = 0; k < 310; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  int st[4];
  memset(st, 0, sizeof(st));

  cin >> n;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    st[t]++;
  }
  // cout<<dfs(st[1],st[2],st[3]);
  // cout<<setprecesion(12)<<dfs(st[1],st[2],st[3]);
  cout << fixed << setprecision(12) << rec(st[1], st[2], st[3]) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
double dp[301][301][301];
int n;
// double dfs(int a, int b, int c){
//     if(a+b+c == 0)
//     return 0.0;
//     if(dp[a][b][c] > -0.5)
//     return dp[a][b][c];
//     double ks = (double)n/ (a+b+c);
//     double ret=0;
//     if(a){
//         ret += (dfs(a-1,b,c) + ks)*(a/(a+b+c));
//     }
//     if(b){
//         ret += (dfs(a+1,b-1,c) + ks)*(b/(a+b+c));
//     }
//     if(c){
//         ret += (dfs(a,b+1,c-1) + ks)*(c/(a+b+c));
//     }
//     return dp[a][b][c]=ret;
// }
double rec(int i, int j, int k) {
  if (!i && !j && !k)
    return 0.0;
  if (dp[i][j][k] > 0.0)
    return dp[i][j][k];

  double sum = i + j + k;
  double ret = n / sum;
  if (i > 0)
    ret += (double)(i / sum) * rec(i - 1, j, k);
  if (j > 0)
    ret += (double)(j / sum) * rec(i + 1, j - 1, k);
  if (k > 0)
    ret += (double)(k / sum) * rec(i, j + 1, k - 1);

  return dp[i][j][k] = ret;
}

int main() {
  // memset(dp, -1, sizeof(dp));
  for (int i = 0; i < 301; i++) {
    for (int j = 0; j < 301; j++) {
      for (int k = 0; k < 301; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  int st[4];
  memset(st, 0, sizeof(st));

  cin >> n;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    st[t]++;
  }
  // cout<<dfs(st[1],st[2],st[3]);
  // cout<<setprecesion(12)<<dfs(st[1],st[2],st[3]);
  cout << fixed << setprecision(12) << rec(st[1], st[2], st[3]) << endl;

  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int t;
double dp[301][301][301];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  short a[3] = {0};
  for (int i = 0; i < t; i++) {
    int x;
    cin >> x;
    a[x - 1]++;
  }
  for (int threes = 0; threes <= a[2]; threes++) {
    for (int twos = 0; twos <= a[1] + a[2]; twos++) {
      for (int ones = 0; ones <= a[0] + a[1] + a[2]; ones++) {
        double sum = ones + twos + threes;
        if (sum <= 0.5)
          continue;
        double p_waste = t / sum;
        auto &p = dp[ones][twos][threes];
        if (ones > 0) {
          p += (ones) * (p_waste + dp[ones - 1][twos][threes]);
        }
        if (twos > 0) {
          p += (twos) * (p_waste + dp[ones + 1][twos - 1][threes]);
        }
        if (threes > 0) {
          p += (threes) * (p_waste + dp[ones][twos + 1][threes - 1]);
        }
        p /= sum;
      }
    }
  }
  cout << setprecision(10) << dp[a[0]][a[1]][a[2]];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int t;
double dp[301][301][301];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  short a[3] = {0};
  for (int i = 0; i < t; i++) {
    int x;
    cin >> x;
    a[x - 1]++;
  }
  for (int threes = 0; threes <= a[2]; threes++) {
    for (int twos = 0; twos <= -threes + a[1] + a[2]; twos++) {
      for (int ones = 0; ones <= -threes - twos + a[0] + a[1] + a[2]; ones++) {
        double sum = ones + twos + threes;
        if (sum <= 0.5)
          continue;
        double p_waste = t / sum;
        auto &p = dp[ones][twos][threes];
        if (ones > 0) {
          p += (ones) * (p_waste + dp[ones - 1][twos][threes]);
        }
        if (twos > 0) {
          p += (twos) * (p_waste + dp[ones + 1][twos - 1][threes]);
        }
        if (threes > 0) {
          p += (threes) * (p_waste + dp[ones][twos + 1][threes - 1]);
        }
        p /= sum;
      }
    }
  }
  cout << setprecision(10) << dp[a[0]][a[1]][a[2]];
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int t;
double dp[301][301][301];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  short a[3] = {0};
  for (int i = 0; i < t; i++) {
    int x;
    cin >> x;
    a[x - 1]++;
  }
  for (int threes = 0; threes <= t; threes++) {
    for (int twos = 0; twos <= t; twos++) {
      for (int ones = 0; ones <= t; ones++) {
        double sum = ones + twos + threes;
        if (sum <= 0.5)
          continue;
        double p_waste = t / sum;
        auto &p = dp[ones][twos][threes];
        if (ones > 0) {
          p += (ones) * (p_waste + dp[ones - 1][twos][threes]);
        }
        if (twos > 0) {
          p += (twos) * (p_waste + dp[ones + 1][twos - 1][threes]);
        }
        if (threes > 0) {
          p += (threes) * (p_waste + dp[ones][twos + 1][threes - 1]);
        }
        p /= sum;
      }
    }
  }
  cout << setprecision(10) << dp[a[0]][a[1]][a[2]];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int t;
double dp[301][301][301];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  short a[3] = {0};
  for (int i = 0; i < t; i++) {
    int x;
    cin >> x;
    a[x - 1]++;
  }
  for (int threes = 0; threes <= t; threes++) {
    for (int twos = 0; twos <= t - threes; twos++) {
      for (int ones = 0; ones <= t - threes - twos; ones++) {
        double sum = ones + twos + threes;
        if (sum <= 0.5)
          continue;
        double p_waste = t / sum;
        auto &p = dp[ones][twos][threes];
        if (ones > 0) {
          p += (ones) * (p_waste + dp[ones - 1][twos][threes]);
        }
        if (twos > 0) {
          p += (twos) * (p_waste + dp[ones + 1][twos - 1][threes]);
        }
        if (threes > 0) {
          p += (threes) * (p_waste + dp[ones][twos + 1][threes - 1]);
        }
        p /= sum;
      }
    }
  }
  cout << setprecision(10) << dp[a[0]][a[1]][a[2]];
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#ifdef DBP
#define deb(...) fprintf(stderr, __VA_ARGS__)
#else
#define deb(...) 0
#endif
#define fst first
#define snd second
#define fore(x, a, b) for (int x = (a), qwerty = (b); x < qwerty; x++)
#define pb push_back
#define mset(a, v) memset((a), (v), sizeof(a))
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define FIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
typedef long long ll;

const int N = 301;
int A[N], n;
double dp[N][N][N];

int main() {
  FIN;
  cout << setprecision(12);
  cin >> n;
  fore(x, 0, n) cin >> A[x];
  double nd = n;
  fore(c, 0, n + 1) fore(b, 0, n + 1) fore(a, 0, n + 1) {
    if (a + b + c == 0)
      continue;
    double s = a + b + c, &r = dp[a][b][c] = 1;
    if (a)
      r += dp[a - 1][b][c] * (a / nd);
    if (b)
      r += dp[a + 1][b - 1][c] * (b / nd);
    if (c)
      r += dp[a][b + 1][c - 1] * (c / nd);
    r /= (1 - (nd - s) / nd);
  }
  cout << dp[count(A, A + n, 1)][count(A, A + n, 2)][count(A, A + n, 3)]
       << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#ifdef DBP
#define deb(...) fprintf(stderr, __VA_ARGS__)
#else
#define deb(...) 0
#endif
#define fst first
#define snd second
#define fore(x, a, b) for (int x = (a), qwerty = (b); x < qwerty; x++)
#define pb push_back
#define mset(a, v) memset((a), (v), sizeof(a))
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define FIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
typedef long long ll;

const int N = 301;
int A[N], n;
double dp[N][N][N];

int main() {
  FIN;
  cout << setprecision(12);
  cin >> n;
  fore(x, 0, n) cin >> A[x];
  double nd = n;
  fore(c, 0, n + 1) fore(b, 0, n + 1) fore(a, 0, n + 1) {
    if (a + b + c == 0 || a + b + c > n)
      continue;
    double s = a + b + c, &r = dp[a][b][c] = 1;
    if (a)
      r += dp[a - 1][b][c] * (a / nd);
    if (b)
      r += dp[a + 1][b - 1][c] * (b / nd);
    if (c)
      r += dp[a][b + 1][c - 1] * (c / nd);
    r /= (1 - (nd - s) / nd);
  }
  cout << dp[count(A, A + n, 1)][count(A, A + n, 2)][count(A, A + n, 3)]
       << "\n";
}
Problem p03169 (C++)

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

using namespace std;
const int N = 301;
double dp[N][N][N];
int n;
int cnt[4];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cnt[x]++;
  }

  for (int k = 0; k <= n; 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)
          dp[i][j][k] += 1.0 * i / n * dp[i - 1][j][k];
        if (j)
          dp[i][j][k] += 1.0 * j / n * dp[i + 1][j - 1][k];
        if (k)
          dp[i][j][k] += 1.0 * k / n * dp[i][j + 1][k - 1];
        dp[i][j][k] /= 1 - 1.0 * (n - i - j - k) / n;
      }
    }
  }
  printf("%.10f", dp[cnt[1]][cnt[2]][cnt[3]]);
}

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

using namespace std;
const int N = 302;
double dp[N][N][N];
int n;
int cnt[4];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cnt[x]++;
  }

  for (int k = 0; k <= n; 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)
          dp[i][j][k] += 1.0 * i / n * dp[i - 1][j][k];
        if (j)
          dp[i][j][k] += 1.0 * j / n * dp[i + 1][j - 1][k];
        if (k)
          dp[i][j][k] += 1.0 * k / n * dp[i][j + 1][k - 1];
        dp[i][j][k] /= 1 - 1.0 * (n - i - j - k) / n;
      }
    }
  }
  printf("%.10f", dp[cnt[1]][cnt[2]][cnt[3]]);
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const int INF = 1000000000;

int main() {
  int N;
  cin >> N;
  vector<int> a(3, 0);
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    a[x - 1]++;
  }
  double dp[N + 1][N + 1][N + 1];
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < N + 1; j++) {
      for (int k = 0; k < N + 1; k++) {
        dp[i][j][k] = 0.0;
      }
    }
  }
  for (int k = 0; k <= N; k++) {
    for (int j = 0; j <= N; j++) {
      for (int i = 0; i <= N; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        double sum = i + j + k;
        double t = 0.0;
        if (j > 0)
          t += dp[i + 1][j - 1][k] * j;
        if (k > 0)
          t += dp[i][j + 1][k - 1] * k;
        if (i > 0)
          t += dp[i - 1][j][k] * i;
        dp[i][j][k] = (t + N) / sum;
      }
    }
  }
  cout << setprecision(10) << fixed << dp[a[0]][a[1]][a[2]] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const int INF = 1000000000;

int main() {
  int N;
  cin >> N;
  vector<int> a(3, 0);
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    a[x - 1]++;
  }
  double dp[N + 2][N + 2][N + 2];
  for (int i = 0; i < N + 2; i++) {
    for (int j = 0; j < N + 2; j++) {
      for (int k = 0; k < N + 2; k++) {
        dp[i][j][k] = 0.0;
      }
    }
  }
  for (int k = 0; k <= N; k++) {
    for (int j = 0; j <= N; j++) {
      for (int i = 0; i <= N; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        double sum = i + j + k;
        double t = 0.0;
        if (j > 0)
          t += dp[i + 1][j - 1][k] * j;
        if (k > 0)
          t += dp[i][j + 1][k - 1] * k;
        if (i > 0)
          t += dp[i - 1][j][k] * i;
        dp[i][j][k] = (t + N) / sum;
      }
    }
  }
  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;
using vi = vector<int>;

const int N = 301;

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

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vi cnt(3);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cnt[x - 1]++;
  }
  for (int c = 0; c <= n; c++) {
    for (int b = 0; b <= n; b++) {
      for (int a = 0; a <= n; a++) {
        int d = n - a - b - c;
        if (d == n) {
          continue;
        }
        double p = 0.0;
        if (a)
          p += 1.0 * a / n * (1 + dp[a - 1][b][c]);
        if (b)
          p += 1.0 * b / n * (1 + dp[a + 1][b - 1][c]);
        if (c)
          p += 1.0 * c / n * (1 + dp[a][b + 1][c - 1]);
        if (d)
          p = (p + 1.0 * d / n) / (1 - 1.0 * d / n);
        dp[a][b][c] = p;
      }
    }
  }
  cout << fixed << setprecision(14) << dp[cnt[0]][cnt[1]][cnt[2]] << '\n';
}


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

const int N = 303;

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

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vi cnt(3);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cnt[x - 1]++;
  }
  for (int c = 0; c <= n; c++) {
    for (int b = 0; b <= n; b++) {
      for (int a = 0; a <= n; a++) {
        int d = n - a - b - c;
        if (d == n) {
          continue;
        }
        double p = 0.0;
        if (a)
          p += 1.0 * a / n * (1 + dp[a - 1][b][c]);
        if (b)
          p += 1.0 * b / n * (1 + dp[a + 1][b - 1][c]);
        if (c)
          p += 1.0 * c / n * (1 + dp[a][b + 1][c - 1]);
        if (d)
          p = (p + 1.0 * d / n) / (1 - 1.0 * d / n);
        dp[a][b][c] = p;
      }
    }
  }
  cout << fixed << setprecision(14) << dp[cnt[0]][cnt[1]][cnt[2]] << '\n';
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int c[4] = {};
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    c[a]++;
  }
  double dp[301][301][301] = {};
  for (int k = 0; k <= c[3]; k++)
    for (int j = 0; j <= c[2] + c[3]; j++)
      for (int i = 0; i <= c[1] + c[2] + c[3]; i++) {
        if (i + j + k == 0)
          continue;
        if (i > 0)
          dp[i][j][k] += dp[i - 1][j][k] * i / n;
        if (j > 0)
          dp[i][j][k] += dp[i + 1][j - 1][k] * j / n;
        if (k > 0)
          dp[i][j][k] += dp[i][j + 1][k - 1] * k / n;
        dp[i][j][k] += 1;
        dp[i][j][k] *= (double)n / (i + j + k);
      }
  cout << setprecision(12) << dp[c[1]][c[2]][c[3]] << endl;
}


Fixed submission:
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int c[4] = {};
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    c[a]++;
  }
  double dp[302][302][302] = {};
  for (int k = 0; k <= c[3]; k++)
    for (int j = 0; j <= c[2] + c[3]; j++)
      for (int i = 0; i <= c[1] + c[2] + c[3]; i++) {
        if (i + j + k == 0)
          continue;
        if (i > 0)
          dp[i][j][k] += dp[i - 1][j][k] * i / n;
        if (j > 0)
          dp[i][j][k] += dp[i + 1][j - 1][k] * j / n;
        if (k > 0)
          dp[i][j][k] += dp[i][j + 1][k - 1] * k / n;
        dp[i][j][k] += 1;
        dp[i][j][k] *= (double)n / (i + j + k);
      }
  cout << setprecision(12) << dp[c[1]][c[2]][c[3]] << endl;
}
Problem p03169 (C++)

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

using namespace std;

const int N = 301;
double dp[N][N][N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(9);

  int n;
  cin >> n;
  array<int, 4> cnt{};
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    ++cnt[a];
    sum += a;
  }

  for (int s = 1; s <= sum; ++s) {
    for (int j = 0; j <= n; ++j) {
      for (int k = 0; k <= n; ++k) {
        int i = s - 2 * j - 3 * k;
        if (i < 0 || i > n)
          continue;
        dp[i][j][k] = n;
        if (i > 0)
          dp[i][j][k] += i * dp[i - 1][j][k];
        if (j > 0)
          dp[i][j][k] += j * dp[i + 1][j - 1][k];
        if (k > 0)
          dp[i][j][k] += k * dp[i][j + 1][k - 1];
        dp[i][j][k] /= (i + j + k);
      }
    }
  }

  cout << dp[cnt[1]][cnt[2]][cnt[3]] << endl;

  return 0;
}


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

using namespace std;

const int N = 310;
double dp[N][N][N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(9);

  int n;
  cin >> n;
  array<int, 4> cnt{};
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    ++cnt[a];
    sum += a;
  }

  for (int s = 1; s <= sum; ++s) {
    for (int j = 0; j <= n; ++j) {
      for (int k = 0; k <= n; ++k) {
        int i = s - 2 * j - 3 * k;
        if (i < 0 || i > n)
          continue;
        dp[i][j][k] = n;
        if (i > 0)
          dp[i][j][k] += i * dp[i - 1][j][k];
        if (j > 0)
          dp[i][j][k] += j * dp[i + 1][j - 1][k];
        if (k > 0)
          dp[i][j][k] += k * dp[i][j + 1][k - 1];
        dp[i][j][k] /= (i + j + k);
      }
    }
  }

  cout << dp[cnt[1]][cnt[2]][cnt[3]] << endl;

  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IO_OP                                                                  \
  std::ios::sync_with_stdio(0);                                                \
  std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x)                                                               \
  cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7;

double dp[303][501][901];

signed main() {
  IO_OP;

  int n, sum = 0;
  cin >> n;
  int cnt[3] = {};
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    cnt[x - 1]++;
  }
  for (int k = 0; k * 3 <= sum; k++)
    for (int j = 0; k * 3 + j * 2 <= sum; j++)
      for (int i = 0; i + k * 3 + j * 2 <= sum; i++) {
        if (i == 0 && j == 0 && k == 0) {
          dp[i][j][k] = 0;
        } else {
          int left = n - i - j - k;
          double c = 1 - (double)left / n;
          dp[i][j][k] = (1 + (i ? dp[i - 1][j][k] : 0.) * i / n +
                         (j ? dp[i + 1][j - 1][k] : 0.) * j / n +
                         (k ? dp[i][j + 1][k - 1] : 0.) * k / n) /
                        c;
        }
      }

  cout << fixed << setprecision(10) << dp[cnt[0]][cnt[1]][cnt[2]] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define IO_OP                                                                  \
  std::ios::sync_with_stdio(0);                                                \
  std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x)                                                               \
  cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7;

double dp[901][501][303];

signed main() {
  IO_OP;

  int n, sum = 0;
  cin >> n;
  int cnt[3] = {};
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    cnt[x - 1]++;
  }
  for (int k = 0; k * 3 <= sum; k++)
    for (int j = 0; k * 3 + j * 2 <= sum; j++)
      for (int i = 0; i + k * 3 + j * 2 <= sum; i++) {
        if (i == 0 && j == 0 && k == 0) {
          dp[i][j][k] = 0;
        } else {
          int left = n - i - j - k;
          double c = 1 - (double)left / n;
          dp[i][j][k] = (1 + (i ? dp[i - 1][j][k] : 0.) * i / n +
                         (j ? dp[i + 1][j - 1][k] : 0.) * j / n +
                         (k ? dp[i][j + 1][k - 1] : 0.) * k / n) /
                        c;
        }
      }

  cout << fixed << setprecision(10) << dp[cnt[0]][cnt[1]][cnt[2]] << endl;
}
Problem p03169 (C++)

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

// dp[0][0][0] = 0
// ans = dp[#{i| a[i] == 1}][][#{k|a[k] == 3}]
// dp[i][j][k] = 1 + ((N - i - j - k) / N) * dp[i][j][k] + (i / N) * dp[i -
// 1][j][k] + ... (i + j + k) / N * dp[i][j][k] = 1 + (i / N) * dp[i - 1][j][k]
// + (j / N) * dp[i + 1][j - 1][k] + ...
double dp[301][301][301];
int N;

int main() {
  int first_cnt[4] = {};
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    first_cnt[a]++;
  }
  for (int k = 0; k <= first_cnt[3]; k++) {
    for (int j = 0; j <= first_cnt[2] + first_cnt[3]; j++) {
      for (int i = 0; i <= N; i++) {
        if (i == 0 && j == 0 && k == 0) {
          continue;
        }
        dp[i][j][k] += 1.0 * N / (i + j + k);
        if (i > 0) {
          dp[i][j][k] += 1.0 * i / (i + j + k) * dp[i - 1][j][k];
        }
        if (j > 0) {
          dp[i][j][k] += 1.0 * j / (i + j + k) * dp[i + 1][j - 1][k];
        }
        if (k > 0) {
          dp[i][j][k] += 1.0 * k / (i + j + k) * dp[i][j + 1][k - 1];
        }
      }
    }
  }
  cout << setprecision(10) << dp[first_cnt[1]][first_cnt[2]][first_cnt[3]]
       << endl;
}


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

// dp[0][0][0] = 0
// ans = dp[#{i| a[i] == 1}][][#{k|a[k] == 3}]
// dp[i][j][k] = 1 + ((N - i - j - k) / N) * dp[i][j][k] + (i / N) * dp[i -
// 1][j][k] + ... (i + j + k) / N * dp[i][j][k] = 1 + (i / N) * dp[i - 1][j][k]
// + (j / N) * dp[i + 1][j - 1][k] + ...
double dp[302][302][302];
int N;

int main() {
  int first_cnt[4] = {};
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    first_cnt[a]++;
  }
  for (int k = 0; k <= first_cnt[3]; k++) {
    for (int j = 0; j <= first_cnt[2] + first_cnt[3]; j++) {
      for (int i = 0; i <= N; i++) {
        if (i == 0 && j == 0 && k == 0) {
          continue;
        }
        dp[i][j][k] += 1.0 * N / (i + j + k);
        if (i > 0) {
          dp[i][j][k] += 1.0 * i / (i + j + k) * dp[i - 1][j][k];
        }
        if (j > 0) {
          dp[i][j][k] += 1.0 * j / (i + j + k) * dp[i + 1][j - 1][k];
        }
        if (k > 0) {
          dp[i][j][k] += 1.0 * k / (i + j + k) * dp[i][j + 1][k - 1];
        }
      }
    }
  }
  cout << setprecision(10) << dp[first_cnt[1]][first_cnt[2]][first_cnt[3]]
       << endl;
}
Problem p03169 (C++)

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

using namespace std;

const int MAX_N = 100;

double p[1 + MAX_N][1 + MAX_N][1 + MAX_N];

int main() {
  int n;
  scanf("%d", &n);
  vector<int> count(3, 0);
  for (int i = 0; i < n; i++) {
    int ai;
    scanf("%d", &ai);
    ai--;
    count[ai]++;
  }
  p[count[0]][count[1]][count[2]] = 1;
  double answer = 0;
  for (int three = n; three >= 0; three--) {
    for (int two = n; two >= 0; two--) {
      for (int one = n; one >= 0; one--) {
        int sum = one + two + three;
        if (sum > 0) {
          double waitTime = (double)n / sum;
          answer += p[one][two][three] * waitTime;
          if (two < n && three > 0) {
            p[one][two + 1][three - 1] += three * p[one][two][three] / sum;
          }
          if (one < n && two > 0) {
            p[one + 1][two - 1][three] += two * p[one][two][three] / sum;
          }
          if (one > 0) {
            p[one - 1][two][three] += one * p[one][two][three] / sum;
          }
        }
      }
    }
  }
  printf("%.14lf\n", answer);
  return 0;
}


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

using namespace std;

const int MAX_N = 300;

double p[1 + MAX_N][1 + MAX_N][1 + MAX_N];

int main() {
  int n;
  scanf("%d", &n);
  vector<int> count(3, 0);
  for (int i = 0; i < n; i++) {
    int ai;
    scanf("%d", &ai);
    ai--;
    count[ai]++;
  }
  p[count[0]][count[1]][count[2]] = 1;
  double answer = 0;
  for (int three = n; three >= 0; three--) {
    for (int two = n; two >= 0; two--) {
      for (int one = n; one >= 0; one--) {
        int sum = one + two + three;
        if (sum > 0) {
          double waitTime = (double)n / sum;
          answer += p[one][two][three] * waitTime;
          if (two < n && three > 0) {
            p[one][two + 1][three - 1] += three * p[one][two][three] / sum;
          }
          if (one < n && two > 0) {
            p[one + 1][two - 1][three] += two * p[one][two][three] / sum;
          }
          if (one > 0) {
            p[one - 1][two][three] += one * p[one][two][three] / sum;
          }
        }
      }
    }
  }
  printf("%.14lf\n", answer);
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

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

  unsigned N;

  std::cin >> N;
  vector<vector<vector<double>>> dp(
      N + 2, vector<vector<double>>(N + 2, vector<double>(N + 2, 0.0)));
  vector<int> vals(4, 0);

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

  int max = vals[1] + vals[2] + vals[3];

  for (int z = 0; z <= max; ++z) {
    for (int y = 0; y <= max; ++y) {
      for (int x = 0; x <= max; ++x) {
        if (!x && !y && !z) {
          dp[0][0][0] = 0.0;
          continue;
        }
        double sum = N;
        if (x > 0)
          sum += x * dp[x - 1][y][z];
        if (y > 0)
          sum += y * dp[x + 1][y - 1][z];
        if (z > 0)
          sum += z * dp[x][y + 1][z - 1];
        dp[x][y][z] = sum / (x + y + z);
        if (x == vals[1] && y == vals[2] && z == vals[3]) {
          std::cout << std::setprecision(20) << dp[x][y][z];
          return 0;
        }
      }
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

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

  unsigned N;

  std::cin >> N;
  vector<vector<vector<double>>> dp(
      N + 2, vector<vector<double>>(N + 2, vector<double>(N + 2, 0.0)));
  vector<int> vals(4, 0);

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

  for (int z = 0; z <= vals[3]; ++z) {
    for (int y = 0; y <= vals[2] + vals[3]; ++y) {
      for (int x = 0; x <= vals[1] + vals[2] + vals[3]; ++x) {
        if (x + y + z == 0) {
          dp[0][0][0] = 0.0;
          continue;
        }
        double sum = N;
        if (x > 0)
          sum += x * dp[x - 1][y][z];
        if (y > 0)
          sum += y * dp[x + 1][y - 1][z];
        if (z > 0)
          sum += z * dp[x][y + 1][z - 1];
        dp[x][y][z] = sum / (x + y + z);
        if (x == vals[1] && y == vals[2] && z == vals[3]) {
          std::cout << std::setprecision(20) << dp[x][y][z];
          return 0;
        }
      }
    }
  }

  return 0;
}
Problem p03169 (C++)

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

using ll = long long;
using ld = long double;

const ll inf = 1e18;
const int N = 2 * 1e2 + 10;

ll res;
ll a[N], h[5];

ld dp[N][N][N];

void solve() {
  ll n, x;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    ++h[x];
  }
  for (int k = 0; k < N; ++k) {
    for (int j = 0; j + k < N; ++j) {
      for (int i = 0; i + j + k < N; ++i) {
        if (i + j + k == 0)
          continue;
        if (i + j + k > n)
          continue;
        ld pi = 1.0 * i / (i + j + k);
        ld pj = 1.0 * j / (i + j + k);
        ld pk = 1.0 * k / (i + j + k);
        dp[i][j][k] = 1.0 * n / (i + j + k);
        if (i)
          dp[i][j][k] += pi * dp[i - 1][j][k];
        if (j)
          dp[i][j][k] += pj * dp[i + 1][j - 1][k];
        if (k)
          dp[i][j][k] += pk * dp[i][j + 1][k - 1];
      }
    }
  }
  cout << fixed << setprecision(10) << dp[h[1]][h[2]][h[3]];
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}
// nEro

Fixed submission:
//                                      KiSmAt
#include "bits/stdc++.h"
using namespace std;

using ll = long long;
using ld = long double;

const ll inf = 1e18;
const int N = 3 * 1e2 + 10;

ll res;
ll a[N], h[5];

ld dp[N][N][N];

void solve() {
  ll n, x;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    ++h[x];
  }
  for (int k = 0; k < N; ++k) {
    for (int j = 0; j + k < N; ++j) {
      for (int i = 0; i + j + k < N; ++i) {
        if (i + j + k == 0)
          continue;
        if (i + j + k > n)
          continue;
        ld pi = 1.0 * i / (i + j + k);
        ld pj = 1.0 * j / (i + j + k);
        ld pk = 1.0 * k / (i + j + k);
        dp[i][j][k] = 1.0 * n / (i + j + k);
        if (i)
          dp[i][j][k] += pi * dp[i - 1][j][k];
        if (j)
          dp[i][j][k] += pj * dp[i + 1][j - 1][k];
        if (k)
          dp[i][j][k] += pk * dp[i][j + 1][k - 1];
      }
    }
  }
  cout << fixed << setprecision(10) << dp[h[1]][h[2]][h[3]];
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}
// nEro
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mx 1000000007
#define db double

using namespace std;

db dp[100 + 1][100 + 1][100 + 1] = {0}, n;

db solve(int c1, int c2, int c3) {
  if (dp[c1][c2][c3] != 0)
    return dp[c1][c2][c3];
  if (c1 == 0 && c2 == 0 && c3 == 0)
    return dp[c1][c2][c3] = 0;
  db j = 0, k = 0, l = 0, sum = c1 + c2 + c3;
  if (c1 != 0) {
    j = (c1 / sum) * solve(c1 - 1, c2, c3);
  }
  if (c2 != 0) {
    k = (c2 / sum) * solve(c1 + 1, c2 - 1, c3);
  }
  if (c3 != 0) {
    l = (c3 / sum) * solve(c1, c2 + 1, c3 - 1);
  }
  dp[c1][c2][c3] = (n / (sum)) + j + k + l;
  return dp[c1][c2][c3];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int c1 = 0, c2 = 0, c3 = 0;
  int sz;
  cin >> sz;
  n = sz;
  int p[sz + 1];
  for (int i = 1; i <= sz; ++i) {
    cin >> p[i];
    if (p[i] == 1)
      ++c1;
    if (p[i] == 2)
      ++c2;
    if (p[i] == 3)
      ++c3;
  }
  cout << fixed << setprecision(15) << solve(c1, c2, c3);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mx 1000000007
#define db double

using namespace std;

db dp[301][301][301] = {0}, n;

db solve(int c1, int c2, int c3) {
  if (dp[c1][c2][c3] != 0)
    return dp[c1][c2][c3];
  if (c1 == 0 && c2 == 0 && c3 == 0)
    return dp[c1][c2][c3] = 0;
  db j = 0, k = 0, l = 0, sum = c1 + c2 + c3;
  if (c1 != 0) {
    j = (c1 / sum) * solve(c1 - 1, c2, c3);
  }
  if (c2 != 0) {
    k = (c2 / sum) * solve(c1 + 1, c2 - 1, c3);
  }
  if (c3 != 0) {
    l = (c3 / sum) * solve(c1, c2 + 1, c3 - 1);
  }
  dp[c1][c2][c3] = (n / (sum)) + j + k + l;
  return dp[c1][c2][c3];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int c1 = 0, c2 = 0, c3 = 0;
  int sz;
  cin >> sz;
  n = sz;
  int p[sz + 1];
  for (int i = 1; i <= sz; ++i) {
    cin >> p[i];
    if (p[i] == 1)
      ++c1;
    if (p[i] == 2)
      ++c2;
    if (p[i] == 3)
      ++c3;
  }
  cout << fixed << setprecision(15) << solve(c1, c2, c3);
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):


#include <bits/stdc++.h>
using namespace std;
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 (dp[x][y][z] > -0.9) {
    return dp[x][y][z];
  }

  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() {
  int n;
  cin >> n;
  int one, two, three;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x == 1) {
      one++;
    } else if (x == 2) {
      two++;
    } else if (x == 3) {
      three++;
    }
  }

  cout << fixed << setprecision(10) << solve(one, two, three, n);
}


Fixed submission:


#include <bits/stdc++.h>
using namespace std;
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 (dp[x][y][z] > -0.9) {
    return dp[x][y][z];
  }

  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() {
  int n;
  cin >> n;
  int one = 0, two = 0, three = 0;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x == 1) {
      one++;
    } else if (x == 2) {
      two++;
    } else if (x == 3) {
      three++;
    }
  }

  cout << fixed << setprecision(10) << solve(one, two, three, n);
}
Problem p03169 (C++)

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

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

  vector<int> numSushi(4);
  int total = 0;
  for (int i = 0; i < N; ++i) {
    ++numSushi[a[i]];
    total += a[i];
  }
  // dp[i][j][k]: 寿司が1個, 2個, 3個の皿が i枚, j枚,
  // k枚のとき、全ての寿司を食べるのに必要な操作回数の期待値
  double dp[31][31][301] = {};
  for (int k = 0; k <= N; ++k) {
    for (int j = 0; j <= N; ++j) {
      for (int i = 0; i <= N; ++i) {
        if (0 < i + j + k and i + j + k <= N) {
          if (i != 0)
            dp[i][j][k] += dp[i - 1][j][k] * i;
          if (j != 0)
            dp[i][j][k] += dp[i + 1][j - 1][k] * j;
          if (k != 0)
            dp[i][j][k] += dp[i][j + 1][k - 1] * k;
          dp[i][j][k] = (dp[i][j][k] + N) / (i + j + k);
        }
      }
    }
  }
  cout << fixed << setprecision(9) << dp[numSushi[1]][numSushi[2]][numSushi[3]]
       << endl;

  return 0;
}

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

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

  vector<int> numSushi(4);
  int total = 0;
  for (int i = 0; i < N; ++i) {
    ++numSushi[a[i]];
    total += a[i];
  }
  // dp[i][j][k]: 寿司が1個, 2個, 3個の皿が i枚, j枚,
  // k枚のとき、全ての寿司を食べるのに必要な操作回数の期待値
  double dp[301][301][301] = {};
  for (int k = 0; k <= N; ++k) {
    for (int j = 0; j <= N; ++j) {
      for (int i = 0; i <= N; ++i) {
        if (0 < i + j + k and i + j + k <= N) {
          if (i != 0)
            dp[i][j][k] += dp[i - 1][j][k] * i;
          if (j != 0)
            dp[i][j][k] += dp[i + 1][j - 1][k] * j;
          if (k != 0)
            dp[i][j][k] += dp[i][j + 1][k - 1] * k;
          dp[i][j][k] = (dp[i][j][k] + N) / (i + j + k);
        }
      }
    }
  }
  cout << fixed << setprecision(9) << dp[numSushi[1]][numSushi[2]][numSushi[3]]
       << endl;

  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;
int sum[4];
double dp[31][31][2];

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%d", &a);
    sum[a]++;
  }
  double fail;
  for (int k = 0; k <= sum[3]; k++)
    for (int j = 0; j <= sum[3] + sum[2] - k; j++)
      for (int i = 0; i <= N - j - k; i++) {
        fail = 1.0 / (1 - (double)(N - i - j - k) / N);
        double divis = (i + j + k);

        dp[i][j][k % 2] = 0;

        if (i) {

          dp[i][j][k % 2] += i / divis * (dp[i - 1][j][k % 2]);
        }
        if (j)
          dp[i][j][k % 2] += j / divis * (dp[i + 1][j - 1][k % 2]);
        if (k)
          dp[i][j][k % 2] += k / divis * (dp[i][j + 1][(k + 1) % 2]);
        if (i + j + k)
          dp[i][j][k % 2] += fail;
      }

  printf("%.9lf\n", dp[sum[1]][sum[2]][sum[3] % 2]);
}

Fixed submission:
#include <iostream>

using namespace std;
int sum[4];
double dp[301][301][2];

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%d", &a);
    sum[a]++;
  }
  double fail;
  for (int k = 0; k <= sum[3]; k++)
    for (int j = 0; j <= sum[3] + sum[2] - k; j++)
      for (int i = 0; i <= N - j - k; i++) {
        fail = 1.0 / (1 - (double)(N - i - j - k) / N);
        double divis = (i + j + k);

        dp[i][j][k % 2] = 0;

        if (i) {

          dp[i][j][k % 2] += i / divis * (dp[i - 1][j][k % 2]);
        }
        if (j)
          dp[i][j][k % 2] += j / divis * (dp[i + 1][j - 1][k % 2]);
        if (k)
          dp[i][j][k % 2] += k / divis * (dp[i][j + 1][(k + 1) % 2]);
        if (i + j + k)
          dp[i][j][k % 2] += fail;
      }

  printf("%.9lf\n", dp[sum[1]][sum[2]][sum[3] % 2]);
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;
int sum[4];
double dp[31][31][31];

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%d", &a);
    sum[a]++;
  }
  double fail;
  for (int k = 0; k <= sum[3]; k++)
    for (int j = 0; j <= sum[3] + sum[2] - k; j++)
      for (int i = 0; i <= N - j - k; i++) {
        fail = 1.0 / (1 - (double)(N - i - j - k) / N);
        double divis = (i + j + k);
        if (i) {

          dp[i][j][k] += i / divis * (dp[i - 1][j][k]);
        }
        if (j)
          dp[i][j][k] += j / divis * (dp[i + 1][j - 1][k]);
        if (k)
          dp[i][j][k] += k / divis * (dp[i][j + 1][k - 1]);
        if (i + j + k)
          dp[i][j][k] += fail;
      }
  printf("%.9lf\n", dp[sum[1]][sum[2]][sum[3]]);
}

Fixed submission:
#include <iostream>

using namespace std;
int sum[4];
double dp[301][301][301];

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%d", &a);
    sum[a]++;
  }
  double fail;
  for (int k = 0; k <= sum[3]; k++)
    for (int j = 0; j <= sum[3] + sum[2] - k; j++)
      for (int i = 0; i <= N - j - k; i++) {
        fail = 1.0 / (1 - (double)(N - i - j - k) / N);
        double divis = (i + j + k);
        if (i) {

          dp[i][j][k] += i / divis * (dp[i - 1][j][k]);
        }
        if (j)
          dp[i][j][k] += j / divis * (dp[i + 1][j - 1][k]);
        if (k)
          dp[i][j][k] += k / divis * (dp[i][j + 1][k - 1]);
        if (i + j + k)
          dp[i][j][k] += fail;
      }
  printf("%.9lf\n", dp[sum[1]][sum[2]][sum[3]]);
}