Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long int
#define ulli unsigned long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define initialize(array, size, value)                                         \
  for (ll i = 0; i < size; i++)                                                \
  array[i] = value
#define couta(array, size)                                                     \
  for (lli i = 0; i < size; i++)                                               \
  cout << array[i] << " "
#define vl vector<ll>
#define vp vector<pair<ll, ll>>
#define sl set<ll>
#define msp multiset<pair<long long, long long>> S;
#define pll pair<ll, ll>
#define mll                                                                    \
  map<lli,                                                                     \
      lli> // for( map<lli, lli>::iterator
           // i=temp.begin();i!=temp.end();i++)cout<<i->fi<<" "<<i->se<<endl;
#define mvl map<lli, vl>
#define umll unordered_map<lli, lli>
#define vt vector<pair<lli, pll>>
#define vf vector<pair<pll, pll>>
#define qu queue<lli>
#define pq priority_queue<lli>
#define dq deque<lli>
#define ptr vector<lli>::iterator
#define bs(array, x)                                                           \
  binary_search(array.begin(), array.end(),                                    \
                x) // also valid for set and multiset
#define lb(array, x) lower_bound(array.begin(), array.end(), x)
#define ub(array, x) upper_bound(array.begin(), array.end(), x)
#define nobw(array, i, j)                                                      \
  upper_bound(array.begin(), array.end(), j) -                                 \
      lower_bound(array.begin(), array.end(),                                  \
                  i) // number of numbers between i & j
#define vc clear()
#define endl '\n'
#define sp system("pause");
#define INF 9223372036854775807
#define MINF -9223372036854775808
typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

ll n;
ll a[305];
ld dp[302][302][302];
ld f(ll b, ll c, ll d) {
  // cout<<"call "<<b<<" "<<c<<" "<<d<<endl;
  ld aa = n - b - c - d;
  if (b < 0 || c < 0 || d < 0 || n - b - c - d < 0)
    return 0;
  else if (aa == n) {
    return 0;
  } else if (dp[b][c][d] != 0) {
    return dp[b][c][d];
  } else {
    ld ans = 0;
    ld x = (ld)b / (ld)n;
    ld y = (ld)c / (ld)n;
    ld z = (ld)d / (ld)n;
    ans = 1 + x * f(b - 1, c, d) + y * f(b + 1, c - 1, d) +
          z * f(b, c + 1, d - 1);
    ans = ans / ((ld)1 - (aa / (ld)n));
    return dp[b][c][d] = ans;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // ll n;
  cin >> n;
  // ll a[n];
  ll c0 = 0, c1 = 0, c2 = 0, c3 = 0;
  loop(i, 0, n) {
    cin >> a[i];
    if (a[i] == 0)
      c0++;
    else if (a[i] == 1)
      c1++;
    else if (a[i] == 2)
      c2++;
    else if (a[i] == 3)
      c3++;
  }
  memset(dp, 0, sizeof dp);
  // cout<<c0<<" "<<c1<<" "<<c2<<" "<<c3<<endl;
  cout << fixed << setprecision(15) << f(c1, c2, c3) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long int
#define ulli unsigned long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define initialize(array, size, value)                                         \
  for (ll i = 0; i < size; i++)                                                \
  array[i] = value
#define couta(array, size)                                                     \
  for (lli i = 0; i < size; i++)                                               \
  cout << array[i] << " "
#define vl vector<ll>
#define vp vector<pair<ll, ll>>
#define sl set<ll>
#define msp multiset<pair<long long, long long>> S;
#define pll pair<ll, ll>
#define mll                                                                    \
  map<lli,                                                                     \
      lli> // for( map<lli, lli>::iterator
           // i=temp.begin();i!=temp.end();i++)cout<<i->fi<<" "<<i->se<<endl;
#define mvl map<lli, vl>
#define umll unordered_map<lli, lli>
#define vt vector<pair<lli, pll>>
#define vf vector<pair<pll, pll>>
#define qu queue<lli>
#define pq priority_queue<lli>
#define dq deque<lli>
#define ptr vector<lli>::iterator
#define bs(array, x)                                                           \
  binary_search(array.begin(), array.end(),                                    \
                x) // also valid for set and multiset
#define lb(array, x) lower_bound(array.begin(), array.end(), x)
#define ub(array, x) upper_bound(array.begin(), array.end(), x)
#define nobw(array, i, j)                                                      \
  upper_bound(array.begin(), array.end(), j) -                                 \
      lower_bound(array.begin(), array.end(),                                  \
                  i) // number of numbers between i & j
#define vc clear()
#define endl '\n'
#define sp system("pause");
#define INF 9223372036854775807
#define MINF -9223372036854775808
typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

ll n;
ll a[305];
ld dp[302][302][302];
ld f(ll b, ll c, ll d) {
  // cout<<"call "<<b<<" "<<c<<" "<<d<<endl;
  ld aa = n - b - c - d;
  if (b < 0 || c < 0 || d < 0 || n - b - c - d < 0)
    return 0;
  else if (aa == n) {
    return 0;
  } else if (dp[b][c][d] != 0) {
    return dp[b][c][d];
  } else {
    ld ans = 0;
    ld x = (ld)b / (ld)n;
    ld y = (ld)c / (ld)n;
    ld z = (ld)d / (ld)n;
    ans = 1 + x * f(b - 1, c, d) + y * f(b + 1, c - 1, d) +
          z * f(b, c + 1, d - 1);
    ans = ans / ((ld)1 - (aa / (ld)n));
    return dp[b][c][d] = ans;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  /* #ifndef ONLINE_JUDGE
       freopen("input.txt", "r", stdin);
       freopen("output.txt", "w", stdout);
   #endif
*/
  // ll n;
  cin >> n;
  // ll a[n];
  ll c0 = 0, c1 = 0, c2 = 0, c3 = 0;
  loop(i, 0, n) {
    cin >> a[i];
    if (a[i] == 0)
      c0++;
    else if (a[i] == 1)
      c1++;
    else if (a[i] == 2)
      c2++;
    else if (a[i] == 3)
      c3++;
  }
  memset(dp, 0, sizeof dp);
  // cout<<c0<<" "<<c1<<" "<<c2<<" "<<c3<<endl;
  cout << fixed << setprecision(15) << f(c1, c2, c3) << endl;
}