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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
double dp[303][303][303] = {};
double dfs(int i, int j, int k, int n) {
  // if(dp[i][j][k]!=-1)      return dp[i][j][k];
  if ((i < 0) || (j < 0) || (k < 0))
    return 0;
  else if ((i == 0) && (j == 0) && (k == 0))
    return dp[i][j][k] = 0;
  else if (dp[i][j][k] != -1)
    return dp[i][j][k];
  return dp[i][j][k] =
             ((double)n / (i + j + k)) +
             ((((double)i) / (i + j + k) * (dfs(i - 1, j, k, n))) +
              (((double)j) / (i + j + k) * (dfs(i + 1, j - 1, k, n))) +
              (((double)k) / (i + j + k) * (dfs(i, j + 1, k - 1, n))));
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // MEM(dp,-1);
  for (int i = 0; i <= 303; i++)
    for (int j = 0; j < 303; j++)
      for (int k = 0; k < 303; k++)
        dp[i][j][k] = -1;
  int i, n;
  cin >> n;
  int a = 0, b = 0, c = 0, z = 0, count[4] = {};
  for (i = 0; i < n; i++) {
    cin >> z;
    if (z == 1)
      a++;
    if (z == 2)
      b++;
    if (z == 3)
      c++;
  }
  double ans = dfs(a, b, c, n);
  cout << fixed << setprecision(12) << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
double dp[303][303][303] = {};
double dfs(int i, int j, int k, int n) {
  // if(dp[i][j][k]!=-1)      return dp[i][j][k];
  if ((i < 0) || (j < 0) || (k < 0))
    return 0;
  else if ((i == 0) && (j == 0) && (k == 0))
    return dp[i][j][k] = 0;
  else if (dp[i][j][k] != -1)
    return dp[i][j][k];
  return dp[i][j][k] =
             ((double)n / (i + j + k)) +
             ((((double)i) / (i + j + k) * (dfs(i - 1, j, k, n))) +
              (((double)j) / (i + j + k) * (dfs(i + 1, j - 1, k, n))) +
              (((double)k) / (i + j + k) * (dfs(i, j + 1, k - 1, n))));
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // MEM(dp,-1);
  for (int i = 0; i < 303; i++)
    for (int j = 0; j < 303; j++)
      for (int k = 0; k < 303; k++)
        dp[i][j][k] = -1;
  int i, n;
  cin >> n;
  int a = 0, b = 0, c = 0, z = 0, count[4] = {};
  for (i = 0; i < n; i++) {
    cin >> z;
    if (z == 1)
      a++;
    if (z == 2)
      b++;
    if (z == 3)
      c++;
  }
  double ans = dfs(a, b, c, n);
  cout << fixed << setprecision(12) << ans;
}
Problem p03169 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; i++)
#define frab(i, a, b) for (int i = a; i < b; i++)
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<vector<ll>> mat;

const ll MOD = 1e9 + 7;
const ll INF = 2e15 + 10;
const ll MAX = 1e14 + 10;
const ld EPS = 1e-9;
const int N = 302;
const int M = 110;

ld d[N * 3][N * 3 / 2][N];
int cnt[N];
int a[N * 2];

void solve() {
  int n;
  cin >> n;
  fr(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }

  ld k = n;
  int ssum = accumulate(a, a + n, 0);
  d[0][0][0] = 0;
  for (int sum = 1; sum <= ssum; sum++)
    for (int c1 = 0; c1 <= sum; c1++)
      for (int c2 = 0; 2 * c2 <= sum; c2++) {
        int c3 = (sum - c1 - 2 * c2) / 3;
        if (c3 < 0 || (sum - c1 - 2 * c2) % 3)
          continue;
        d[c1][c2][c3] = ((c1 ? (c1 / k) * d[c1 - 1][c2][c3] : 0) +
                         (c2 ? (c2 / k) * d[c1 + 1][c2 - 1][c3] : 0) +
                         (c3 ? (c3 / k) * d[c1][c2 + 1][c3 - 1] : 0) + 1) /
                        (c1 + c2 + c3) * (ld)(n);
      }
  cout << fixed << setprecision(12) << d[cnt[1]][cnt[2]][cnt[3]];
}

int main() {
  // freopen("a.in", "r", stdin);
  ios_base::sync_with_stdio(false);
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; i++)
#define frab(i, a, b) for (int i = a; i < b; i++)
#define pb push_back

using namespace std;

typedef long long ll;
typedef double ld;
typedef vector<vector<ll>> mat;

const ll MOD = 1e9 + 7;
const ll INF = 2e15 + 10;
const ll MAX = 1e14 + 10;
const ld EPS = 1e-9;
const int N = 302;
const int M = 110;

ld d[N * 3][N * 3 / 2][N];
int cnt[N];
int a[N * 2];

void solve() {
  int n;
  cin >> n;
  fr(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }

  ld k = n;
  int ssum = accumulate(a, a + n, 0);
  d[0][0][0] = 0;
  for (int sum = 1; sum <= ssum; sum++)
    for (int c1 = 0; c1 <= sum; c1++)
      for (int c2 = 0; 2 * c2 <= sum; c2++) {
        int c3 = (sum - c1 - 2 * c2) / 3;
        if (c3 < 0 || (sum - c1 - 2 * c2) % 3)
          continue;
        d[c1][c2][c3] = ((c1 ? (c1 / k) * d[c1 - 1][c2][c3] : 0) +
                         (c2 ? (c2 / k) * d[c1 + 1][c2 - 1][c3] : 0) +
                         (c3 ? (c3 / k) * d[c1][c2 + 1][c3 - 1] : 0) + 1) /
                        (c1 + c2 + c3) * (ld)(n);
      }
  cout << fixed << setprecision(12) << d[cnt[1]][cnt[2]][cnt[3]];
}

int main() {
  // freopen("a.in", "r", stdin);
  ios_base::sync_with_stdio(false);
  solve();
}
Problem p03169 (C++)

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

