Problem p03169 (C++)

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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
typedef map<ll, ll> mii;
const int MOD = 1e9 + 7;

#define fastIO()                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define mp make_pair

#define what_is(args...)                                                       \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

int main() {
  int N;
  cin >> N;
  vi a(N);
  for (auto &i : a)
    cin >> i;
  ld dp[N + 1][N + 1][N + 1];
  dp[0][0][0] = 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 + j + k <= 0 || i + j + k > N)
          continue;
        dp[i][j][k] = 0;
        if (i >= 1)
          dp[i][j][k] += ld(i) * dp[i - 1][j][k];
        if (j >= 1)
          dp[i][j][k] += ld(j) * dp[i + 1][j - 1][k];
        if (k >= 1)
          dp[i][j][k] += ld(k) * dp[i][j + 1][k - 1];
        assert(i + j + k > 0);
        dp[i][j][k] = (dp[i][j][k] + N) / (i + j + k);
        // cerr << i << " " << j << " " <<k << " -> " << dp[i][j][k] << endl;
      }
    }
  }
  int i, j, k;
  i = j = k = 0;
  for (auto x : a) {
    if (x == 1)
      ++i;
    else if (x == 2)
      ++j;
    else if (x == 3)
      ++k;
  }
  assert(i + j + k >= 0 && i + j + k <= N);
  cout << fixed << setprecision(12);
  cout << dp[i][j][k] << endl;
  return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
typedef map<ll, ll> mii;
const int MOD = 1e9 + 7;

#define fastIO()                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define mp make_pair

#define what_is(args...)                                                       \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

int main() {
  int N;
  cin >> N;
  vi a(N);
  for (auto &i : a)
    cin >> i;
  int n = 303;
  vector<vector<vector<ld>>> dp(
      n + 1, vector<vector<ld>>(n + 1, vector<ld>(n + 1, 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 + j + k <= 0 || i + j + k > N)
          continue;
        dp[i][j][k] = 0;
        if (i >= 1)
          dp[i][j][k] += ld(i) * dp[i - 1][j][k];
        if (j >= 1)
          dp[i][j][k] += ld(j) * dp[i + 1][j - 1][k];
        if (k >= 1)
          dp[i][j][k] += ld(k) * dp[i][j + 1][k - 1];
        assert(i + j + k > 0);
        dp[i][j][k] = (dp[i][j][k] + N) / (i + j + k);
        // cerr << i << " " << j << " " <<k << " -> " << dp[i][j][k] << endl;
      }
    }
  }
  int i, j, k;
  i = j = k = 0;
  for (auto x : a) {
    if (x == 1)
      ++i;
    else if (x == 2)
      ++j;
    else if (x == 3)
      ++k;
  }
  assert(i + j + k >= 0 && i + j + k <= N);
  cout << fixed << setprecision(12);
  cout << dp[i][j][k] << endl;
  return 0;
}