Problem p03169 (C++)

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

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b, i)                                                    \
  do {                                                                         \
    cout << "[";                                                               \
    for (int(i) = (a); (i) < (b); ++(i))                                       \
      cout << ary[(i)] << ((b)-1 == (i) ? "" : ", ");                          \
    cout << "]\n";                                                             \
  } while (0)
#define p_map(map, it)                                                         \
  do {                                                                         \
    cout << "{";                                                               \
    for (auto(it) = map.begin();; ++(it)) {                                    \
      if ((it) == map.end()) {                                                 \
        cout << "}\n";                                                         \
        break;                                                                 \
      } else                                                                   \
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \
    }                                                                          \
  } while (0)

double dp[300][300][300];

double rec(int n, int a, int b, int c) {
  if (dp[a][b][c] > -0.5)
    return dp[a][b][c];
  dp[a][b][c] = n;
  if (a)
    dp[a][b][c] += rec(n, a - 1, b, c) * a;
  if (b)
    dp[a][b][c] += rec(n, a + 1, b - 1, c) * b;
  if (c)
    dp[a][b][c] += rec(n, a, b + 1, c - 1) * c;
  dp[a][b][c] /= a + b + c;
  return dp[a][b][c];
}

int main() {
  int n, a[3] = {0, 0, 0};
  cin >> n;
  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] = -1;
  dp[0][0][0] = 0;
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b;
    a[b - 1]++;
  }
  cout << fixed << setprecision(12) << rec(n, a[0], a[1], a[2]) << endl;
  return 0;
}

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

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b, i)                                                    \
  do {                                                                         \
    cout << "[";                                                               \
    for (int(i) = (a); (i) < (b); ++(i))                                       \
      cout << ary[(i)] << ((b)-1 == (i) ? "" : ", ");                          \
    cout << "]\n";                                                             \
  } while (0)
#define p_map(map, it)                                                         \
  do {                                                                         \
    cout << "{";                                                               \
    for (auto(it) = map.begin();; ++(it)) {                                    \
      if ((it) == map.end()) {                                                 \
        cout << "}\n";                                                         \
        break;                                                                 \
      } else                                                                   \
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \
    }                                                                          \
  } while (0)

double dp[310][310][310];

double rec(int n, int a, int b, int c) {
  if (dp[a][b][c] > -0.5)
    return dp[a][b][c];
  dp[a][b][c] = n;
  if (a)
    dp[a][b][c] += rec(n, a - 1, b, c) * a;
  if (b)
    dp[a][b][c] += rec(n, a + 1, b - 1, c) * b;
  if (c)
    dp[a][b][c] += rec(n, a, b + 1, c - 1) * c;
  dp[a][b][c] /= a + b + c;
  return dp[a][b][c];
}

int main() {
  int n, a[3] = {0, 0, 0};
  cin >> n;
  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] = -1;
  dp[0][0][0] = 0;
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b;
    a[b - 1]++;
  }
  cout << fixed << setprecision(12) << rec(n, a[0], a[1], a[2]) << endl;
  return 0;
}