using namespace std;
typedef double db;
db dp[maxn][maxn][maxn];
int n, k, cnt[4];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> k;
    cnt[k]++;
  }
  for (int sum = 1; sum <= 3 * n; sum++)
    for (int k = 0; k * 3 <= sum; k++)
      for (int j = 0; j * 2 + k * 3 <= sum; j++) {
        int i = sum - j * 2 - k * 3;
        if (i > 0)
          dp[i][j][k] += (db)i / (i + j + k) * dp[i - 1][j][k];
        if (j > 0)
          dp[i][j][k] += (db)j / (i + j + k) * dp[i + 1][j - 1][k];
        if (k > 0)
          dp[i][j][k] += (db)k / (i + j + k) * dp[i][j + 1][k - 1];
        dp[i][j][k] += (db)n / (i + j + k);
      }
  printf("%.13f\n", dp[cnt[1]][cnt[2]][cnt[3]]);
}


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

using namespace std;
typedef double db;
db dp[maxn][maxn][maxn];
int n, k, cnt[4];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> k;
    cnt[k]++;
  }
  for (int sum = 1; sum <= 3 * n; sum++)
    for (int k = 0; k * 3 <= sum; k++)
      for (int j = 0; j * 2 + k * 3 <= sum; j++) {
        int i = sum - j * 2 - k * 3;
        if (i + j + k > n)
          continue;
        if (i > 0)
          dp[i][j][k] += (db)i / (i + j + k) * dp[i - 1][j][k];
        if (j > 0)
          dp[i][j][k] += (db)j / (i + j + k) * dp[i + 1][j - 1][k];
        if (k > 0)
          dp[i][j][k] += (db)k / (i + j + k) * dp[i][j + 1][k - 1];
        dp[i][j][k] += (db)n / (i + j + k);
      }
  printf("%.13f\n", dp[cnt[1]][cnt[2]][cnt[3]]);
}
Problem p03169 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define rrep(i, n) RFOR(i, n, 0)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int n;
const int max_n = 303;
double dp[max_n][max_n][max_n];
// dp[i1][i2][i3]
// :寿司j個の皿がij枚あるとき、すべての寿司がなくなるまでの操作回数の期待値

double calc(int i, int j, int k) {
  if (!i && !j && !k)
    return 0.0;
  if (dp[i][j][k] > 0)
    return dp[i][j][k];

  int l = i + j + k;
  double ret = 1.0 * n / l;
  if (i)
    ret += 1.0 * i / l * calc(i - 1, j, k);
  if (j)
    ret += 1.0 * j / l * calc(i + 1, j - 1, k);
  if (k)
    ret += 1.0 * k / l * calc(i, j + 1, k - 1);

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

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

  cin >> n;

  int cnt[4] = {};
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }

  rep(i, max_n) rep(j, max_n) rep(k, max_n) dp[i][j][k] = 0;

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        calc(i, j, k);
      }
    }
  }

  cout << fixed << setprecision(11) << dp[cnt[1]][cnt[2]][cnt[3]] << endl;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define rrep(i, n) RFOR(i, n, 0)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int n;
const int max_n = 303;
double dp[max_n][max_n][max_n];
// dp[i1][i2][i3]
// :寿司j個の皿がij枚あるとき、すべての寿司がなくなるまでの操作回数の期待値

double calc(int i, int j, int k) {
  if (!i && !j && !k)
    return 0.0;
  if (dp[i][j][k] > 0)
    return dp[i][j][k];

  int l = i + j + k;
  double ret = 1.0 * n / l;
  if (i)
    ret += 1.0 * i / l * calc(i - 1, j, k);
  if (j)
    ret += 1.0 * j / l * calc(i + 1, j - 1, k);
  if (k)
    ret += 1.0 * k / l * calc(i, j + 1, k - 1);

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

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

  cin >> n;

  int cnt[4] = {};
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }

  rep(i, max_n) rep(j, max_n) rep(k, max_n) dp[i][j][k] = 0;

  cout << fixed << setprecision(11) << calc(cnt[1], cnt[2], cnt[3]) << endl;
}
Problem p03169 (C++)

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

ll fastpower(ll b, ll p) {
  ll k = p % 2 ? b : 1;
  return p == 0 ? 1 : k * (fastpower(b * b, p / 2));
}

ld dp[105][105][105];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll cnt[4] = {0, 0, 0, 0};
  ll x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    cnt[x]++;
  }
  dp[0][0][0] = 0;
  for (ll three = 0; three <= n; three++) {
    for (ll two = 0; two <= n; two++) {
      for (ll one = 0; one <= n; one++) {
        ll zero = n - two - three - one;
        if (zero < 0 || zero == n)
          continue;
        ld value = 1.0;
        if (three > 0)
          value += (1.0 * three / n) * dp[three - 1][two + 1][one];
        if (two > 0)
          value += (1.0 * two / n) * dp[three][two - 1][one + 1];
        if (one > 0)
          value += (1.0 * one / n) * dp[three][two][one - 1];
        // dp[three][two][one]=(value)/(1-1.0*zero/n);
        dp[three][two][one] = (value * n) / (1.0 * (one + two + three));
      }
    }
  }
  ld ans = dp[cnt[3]][cnt[2]][cnt[1]];
  cout.precision(20);
  cout << ans;
}

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

ll fastpower(ll b, ll p) {
  ll k = p % 2 ? b : 1;
  return p == 0 ? 1 : k * (fastpower(b * b, p / 2));
}

ld dp[305][305][305];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll cnt[4] = {0, 0, 0, 0};
  ll x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    cnt[x]++;
  }
  dp[0][0][0] = 0;
  for (ll three = 0; three <= n; three++) {
    for (ll two = 0; two <= n; two++) {
      for (ll one = 0; one <= n; one++) {
        ll zero = n - two - three - one;
        if (zero < 0 || zero == n)
          continue;
        ld value = 1.0;
        if (three > 0)
          value += (1.0 * three / n) * dp[three - 1][two + 1][one];
        if (two > 0)
          value += (1.0 * two / n) * dp[three][two - 1][one + 1];
        if (one > 0)
          value += (1.0 * one / n) * dp[three][two][one - 1];
        // dp[three][two][one]=(value)/(1-1.0*zero/n);
        dp[three][two][one] = (value * n) / (1.0 * (one + two + three));
      }
    }
  }
  ld ans = dp[cnt[3]][cnt[2]][cnt[1]];
  cout.precision(20);
  cout << ans;
}
Problem p03169 (C++)

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

