Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll n;
V<V<V<double>>> dp(300, V<V<double>>(300, V<double>(300, 0.0)));
V<V<V<bool>>> flag(300, V<V<bool>>(300, V<bool>(300, false)));

double f(ll c1, ll c2, ll c3) {
  if (flag.at(c1).at(c2).at(c3))
    return dp.at(c1).at(c2).at(c3);
  flag.at(c1).at(c2).at(c3) = true;
  double ans = 1 / (1 - (double)(n - c1 - c2 - c3) / n);
  if (c1 > 0)
    ans += f(c1 - 1, c2, c3) * (c1 / (double)n) /
           (1 - (double)(n - c1 - c2 - c3) / n);
  if (c2 > 0)
    ans += f(c1 + 1, c2 - 1, c3) * (c2 / (double)n) /
           (1 - (double)(n - c1 - c2 - c3) / n);
  if (c3 > 0)
    ans += f(c1, c2 + 1, c3 - 1) * (c3 / (double)n) /
           (1 - (double)(n - c1 - c2 - c3) / n);
  return dp.at(c1).at(c2).at(c3) = ans;
}

int main() {
  flag.at(0).at(0).at(0) = true;
  cin >> n;
  vll a(n);
  REP(i, n) cin >> a.at(i);
  vll c(3, 0);
  REP(i, n) c.at(a.at(i) - 1)++;
  cout << fixed << setprecision(12) << f(c.at(0), c.at(1), c.at(2)) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll n;
V<V<V<double>>> dp(305, V<V<double>>(305, V<double>(305, 0.0)));
V<V<V<bool>>> flag(305, V<V<bool>>(305, V<bool>(305, false)));

double f(ll c1, ll c2, ll c3) {
  if (flag.at(c1).at(c2).at(c3))
    return dp.at(c1).at(c2).at(c3);
  flag.at(c1).at(c2).at(c3) = true;
  double ans = 1 / (1 - (double)(n - c1 - c2 - c3) / n);
  if (c1 > 0)
    ans += f(c1 - 1, c2, c3) * (c1 / (double)n) /
           (1 - (double)(n - c1 - c2 - c3) / n);
  if (c2 > 0)
    ans += f(c1 + 1, c2 - 1, c3) * (c2 / (double)n) /
           (1 - (double)(n - c1 - c2 - c3) / n);
  if (c3 > 0)
    ans += f(c1, c2 + 1, c3 - 1) * (c3 / (double)n) /
           (1 - (double)(n - c1 - c2 - c3) / n);
  return dp.at(c1).at(c2).at(c3) = ans;
}

int main() {
  flag.at(0).at(0).at(0) = true;
  cin >> n;
  vll a(n);
  REP(i, n) cin >> a.at(i);
  vll c(3, 0);
  REP(i, n) c.at(a.at(i) - 1)++;
  cout << fixed << setprecision(12) << f(c.at(0), c.at(1), c.at(2)) << endl;
  return 0;
}