Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define int long long
typedef long double ld;
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<vi>
#define viii vector<vii>
#define vpii vector<pii>
#define vb vector<bool>
#define vbb vector<vb>
#define vs vector<string>
#define pb push_back
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define in(a, x)                                                               \
  for (int i = x; i < (a).size(); ++i)                                         \
  cin >> (a)[i]
#define out(a)                                                                 \
  for (auto qwe : a)                                                           \
  cout << qwe << " "

const int INF = 1e9;
const int INF64 = 1e18;
const int MOD = 1e9 + 7;
const int MOD9 = 1e9 + 9;
const int MOD3 = 998244353;
const int P = 37;

const int mxn = 200000;

int n;
vector<vector<vector<ld>>> dp;

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

  cin >> n;

  int a = 0, b = 0, c = 0;

  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;

    if (x == 1)
      ++a;
    if (x == 2)
      ++b;
    if (x == 3)
      ++c;
  }

  dp.assign(n + 2, vector<vector<ld>>(n + 2, vector<ld>(n + 2, 0)));

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

        dp[i][j][k] = (ld)n / (i + j + k);

        if (i > 0)
          dp[i][j][k] += dp[i - 1][j][k] * ((ld)i / (i + j + k));
        if (j > 0)
          dp[i][j][k] += dp[i + 1][j - 1][k] * ((ld)j / (i + j + k));
        if (k > 0)
          dp[i][j][k] += dp[i][j + 1][k - 1] * ((ld)k / (i + j + k));
      }
    }
  }

  cout.precision(10);
  cout << fixed << dp[a][b][c];
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define int long long
typedef long double ld;
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<vi>
#define viii vector<vii>
#define vpii vector<pii>
#define vb vector<bool>
#define vbb vector<vb>
#define vs vector<string>
#define pb push_back
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define in(a, x)                                                               \
  for (int i = x; i < (a).size(); ++i)                                         \
  cin >> (a)[i]
#define out(a)                                                                 \
  for (auto qwe : a)                                                           \
  cout << qwe << " "

const int INF = 1e9;
const int INF64 = 1e18;
const int MOD = 1e9 + 7;
const int MOD9 = 1e9 + 9;
const int MOD3 = 998244353;
const int P = 37;

const int mxn = 200000;

int n;
vector<vector<vector<ld>>> dp;

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

  cin >> n;

  int a = 0, b = 0, c = 0;

  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;

    if (x == 1)
      ++a;
    if (x == 2)
      ++b;
    if (x == 3)
      ++c;
  }

  dp.assign(n + 2, vector<vector<ld>>(n + 2, vector<ld>(n + 2, 0)));

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

        dp[i][j][k] = (ld)n / (i + j + k);

        if (i > 0)
          dp[i][j][k] += dp[i - 1][j][k] * ((ld)i / (i + j + k));
        if (j > 0)
          dp[i][j][k] += dp[i + 1][j - 1][k] * ((ld)j / (i + j + k));
        if (k > 0)
          dp[i][j][k] += dp[i][j + 1][k - 1] * ((ld)k / (i + j + k));
      }
    }
  }

  cout.precision(10);
  cout << fixed << dp[a][b][c];
}