int n;
double dp[305][305][305];
int cnt[4];
int main() {
  freopen("j.in", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  int t;
  for (int i = 0; i < n; i++) {
    cin >> t;
    cnt[t]++;
  }
  dp[0][0][0] = 0;
  for (int n3 = 0; n3 <= cnt[3]; n3++) {
    for (int n2 = 0; n2 + n3 <= cnt[2] + cnt[3]; n2++) {
      for (int n1 = 0; n1 + n2 + n3 <= n - cnt[0]; n1++) {
        int s = n1 + n2 + n3;
        if (s != 0) {
          double temp = double(n);
          if (n1 != 0)
            temp += double(n1) * dp[n1 - 1][n2][n3];
          if (n2 != 0)
            temp += double(n2) * dp[n1 + 1][n2 - 1][n3];
          if (n3 != 0)
            temp += double(n3) * dp[n1][n2 + 1][n3 - 1];
          temp /= double(s);
          dp[n1][n2][n3] = temp;
        }
      }
    }
  }
  cout.precision(16);
  cout << dp[cnt[1]][cnt[2]][cnt[3]] << endl;
}

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

int n;
double dp[305][305][305];
int cnt[4];
int main() {
  // freopen("j.in", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  int t;
  for (int i = 0; i < n; i++) {
    cin >> t;
    cnt[t]++;
  }
  dp[0][0][0] = 0;
  for (int n3 = 0; n3 <= cnt[3]; n3++) {
    for (int n2 = 0; n2 + n3 <= cnt[2] + cnt[3]; n2++) {
      for (int n1 = 0; n1 + n2 + n3 <= n - cnt[0]; n1++) {
        int s = n1 + n2 + n3;
        if (s != 0) {
          double temp = double(n);
          if (n1 != 0)
            temp += double(n1) * dp[n1 - 1][n2][n3];
          if (n2 != 0)
            temp += double(n2) * dp[n1 + 1][n2 - 1][n3];
          if (n3 != 0)
            temp += double(n3) * dp[n1][n2 + 1][n3 - 1];
          temp /= double(s);
          dp[n1][n2][n3] = temp;
        }
      }
    }
  }
  cout.precision(16);
  cout << dp[cnt[1]][cnt[2]][cnt[3]] << endl;
}
Problem p03169 (C++)

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

#define MAXN 310

typedef long double ld;

ld dp[MAXN][MAXN][MAXN];
int N;
int sushi[MAXN];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> sushi[i];
  }
  for (int k = 0; k <= N; k++) {
    for (int j = 0; j <= N; j++) {
      for (int i = 0; i <= N; i++) {
        if (i + j + k <= N) {
          if ((i == 0 && j == 0) && (k == 0)) {
            dp[i][j][k] = (ld)0;
          } else {
            ld temp = (ld)i * dp[i - 1][j][k] + (ld)j * dp[i + 1][j - 1][k] +
                      (ld)k * dp[i][j + 1][k - 1] + (ld)N;
            dp[i][j][k] = temp / ((ld)(i + j + k));
          }
        }
      }
    }
  }
  int c1 = 0, c2 = 0, c3 = 0;
  for (int i = 0; i < N; i++) {
    if (sushi[i] == 1) {
      c1++;
    } else if (sushi[i] == 2) {
      c2++;
    } else {
      c3++;
    }
  }
  cout << setprecision(10);
  cout << dp[c1][c2][c3] << endl;
}


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

#define MAXN 310

typedef long double ld;

ld dp[MAXN][MAXN][MAXN];
int N;
int sushi[MAXN];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> sushi[i];
  }
  for (int k = 0; k <= N; k++) {
    for (int j = 0; j <= N; j++) {
      for (int i = 0; i <= N; i++) {
        if (i + j + k <= N) {
          if ((i == 0 && j == 0) && (k == 0)) {
            dp[i][j][k] = (ld)0;
          } else {
            ld temp = (i != 0 ? (ld)i * dp[i - 1][j][k] : 0) +
                      (j != 0 ? (ld)j * dp[i + 1][j - 1][k] : 0) +
                      (k != 0 ? (ld)k * dp[i][j + 1][k - 1] : 0) + (ld)N;
            dp[i][j][k] = temp / ((ld)(i + j + k));
          }
        }
      }
    }
  }
  int c1 = 0, c2 = 0, c3 = 0;
  for (int i = 0; i < N; i++) {
    if (sushi[i] == 1) {
      c1++;
    } else if (sushi[i] == 2) {
      c2++;
    } else {
      c3++;
    }
  }
  cout << setprecision(10);
  cout << dp[c1][c2][c3] << endl;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

vector<vector<vector<double>>>
    dp(300, vector<vector<double>>(300, vector<double>(300, -1)));
ll n;

double dfs(ll i, ll j, ll k) {
  if (dp[i][j][k] > -1)
    return dp[i][j][k];
  if (i == 0 && j == 0 && k == 0)
    return 0;
  double res = 0;
  if (i)
    res += dfs(i - 1, j, k) * i;
  if (j)
    res += dfs(i + 1, j - 1, k) * j;
  if (k)
    res += dfs(i, j + 1, k - 1) * k;
  res += n;
  res *= 1.0 / (i + j + k);
  return dp[i][j][k] = res;
}

int main() {
  cin >> n;
  ll one = 0, two = 0, three = 0;
  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a == 1)
      one++;
    else if (a == 2)
      two++;
    else
      three++;
  }
  double ans = dfs(one, two, three);
  cout << fixed << setprecision(10) << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

vector<vector<vector<double>>>
    dp(301, vector<vector<double>>(301, vector<double>(301, -1)));
ll n;

double dfs(ll i, ll j, ll k) {
  if (dp[i][j][k] > -1)
    return dp[i][j][k];
  if (i == 0 && j == 0 && k == 0)
    return 0;
  double res = 0;
  if (i)
    res += dfs(i - 1, j, k) * i;
  if (j)
    res += dfs(i + 1, j - 1, k) * j;
  if (k)
    res += dfs(i, j + 1, k - 1) * k;
  res += n;
  res *= 1.0 / (i + j + k);
  return dp[i][j][k] = res;
}

