Problem p03169 (C++)

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

// #include "rubo.h"
// #define mp make_pair
// #define pb push_back
#define in int
#define ll long long
#define vc vector
#define SQ(j) (j) * (j)
// #define x first
// #define y second

// #define ld long double
#define dbl long double
#define pll pair<long long, long long>
#define pii pair<int, int>
#define all(j) j.begin(), j.end()
#define loop(xxx, yyy) for (int xxx = 0; xxx < yyy; xxx++)
// #define printf(fmt, ...) (0)
// #define HOME
// #define y0 ngacaleiebinvoaeu
// #define y1 gnarpipipaigare
#define j1 adsfndnasfafoasp

#define db(x) cout << #x << " = " << x << endl
#define dbCont(x)                                                              \
  cout << #x << ": ";                                                          \
  for (auto shun : x)                                                          \
    cout << shun << ' ';                                                       \
  cout << endl;

typedef long double ld;

using namespace std;

const int K = 100 * 100 + 123;
const int N = 100 + 5;
const int LG = 20;
const int MOD = 1000 * 1000 * 1000 + 7;

int n;
int a[5];

double memo[N][N][N];

double solve(int c0, int c1, int c2, int c3) {
  if (memo[c1][c2][c3] >= 0)
    return memo[c1][c2][c3];
  if (c1 + c2 + c3 == 0)
    return 0;

  double res = 0;

  if (c1)
    res += (c1 + 0.0) / (n) * (solve(c0 + 1, c1 - 1, c2, c3));
  if (c2)
    res += (c2 + 0.0) / (n) * (solve(c0, c1 + 1, c2 - 1, c3));
  if (c3)
    res += (c3 + 0.0) / (n) * (solve(c0, c1, c2 + 1, c3 - 1));

  res += 1;

  res /= 1.0 - (c0 + 0.0) / n;

  return memo[c1][c2][c3] = res;
}

int main() {
  loop(i, N) loop(j, N) loop(k, N) memo[i][j][k] = -1;

  cin >> n;
  loop(i, n) {
    int e;
    cin >> e;
    a[e]++;
  }

  double res = solve(a[0], a[1], a[2], a[3]);

  printf("%.15lf\n", res);
  return 0;
}


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

// #include "rubo.h"
// #define mp make_pair
// #define pb push_back
#define in int
#define ll long long
#define vc vector
#define SQ(j) (j) * (j)
// #define x first
// #define y second

// #define ld long double
#define dbl long double
#define pll pair<long long, long long>
#define pii pair<int, int>
#define all(j) j.begin(), j.end()
#define loop(xxx, yyy) for (int xxx = 0; xxx < yyy; xxx++)
// #define printf(fmt, ...) (0)
// #define HOME
// #define y0 ngacaleiebinvoaeu
// #define y1 gnarpipipaigare
#define j1 adsfndnasfafoasp

#define db(x) cout << #x << " = " << x << endl
#define dbCont(x)                                                              \
  cout << #x << ": ";                                                          \
  for (auto shun : x)                                                          \
    cout << shun << ' ';                                                       \
  cout << endl;

typedef long double ld;

using namespace std;

const int K = 100 * 100 + 123;
const int N = 300 + 5;
const int LG = 20;
const int MOD = 1000 * 1000 * 1000 + 7;

int n;
int a[5];

double memo[N][N][N];

double solve(int c0, int c1, int c2, int c3) {
  if (memo[c1][c2][c3] >= 0)
    return memo[c1][c2][c3];
  if (c1 + c2 + c3 == 0)
    return 0;

  double res = 0;

  if (c1)
    res += (c1 + 0.0) / (n) * (solve(c0 + 1, c1 - 1, c2, c3));
  if (c2)
    res += (c2 + 0.0) / (n) * (solve(c0, c1 + 1, c2 - 1, c3));
  if (c3)
    res += (c3 + 0.0) / (n) * (solve(c0, c1, c2 + 1, c3 - 1));

  res += 1;

  res /= 1.0 - (c0 + 0.0) / n;

  return memo[c1][c2][c3] = res;
}

int main() {
  loop(i, N) loop(j, N) loop(k, N) memo[i][j][k] = -1;

  cin >> n;
  loop(i, n) {
    int e;
    cin >> e;
    a[e]++;
  }

  double res = solve(a[0], a[1], a[2], a[3]);

  printf("%.15lf\n", res);
  return 0;
}