Language: C++

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

ll MAX = 1e9+1;
const int maxn = 300*3+1;
double dp[maxn][maxn][maxn];
int n;

double f(int i, int j, int k) {
    if (i == 0 && j == 0 && k == 0)
        return 0;
    if (dp[i][j][k] != 0)
        return dp[i][j][k];
    int l = n - i - j - k;
    double sol = 1 + (double) i/n * (i == 0 ? 0 : f(i-1,j,k))
        + (double) j/n * (j == 0 ? 0 : f(i+1, j-1, k))
        + (double) k/n * (k == 0 ? 0 : f(i, j+1, k-1));
    sol /= (1 - (double) l/n);

    dp[i][j][k] = sol;

    return sol;
}

void solve() {
    cin >> n;

    vector<int> counts(4);
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        counts[x]++;
    }
    for (int i = 0; i < maxn; i++)
        for (int j = 0; j < maxn; j++)
            for (int k = 0; k < maxn; k++)
                dp[i][j][k] = 0;

    // if f(i,j,k) ~ expected # of moves to finish i 1s, j 2s, and k 3s
    // f(i,j,k) = 1 + (N-i-j-k)/N f(i,j,k) + i/N f(i-1,j,k) + j/N f(i+1,j-1,k) + k/N f(i,j+1,k-1)
    //          (pick)  (pick 0 plate)       (pick 1 plate)     (pick 2 plate)   (pick 3 plate)
    // dp[i][j][k] ~ expected # of moves to finish from state (i,j,k)
    // if i'm at dp[i][j][k], then three states depend on me:
    //      - dp[i+1][j][k]
    //      - dp[i-1][j+1][k]
    //      - dp[i][j-1][k+1]

    //dp[1][0][0] = n;
    //for (int k = 0; k <= counts[3]; k++) {
    //    for (int j = 0; j <= counts[2] + counts[3]; j++) {
    //        for (int i = 0; i <= counts[1] + counts[2] + counts[3]; i++) {
    //            //cout << i << " " << j << " " << k << " ";
    //            if (j > 0)
    //                dp[i][j-1][k+1] += (double) n/(k+1) * dp[i][j][k];
    //            if (i > 0)
    //                dp[i-1][j+1][k] += (double) n/(j+1) * dp[i][j][k];
    //            dp[i+1][j][k] += (double) n/(i+1) * dp[i][j][k];
    //            //cout << dp[i][j][k] << endl;
    //        }
    //    }
    //}

    double ans = f(counts[1], counts[2], counts[3]);

    cout.precision(20);
    //cout << dp[counts[1]][counts[2]][counts[3]] << endl;
    cout << ans << endl;
}


int main() {
    solve();
	return 0;
}



Compiler output:
/tmp/cci48CcW.o: in function `f(int, int, int)':
a.cc:(.text+0xb9): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cci48CcW.o
a.cc:(.text+0xd4): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cci48CcW.o
a.cc:(.text+0x138): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cci48CcW.o
a.cc:(.text+0x198): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cci48CcW.o
a.cc:(.text+0x1f8): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cci48CcW.o
/tmp/cci48CcW.o: in function `solve()':
a.cc:(.text+0x27d): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cci48CcW.o
a.cc:(.text+0x300): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cci48CcW.o
collect2: error: ld returned 1 exit status