int main() {
  cin >> n;
  ll one = 0, two = 0, three = 0;
  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a == 1)
      one++;
    else if (a == 2)
      two++;
    else
      three++;
  }
  double ans = dfs(one, two, three);
  cout << fixed << setprecision(10) << ans << endl;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};

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

  int n, a[310], num[4] = {0};

  cin >> n;

  REP(i, n) {
    cin >> a[i];
    num[a[i]]++;
  }

  double dp[30][30][30];

  memset(dp, 0, sizeof(dp));

  for (int k = 0; k <= n; k++) {
    for (int j = 0; j <= n; j++) {
      for (int i = 0; i <= n; i++) {
        if ((i + j + k) && i + j + k <= n) {

          if (i)
            dp[i][j][k] += dp[i - 1][j][k] * i / n;
          if (j)
            dp[i][j][k] += dp[i + 1][j - 1][k] * j / n;
          if (k)
            dp[i][j][k] += dp[i][j + 1][k - 1] * k / n;
          dp[i][j][k] += 1;
          dp[i][j][k] /= 1.0 * (i + j + k) / n;
        }
      }
    }
  }

  cout << setprecision(20) << dp[num[1]][num[2]][num[3]] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};

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

  int n, a[310], num[4] = {0};

  cin >> n;

  REP(i, n) {
    cin >> a[i];
    num[a[i]]++;
  }

  double dp[301][301][301];

  memset(dp, 0, sizeof(dp));

  for (int k = 0; k <= n; k++) {
    for (int j = 0; j <= n; j++) {
      for (int i = 0; i <= n; i++) {
        if ((i + j + k) && i + j + k <= n) {

          if (i)
            dp[i][j][k] += dp[i - 1][j][k] * i / n;
          if (j)
            dp[i][j][k] += dp[i + 1][j - 1][k] * j / n;
          if (k)
            dp[i][j][k] += dp[i][j + 1][k - 1] * k / n;
          dp[i][j][k] += 1;
          dp[i][j][k] /= 1.0 * (i + j + k) / n;
        }
      }
    }
  }

  cout << setprecision(20) << dp[num[1]][num[2]][num[3]] << endl;

  return 0;
}
Problem p03169 (C++)

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

double dp[305][305][305];

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

  int N;
  cin >> N;

  int x = 0, y = 0, z = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a == 1)
      x++;
    if (a == 2)
      y++;
    if (a == 3)
      z++;
  }

  int MX = max(x, max(y * 2, z * 3));

  dp[0][0][0] = 0.0;
  for (int k = 0; k <= MX; k++) {
    for (int j = 0; j <= MX; j++) {
      for (int i = 0; i <= MX; i++) {
        int T = i + j + k;
        if (T == 0)
          continue;
        dp[i][j][k] = (1.0 * N / T);

        double r1 = 0.0;
        if (i > 0)
          r1 = (1.0 * i / T) * dp[i - 1][j][k];

        double r2 = 0.0;
        if (j > 0)
          r2 = (1.0 * j / T) * dp[i + 1][j - 1][k];

        double r3 = 0.0;
        if (k > 0)
          r3 = (1.0 * k / T) * dp[i][j + 1][k - 1];

        dp[i][j][k] += (r1 + r2 + r3);
      }
    }
  }

  cout << setprecision(9) << fixed << dp[x][y][z] << "\n";
}


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

double dp[305][305][305];

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

  int N;
  cin >> N;

  int x = 0, y = 0, z = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a == 1)
      x++;
    if (a == 2)
      y++;
    if (a == 3)
      z++;
  }

  int MX = x + y + z + 2;

  dp[0][0][0] = 0.0;
  for (int k = 0; k <= MX; k++) {
    for (int j = 0; j <= MX; j++) {
      for (int i = 0; i <= MX; i++) {
        int T = i + j + k;
        if (T == 0)
          continue;
        dp[i][j][k] = (1.0 * N / T);

        double r1 = 0.0;
        if (i > 0)
          r1 = (1.0 * i / T) * dp[i - 1][j][k];

        double r2 = 0.0;
        if (j > 0)
          r2 = (1.0 * j / T) * dp[i + 1][j - 1][k];

        double r3 = 0.0;
        if (k > 0)
          r3 = (1.0 * k / T) * dp[i][j + 1][k - 1];

        dp[i][j][k] += (r1 + r2 + r3);
      }
    }
  }

  cout << setprecision(9) << fixed << dp[x][y][z] << "\n";
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

