Language: C++

ng ll = long long;
 
const int nax = 305;
double p[nax][nax][nax], ev[nax][nax][nax];
// ev[a][b][c] - the expected number of operations so far to get to state (a,b,c)
// easier: p-bility of getting to this state at all
 
int main() {
    int n;
    scanf("%d", &n);
    vector<int> cnt(4);
    for(int i = 0; i < n; ++i) {
        int x;
        scanf("%d", &x);
        ++cnt[x];
    }
    p[cnt[1]][cnt[2]][cnt[3]] = 1;
    for(int c = n; c >= 0; --c) {
        for(int b = n; b >= 0; --b) {
            for(int a = n; a >= 0; --a) {
                if(a == 0 && b == 0 && c == 0) {
                    continue;
                }
                if(a + b + c > n) {
                    continue;
                }
                // dp[a][b][c]
                
                double p_waste = (double) (n - (a + b + c)) / n;
                // p_waste + p_waste^2 + ... = S
                // 1 + S = S / p_waste
                double ev_waste = p_waste / (1 - p_waste) + 1;
                debug() << imie(a) imie(b) imie(c) imie(ev_waste);
                ev[a][b][c] += ev_waste * p[a][b][c];
                
                // remove 1 -> dp[a-1][b][c]
                // we will eventually choose one of a+b+c
                if(a != 0) {
                    double p_go = (double) a / (a + b + c);
                    p[a-1][b][c] += p[a][b][c] * p_go;
                    ev[a-1][b][c] += ev[a][b][c] * p_go;
                }
                
                // remove 2 -> dp[a+1][b-1][c]
                if(b != 0) {
                    double p_go = (double) b / (a + b + c);
                    p[a+1][b-1][c] += p[a][b][c] * p_go;
                    ev[a+1][b-1][c] += ev[a][b][c] * p_go;
                }
                
                // remove 3
                if(c != 0) {
                    double p_go = (double) c / (a + b + c);
                    p[a][b+1][c-1] += p[a][b][c] * p_go;
                    ev[a][b+1][c-1] += ev[a][b][c] * p_go;
                }
            }
        }
    }
    printf("%.10lf\n", ev[0][0][0]); // + cnt[1] + 2 * cnt[2] + 3 * cnt[3]);
}

Compiler output:
a.cc:1:1: error: 'ng' does not name a type; did you mean 'long'?
    1 | ng ll = long long;
      | ^~
      | long
a.cc: In function 'int main()':
a.cc:10:5: error: 'scanf' was not declared in this scope
   10 |     scanf("%d", &n);
      |     ^~~~~
a.cc:11:5: error: 'vector' was not declared in this scope
   11 |     vector<int> cnt(4);
      |     ^~~~~~
a.cc:11:12: error: expected primary-expression before 'int'
   11 |     vector<int> cnt(4);
      |            ^~~
a.cc:15:11: error: 'cnt' was not declared in this scope; did you mean 'int'?
   15 |         ++cnt[x];
      |           ^~~
      |           int
a.cc:17:7: error: 'cnt' was not declared in this scope; did you mean 'int'?
   17 |     p[cnt[1]][cnt[2]][cnt[3]] = 1;
      |       ^~~
      |       int
a.cc:33:17: error: 'debug' was not declared in this scope
   33 |                 debug() << imie(a) imie(b) imie(c) imie(ev_waste);
      |                 ^~~~~
a.cc:33:28: error: 'imie' was not declared in this scope
   33 |                 debug() << imie(a) imie(b) imie(c) imie(ev_waste);
      |                            ^~~~
a.cc:60:5: error: 'printf' was not declared in this scope
   60 |     printf("%.10lf\n", ev[0][0][0]); // + cnt[1] + 2 * cnt[2] + 3 * cnt[3]);
      |     ^~~~~~
a.cc:1:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
  +++ |+#include <cstdio>
    1 | ng ll = long long;