Problem p03169 (C++)

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

using namespace std;

#define sp << " " <<
#define mod 1000000007

/*
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

long long dice()
{
    uniform_int_distribution<long long> uid(1,2);//specify l and r.
    return uid(rng) ;
}

int read()
{
       int cc = getc(stdin);
       for (;cc < '0' || cc > '9';)  cc = getc(stdin);
       int ret = 0;
       for (;cc >= '0' && cc <= '9';)
       {
          ret = ret * 10 + cc - '0';
          cc = getc(stdin);
       }
      return ret;
}

inline void print(int n)
{
  if (n == 0)
  {
    putchar('0');
    putchar('\n');
  }
  else if (n == -1)
  {
    putchar('-');
    putchar('1');
    putchar('\n');
  }
  else
  {
    char buf[11];
    buf[10] = '\n';
    int i = 9;
    while (n)
    {
      buf[i--] = n % 10 + '0';
      n /= 10;
    }
    while (buf[i] != '\n')
      putchar(buf[++i]);
  }
}
*/

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long double n;
  int x = 0, y = 0, z = 0;
  cin >> n;
  int d;
  for (int i = 0; i < n; i++) {
    cin >> d;
    if (d == 1)
      x++;
    else if (d == 2)
      y++;
    else
      z++;
  }
  dp[0][0][0] = 0;
  for (int i = 0; i <= (z); i++) {
    for (int j = 0; j <= (y + z); j++) {
      for (int k = 0; k <= (x + y + z); k++) {
        if ((i + j + k) > 0)
          dp[k][j][i] = (n / (long double)(i + j + k));
        if (k > 0)
          dp[k][j][i] += (long double)k * dp[k - 1][j][i] / (i + j + k);
        if (j > 0)
          dp[k][j][i] += (long double)j * dp[k + 1][j - 1][i] / (i + j + k);
        if (i > 0)
          dp[k][j][i] += (long double)i * dp[k][j + 1][i - 1] / (i + j + k);
      }
    }
  }
  cout << fixed << setprecision(13) << dp[x][y][z];
  return 0;
}


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

using namespace std;

#define sp << " " <<
#define mod 1000000007

/*
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

long long dice()
{
    uniform_int_distribution<long long> uid(1,2);//specify l and r.
    return uid(rng) ;
}

int read()
{
       int cc = getc(stdin);
       for (;cc < '0' || cc > '9';)  cc = getc(stdin);
       int ret = 0;
       for (;cc >= '0' && cc <= '9';)
       {
          ret = ret * 10 + cc - '0';
          cc = getc(stdin);
       }
      return ret;
}

inline void print(int n)
{
  if (n == 0)
  {
    putchar('0');
    putchar('\n');
  }
  else if (n == -1)
  {
    putchar('-');
    putchar('1');
    putchar('\n');
  }
  else
  {
    char buf[11];
    buf[10] = '\n';
    int i = 9;
    while (n)
    {
      buf[i--] = n % 10 + '0';
      n /= 10;
    }
    while (buf[i] != '\n')
      putchar(buf[++i]);
  }
}
*/

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long double n;
  int x = 0, y = 0, z = 0;
  cin >> n;
  int d;
  for (int i = 0; i < n; i++) {
    cin >> d;
    if (d == 1)
      x++;
    else if (d == 2)
      y++;
    else
      z++;
  }
  dp[0][0][0] = 0;
  for (int i = 0; i <= 300; i++) {
    for (int j = 0; j <= 300; j++) {
      for (int k = 0; k <= 300; k++) {
        if (i + j + k > 300)
          continue;
        if ((i + j + k) > 0)
          dp[k][j][i] = (n / (long double)(i + j + k));
        if (k > 0)
          dp[k][j][i] += (long double)k * dp[k - 1][j][i] / (i + j + k);
        if (j > 0)
          dp[k][j][i] += (long double)j * dp[k + 1][j - 1][i] / (i + j + k);
        if (i > 0)
          dp[k][j][i] += (long double)i * dp[k][j + 1][i - 1] / (i + j + k);
      }
    }
  }
  cout << fixed << setprecision(13) << dp[x][y][z];
  return 0;
}