int main() {
  int n;
  // cout << "here" << endl;
  cin >> n;
  vec<vec<vec<double>>> dp(302, vvec<double>(32, vec<double>(302, 0)));
  int num[3] = {0, 0, 0};
  rep(i, n) {
    int a;
    cin >> a;
    num[a - 1]++;
  }
  // cout << "here" << endl;
  rep(k, n + 1) rep(j, n + 1) rep(i, n + 1) {
    double d = 0.0;
    // cout << "here1" << endl;
    if (i == 0 && j == 0 && k == 0)
      continue;
    if (i > 0)
      d += i * dp[i - 1][j][k];
    if (j > 0)
      d += j * dp[i + 1][j - 1][k];
    if (k > 0)
      d += k * dp[i][j + 1][k - 1];
    dp[i][j][k] = (d + n) / (1.0 * (i + j + k));
  }
  cout << setprecision(10) << dp[num[0]][num[1]][num[2]] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

int main() {
  int n;
  // cout << "here" << endl;
  cin >> n;
  vec<vec<vec<double>>> dp(302, vvec<double>(302, vec<double>(302, 0)));
  int num[3] = {0, 0, 0};
  rep(i, n) {
    int a;
    cin >> a;
    num[a - 1]++;
  }
  // cout << "here" << endl;
  rep(k, n + 1) rep(j, n + 1) rep(i, n + 1) {
    double d = 0.0;
    // cout << "here1" << endl;
    if (i == 0 && j == 0 && k == 0)
      continue;
    if (i > 0)
      d += i * dp[i - 1][j][k];
    if (j > 0)
      d += j * dp[i + 1][j - 1][k];
    if (k > 0)
      d += k * dp[i][j + 1][k - 1];
    dp[i][j][k] = (d + n) / (1.0 * (i + j + k));
  }
  cout << setprecision(10) << dp[num[0]][num[1]][num[2]] << endl;
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define MAXN 105
#define INF 0x3f3f3f3f
#define p 1000000007
#define rint register int
#define LL long long
#define LD long double
using namespace std;

int n, sum, a[MAXN], num[4];
LD f[MAXN][MAXN][MAXN];

int main() {
  scanf("%d", &n);
  for (rint i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    num[a[i]]++;
    sum += a[i];
  }
  for (rint x = 1; x <= sum; ++x) {
    for (rint i = 0; i <= x / 3; ++i) {
      for (rint j = 0; j <= (x - i * 3) / 2; ++j) {
        int k = x - i * 3 - j * 2;
        if (i + j + k > n)
          continue;
        // printf("%d %d %d!!!\n", i, j, k);
        f[i][j][k] = n;
        if (i > 0)
          f[i][j][k] += i * f[i - 1][j + 1][k];
        if (j > 0)
          f[i][j][k] += j * f[i][j - 1][k + 1];
        if (k > 0)
          f[i][j][k] += k * f[i][j][k - 1];
        f[i][j][k] /= (i + j + k);
        // printf("%d %d %d %Lf!!!\n", i, j, k, f[i][j][k]);
      }
    }
  }
  printf("%.10Lf\n", f[num[3]][num[2]][num[1]]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define MAXN 305
#define INF 0x3f3f3f3f
#define p 1000000007
#define rint register int
#define LL long long
#define LD long double
using namespace std;

int n, sum, a[MAXN], num[4];
LD f[MAXN][MAXN][MAXN];

int main() {
  scanf("%d", &n);
  for (rint i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    num[a[i]]++;
    sum += a[i];
  }
  for (rint x = 1; x <= sum; ++x) {
    for (rint i = 0; i <= x / 3; ++i) {
      for (rint j = 0; j <= (x - i * 3) / 2; ++j) {
        int k = x - i * 3 - j * 2;
        if (i + j + k > n)
          continue;
        // printf("%d %d %d!!!\n", i, j, k);
        f[i][j][k] = n;
        if (i > 0)
          f[i][j][k] += i * f[i - 1][j + 1][k];
        if (j > 0)
          f[i][j][k] += j * f[i][j - 1][k + 1];
        if (k > 0)
          f[i][j][k] += k * f[i][j][k - 1];
        f[i][j][k] /= (i + j + k);
        // printf("%d %d %d %Lf!!!\n", i, j, k, f[i][j][k]);
      }
    }
  }
  printf("%.10Lf\n", f[num[3]][num[2]][num[1]]);
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;

#define ll long long
#define mod 1000000007

double dp[301][301][301];

double solve_ite(int *a, int n, int one, int two, int three) {

  for (int i = 0; i <= three; i++) {
    for (int j = 0; j <= two + 3 * three / 2 + 1; j++) {
      for (int k = 0; k <= one + 2 * two + 3 * three + 2; k++) {
        if (i != 0 or j != 0 or k != 0) {
          dp[i][j][k] = n / (double)(i + j + k);
        }
        if (k > 0) {
          dp[i][j][k] += k / (double)(i + j + k) * dp[i][j][k - 1];
        }
        if (j > 0) {
          dp[i][j][k] += j / (double)(i + j + k) * dp[i][j - 1][k + 1];
        }
        if (i > 0) {
          dp[i][j][k] += i / (double)(i + j + k) * dp[i - 1][j + 1][k];
        }
      }
    }
  }
  return dp[three][two][one];
}

int main() {

  int n;
  cin >> n;
  int a[n];
  int one = 0, two = 0, three = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] == 1)
      one++;
    if (a[i] == 2)
      two++;
    if (a[i] == 3)
      three++;
  }
  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] = 0;
  cout << setprecision(14) << solve_ite(a, n, one, two, three);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;

#define ll long long
#define mod 1000000007

double dp[301][301][301];

double solve_ite(int *a, int n, int one, int two, int three) {

  for (int i = 0; i <= three; i++) {
    for (int j = 0; j <= two + three; j++) {
      for (int k = 0; k <= one + two + three; k++) {
        if (i != 0 or j != 0 or k != 0) {
          dp[i][j][k] = n / (double)(i + j + k);
        }
        if (k > 0) {
          dp[i][j][k] += k / (double)(i + j + k) * dp[i][j][k - 1];
        }
        if (j > 0) {
          dp[i][j][k] += j / (double)(i + j + k) * dp[i][j - 1][k + 1];
        }
        if (i > 0) {
          dp[i][j][k] += i / (double)(i + j + k) * dp[i - 1][j + 1][k];
        }
      }
    }
  }
  return dp[three][two][one];
}

int main() {

  int n;
  cin >> n;
  int a[n];
  int one = 0, two = 0, three = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] == 1)
      one++;
    if (a[i] == 2)
      two++;
    if (a[i] == 3)
      three++;
  }
  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] = 0;
  cout << setprecision(14) << solve_ite(a, n, one, two, three);

  return 0;
}
Problem p03169 (C++)

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

using ld = long double;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<vector<vector<ld>>> dp(
      n + 1, vector<vector<ld>>(n + 1, vector<ld>(n + 1, 0)));
  vector<ld> weight(n + 1, 0);
  for (int i = 0; i <= n; i++) {
    ld x = 1;
    for (int j = 0; j <= 100000; j++) {
      weight[i] += (j + 1) * x;
      x = x * i / n;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        if (i + j + k > n)
          continue;
        ld x = n - (i + j + k);
        if (i > 0) {
          dp[i][j][k] += dp[i - 1][j + 1][k] * i / (n - x) + weight[x] * i / n;
        }
        if (j > 0) {
          dp[i][j][k] += dp[i][j - 1][k + 1] * j / (n - x) + weight[x] * j / n;
        }
        if (k > 0) {
          dp[i][j][k] += dp[i][j][k - 1] * k / (n - x) + weight[x] * k / n;
        }
      }
    }
  }
  int k = count(a.begin(), a.end(), 1);
  int j = count(a.begin(), a.end(), 2);
  int i = count(a.begin(), a.end(), 3);
  cout << fixed << setprecision(10);
  cout << dp[i][j][k] << endl;
  return 0;
}

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

using ld = long double;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<vector<vector<ld>>> dp(
      n + 1, vector<vector<ld>>(n + 1, vector<ld>(n + 1, 0)));
  vector<ld> weight(n + 1, 0);
  for (int i = 0; i <= n; i++) {
    ld x = 1;
    for (int j = 0; j <= 10000; j++) {
      weight[i] += (j + 1) * x;
      x = x * i / n;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        if (i + j + k > n)
          continue;
        ld x = n - (i + j + k);
        if (i > 0) {
          dp[i][j][k] += dp[i - 1][j + 1][k] * i / (n - x) + weight[x] * i / n;
        }
        if (j > 0) {
          dp[i][j][k] += dp[i][j - 1][k + 1] * j / (n - x) + weight[x] * j / n;
        }
        if (k > 0) {
          dp[i][j][k] += dp[i][j][k - 1] * k / (n - x) + weight[x] * k / n;
        }
      }
    }
  }
  int k = count(a.begin(), a.end(), 1);
  int j = count(a.begin(), a.end(), 2);
  int i = count(a.begin(), a.end(), 3);
  cout << fixed << setprecision(10);
  cout << dp[i][j][k] << endl;
  return 0;
}
Problem p03169 (C++)

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

double dp[305][305][305];
bool check[305][305][305];

double n;
double calc(ll a, ll b, ll c) {
  if (check[a][b][c] == true) {
    //  cout<<"true"<<endl;
    return dp[a][b][c];
  }
  double p0 = double((n - a - b - c) / n);
  double p1 = double(a / n);
  double p2 = double(b / n);
  double p3 = double(c / n);

  double now = 1;
  // cout<<n<<"n"<<(n-a-b-c)<<"a"<<endl;
  if (a > 0)
    now += p1 * calc(a - 1, b, c);
  if (b > 0)
    now += p2 * calc(a + 1, b - 1, c);
  if (c > 0)
    now += p3 * calc(a, b + 1, c - 1);
  //  cout<<now<<"now"<<p0<<"p0"<<endl;
  dp[a][b][c] = double(now / (1 - p0));
  check[a][b][c] == true;
  // cout<<dp[a][b][c]<<"a"<<a<<"b"<<b<<"c"<<c<<endl;
  return dp[a][b][c];
}

int main() {
  cin >> n;
  vector<ll> a(n);
  vector<ll> c(3, 0);

  rep(i, n) {
    cin >> a[i];
    if (a[i] == 1)
      c[0]++;
    if (a[i] == 2)
      c[1]++;
    if (a[i] == 3)
      c[2]++;
  }
  //  dp[0][0][0]=0;

  rep(i, 305) rep(j, 305) rep(k, 305) {
    dp[i][j][k] = 0;
    check[i][j][k] = false;
  }
  check[0][0][0] = true;
  rep(i, c[0] + 1) rep(j, c[1] + 1) rep(k, c[2] + 1) {
    dp[i][j][k] = calc(i, j, k);
    check[i][j][k] = true;
  }
  // cout<<c[0]<<endl;

  cout << setprecision(10) << dp[c[0]][c[1]][c[2]];
}


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

double dp[305][305][305];
bool check[305][305][305];

double n;
double calc(ll a, ll b, ll c) {
  if (check[a][b][c] == true) {
    //  cout<<"true"<<endl;
    return dp[a][b][c];
  }
  double p0 = double((n - a - b - c) / n);
  double p1 = double(a / n);
  double p2 = double(b / n);
  double p3 = double(c / n);

  double now = 1;
  // cout<<n<<"n"<<(n-a-b-c)<<"a"<<endl;
  if (a > 0)
    now += p1 * calc(a - 1, b, c);
  if (b > 0)
    now += p2 * calc(a + 1, b - 1, c);
  if (c > 0)
    now += p3 * calc(a, b + 1, c - 1);
  //  cout<<now<<"now"<<p0<<"p0"<<endl;
  dp[a][b][c] = double(now / (1 - p0));
  check[a][b][c] = true;
  // cout<<dp[a][b][c]<<"a"<<a<<"b"<<b<<"c"<<c<<endl;
  return dp[a][b][c];
}

int main() {
  cin >> n;
  vector<ll> a(n);
  vector<ll> c(3, 0);

  rep(i, n) {
    cin >> a[i];
    if (a[i] == 1)
      c[0]++;
    if (a[i] == 2)
      c[1]++;
    if (a[i] == 3)
      c[2]++;
  }
  //  dp[0][0][0]=0;

  rep(i, 305) rep(j, 305) rep(k, 305) {
    dp[i][j][k] = 0;
    check[i][j][k] = false;
  }
  check[0][0][0] = true;
  rep(i, c[0] + 1) rep(j, c[1] + 1) rep(k, c[2] + 1) {
    dp[i][j][k] = calc(i, j, k);
    check[i][j][k] = true;
  }
  // cout<<c[0]<<endl;

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

Buggy submission (status: Runtime Error):
/*
 *    author:  Gary Shih
 *    created: 2020-05-04 13:27:07
 */
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.setf(ios::fixed), cout.precision(9);
  int n;
  cin >> n;
  int c1 = 0, c2 = 0, c3 = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (a == 1)
      ++c1;
    if (a == 2)
      ++c2;
    if (a == 3)
      ++c3;
  }
  int maxSushi = 900;
  vector<vector<vector<double>>> dp(
      maxSushi + 1, vector<vector<double>>(maxSushi / 2 + 1,
                                           vector<double>(maxSushi / 3 + 1)));
  for (int total = 1; total <= c1 + 2 * c2 + 3 * c3; ++total) {
    for (int i = 0; i <= total; ++i)
      for (int j = 0; j <= (total - i); ++j) {
        if ((total - i - 2 * j) % 3 != 0)
          continue;
        int k = (total - i - 2 * j) / 3;
        int rest = i + j + k;
        double prev = 0.0;
        if (i > 0)
          prev += dp[i - 1][j][k] * i / n;
        if (j > 0)
          prev += dp[i + 1][j - 1][k] * j / n;
        if (k > 0)
          prev += dp[i][j + 1][k - 1] * k / n;
        dp[i][j][k] = (prev + 1) * n / rest;
      }
  }
  cout << dp[c1][c2][c3] << '\n';
}


Fixed submission:
/*
 *    author:  Gary Shih
 *    created: 2020-05-04 13:27:07
 */
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.setf(ios::fixed), cout.precision(9);
  int n;
  cin >> n;
  int c1 = 0, c2 = 0, c3 = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (a == 1)
      ++c1;
    if (a == 2)
      ++c2;
    if (a == 3)
      ++c3;
  }
  int maxSushi = 900;
  vector<vector<vector<double>>> dp(
      maxSushi + 1, vector<vector<double>>(maxSushi / 2 + 1,
                                           vector<double>(maxSushi / 3 + 1)));
  for (int total = 1; total <= c1 + 2 * c2 + 3 * c3; ++total) {
    for (int i = 0; i <= total; ++i)
      for (int j = 0; j <= (total - i) / 2; ++j) {
        if ((total - i - 2 * j) % 3 != 0)
          continue;
        int k = (total - i - 2 * j) / 3;
        int rest = i + j + k;
        double prev = 0.0;
        if (i > 0)
          prev += dp[i - 1][j][k] * i / n;
        if (j > 0)
          prev += dp[i + 1][j - 1][k] * j / n;
        if (k > 0)
          prev += dp[i][j + 1][k - 1] * k / n;
        dp[i][j][k] = (prev + 1) * n / rest;
      }
  }
  cout << dp[c1][c2][c3] << '\n';
}
Problem p03169 (C++)

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

using namespace std;

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

const int MX = 307;

ld b[MX + 5][MX + 5][MX + 5];

int main() {
  int n;
  scanf("%d", &n);
  int c[3];
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    if (l > 0)
      c[l - 1] += 1;
  }
  b[0][0][0] = 0.0;
  for (int k = 0; k < MX; k++) {
    for (int j = 0; k + j < MX; j++) {
      for (int i = 0; i + j + k < MX; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        b[i][j][k] += 1.0 * n / (i + j + k);
        b[i + 1][j][k] += b[i][j][k] * 1.0 * (i + 1) / (i + 1 + j + k);
        if (i)
          b[i - 1][j + 1][k] += b[i][j][k] * 1.0 * (j + 1) / (i + j + k);
        if (j)
          b[i][j - 1][k + 1] += b[i][j][k] * 1.0 * (k + 1) / (i + j + k);
      }
    }
  }
  cout << fixed << setprecision(10) << b[c[0]][c[1]][c[2]];
  return 0;
}


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

using namespace std;

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

const int MX = 307;

ld b[MX + 5][MX + 5][MX + 5];

int main() {
  int n;
  scanf("%d", &n);
  int c[3] = {0, 0, 0};
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    if (l > 0)
      c[l - 1] += 1;
  }
  b[0][0][0] = 0.0;
  for (int k = 0; k < MX; k++) {
    for (int j = 0; k + j < MX; j++) {
      for (int i = 0; i + j + k < MX; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        b[i][j][k] += 1.0 * n / (i + j + k);
        b[i + 1][j][k] += b[i][j][k] * 1.0 * (i + 1) / (i + 1 + j + k);
        if (i)
          b[i - 1][j + 1][k] += b[i][j][k] * 1.0 * (j + 1) / (i + j + k);
        if (j)
          b[i][j - 1][k + 1] += b[i][j][k] * 1.0 * (k + 1) / (i + j + k);
      }
    }
  }
  cout << fixed << setprecision(10) << b[c[0]][c[1]][c[2]];
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF (1 << 29)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))

double dp[301][301][301];
bool done[301][301][301];
int n, a;
int cnt[3];

double solve(int i, int j, int k) {
  if (done[i][j][k])
    return dp[i][j][k];
  done[i][j][k] = true;
  double &res = dp[i][j][k];
  double p = 1. * (i + j + k) / n;
  if (i) {
    double p0 = 1. * i / n;
    res += (solve(i - 1, j, k) + 1 / p) * i / (i + j + k);
  }

  if (j) {
    double p0 = 1. * j / n;
    res += (solve(i + 1, j - 1, k) + 1 / p) * j / (i + j + k);
  }

  if (k) {
    double p0 = 1. * k / n;
    res += (solve(i, j + 1, k - 1) + 1 / p) * k / (i + j + k);
  }
  //  cout << i << " " << j <<" " << k <<" " << res << endl;
  return res;
}
int main() {
  done[0][0][0] = true;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cnt[a]++;
  }
  for (int k = 0; k <= n; k++) {
    for (int j = 0; j <= n; j++) {
      for (int i = 0; i <= n; i++) {
        if (i + j + k > n)
          continue;
      }
    }
  }

  printf("%.10lf\n", solve(cnt[1], cnt[2], cnt[3]));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF (1 << 29)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))

double dp[301][301][301];
bool done[301][301][301];
int n, a;
int cnt[4];

double solve(int i, int j, int k) {
  if (done[i][j][k])
    return dp[i][j][k];
  done[i][j][k] = true;
  double &res = dp[i][j][k];
  double p = 1. * (i + j + k) / n;
  if (i) {
    double p0 = 1. * i / n;
    res += (solve(i - 1, j, k) + 1 / p) * i / (i + j + k);
  }

  if (j) {
    double p0 = 1. * j / n;
    res += (solve(i + 1, j - 1, k) + 1 / p) * j / (i + j + k);
  }

  if (k) {
    double p0 = 1. * k / n;
    res += (solve(i, j + 1, k - 1) + 1 / p) * k / (i + j + k);
  }
  //  cout << i << " " << j <<" " << k <<" " << res << endl;
  return res;
}
int main() {
  done[0][0][0] = true;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cnt[a]++;
  }
  for (int k = 0; k <= n; k++) {
    for (int j = 0; j <= n; j++) {
      for (int i = 0; i <= n; i++) {
        if (i + j + k > n)
          continue;
      }
    }
  }

  printf("%.10lf\n", solve(cnt[1], cnt[2], cnt[3]));
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iomanip>
#include <iostream>
using namespace std;
int A, B, C;
int n;

double dp[301][301][301];

double foo(int A, int B, int C) {
  if (dp[A][B][C] != -1)
    return dp[A][B][C];

  double nn = n;
  double AA = A;
  double BB = B;
  double CC = C;

  double res = 0;

  if (A != 0) {
    double E1 = (foo(A - 1, B, C) + 1.0) * AA / nn;
    res += E1;
  }

  if (B != 0) {
    double E1 = (foo(A + 1, B - 1, C) + 1.0) * BB / nn;
    res += E1;
  }

  if (C != 0) {
    double E1 = (foo(A, B + 1, C - 1) + 1.0) * CC / nn;
    res += E1;
  }
  double kl = 1.0 - (AA + BB + CC) / nn;

  res += kl;
  res /= (1.0 - kl);

  // dp[A][B][C] = res;
  return res;
}

int main() {
  A = 0;
  B = 0;
  C = 0;

  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;
      }
    }
  }
  dp[0][0][0] = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (a == 1)
      A++;
    else if (a == 2)
      B++;
    else if (a == 3)
      C++;
  }
  cout << fixed << setprecision(10) << foo(A, B, C);

  return 0;
}

Fixed submission:
#include <iomanip>
#include <iostream>
using namespace std;
int A, B, C;
int n;

double dp[301][301][301];

double foo(int A, int B, int C) {
  if (dp[A][B][C] != -1)
    return dp[A][B][C];

  double nn = n;
  double AA = A;
  double BB = B;
  double CC = C;

  double res = 0;

  if (A != 0) {
    double E1 = (foo(A - 1, B, C) + 1.0) * AA / nn;
    res += E1;
  }

  if (B != 0) {
    double E1 = (foo(A + 1, B - 1, C) + 1.0) * BB / nn;
    res += E1;
  }

  if (C != 0) {
    double E1 = (foo(A, B + 1, C - 1) + 1.0) * CC / nn;
    res += E1;
  }
  double kl = 1.0 - (AA + BB + CC) / nn;

  res += kl;
  res /= (1.0 - kl);

  dp[A][B][C] = res;
  return res;
}

int main() {
  A = 0;
  B = 0;
  C = 0;

  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;
      }
    }
  }
  dp[0][0][0] = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (a == 1)
      A++;
    else if (a == 2)
      B++;
    else if (a == 3)
      C++;
  }
  cout << fixed << setprecision(10) << foo(A, B, C);

  return 0;
}
Problem p03169 (C++)

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

int n, a, b, c, x;
double dp[N][N][N];

double f(int a, int b, int c) {
  if (a == 0 && b == 0 && c == 0)
    return 0;
  if (a < 0 || b < 0 || c < 0)
    return 0;
  if (dp[a][b][c] != 0)
    return dp[a][b][c];
  double ea, eb, ec, ans;
  ea = f(a - 1, b, c);
  eb = f(a + 1, b - 1, c);
  ec = f(a, b + 1, c - 1);
  ans = (a * ea + b * eb + c * ec + n) / (a + b + c);
  // printf("(%d %d %d) -> %.2lf\n",a,b,c,ans);
  return dp[a][b][c] = ans;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x == 1)
      a++;
    if (x == 2)
      b++;
    if (x == 3)
      c++;
  }
  printf("%.20lf\n", f(a, b, c));
  return 0;
}

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

int n, a, b, c, x;
double dp[N][N][N];

double f(int a, int b, int c) {
  if (a == 0 && b == 0 && c == 0)
    return 0;
  if (a < 0 || b < 0 || c < 0)
    return 0;
  if (dp[a][b][c] != 0)
    return dp[a][b][c];
  double ea, eb, ec, ans;
  ea = f(a - 1, b, c);
  eb = f(a + 1, b - 1, c);
  ec = f(a, b + 1, c - 1);
  ans = (a * ea + b * eb + c * ec + n) / (a + b + c);
  // printf("(%d %d %d) -> %.2lf\n",a,b,c,ans);
  return dp[a][b][c] = ans;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x == 1)
      a++;
    if (x == 2)
      b++;
    if (x == 3)
      c++;
  }
  printf("%.20lf\n", f(a, b, c));
  return 0;
}
Problem p03169 (C++)

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

using ll = long long;

const ll INF = 1001001001;
const ll mod = 1000000007;

int N;
double dp[101][101][101];

double rec(int i, int j, int k) {
  if (i + j + k == 0)
    return 0;
  if (dp[i][j][k] > 0)
    return dp[i][j][k];
  double p0 = (double)(N - i - j - k) / N;
  double p1 = (double)i / N;
  double p2 = (double)j / N;
  double p3 = (double)k / N;
  double res = 1;
  if (i > 0)
    res += rec(i - 1, j, k) * p1;
  if (j > 0)
    res += rec(i + 1, j - 1, k) * p2;
  if (k > 0)
    res += rec(i, j + 1, k - 1) * p3;
  res /= 1 - p0;
  return dp[i][j][k] = res;
}

int main() {
  cin >> N;
  int a[3] = {};
  for (int i = 0; i < N; ++i) {
    int x;
    cin >> x;
    x--;
    a[x]++;
  }
  printf("%.10f\n", rec(a[0], a[1], a[2]));
}

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

using ll = long long;

const ll INF = 1001001001;
const ll mod = 1000000007;

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

double rec(int i, int j, int k) {
  if (i + j + k == 0)
    return 0;
  if (dp[i][j][k] > 0)
    return dp[i][j][k];
  double p0 = (double)(N - i - j - k) / N;
  double p1 = (double)i / N;
  double p2 = (double)j / N;
  double p3 = (double)k / N;
  double res = 1;
  if (i > 0)
    res += rec(i - 1, j, k) * p1;
  if (j > 0)
    res += rec(i + 1, j - 1, k) * p2;
  if (k > 0)
    res += rec(i, j + 1, k - 1) * p3;
  res /= 1 - p0;
  return dp[i][j][k] = res;
}

int main() {
  cin >> N;
  int a[3] = {};
  for (int i = 0; i < N; ++i) {
    int x;
    cin >> x;
    x--;
    a[x]++;
  }
  printf("%.10f\n", rec(a[0], a[1], a[2]));
}