Problem:
 Score : 100 points    Problem Statement There are N dishes, numbered 1, 2, \ldots, N .
Initially, for each i ( 1 \leq i \leq N ), Dish i has a_i ( 1 \leq a_i \leq 3 ) pieces of sushi on it.  Taro will perform the following operation repeatedly until all the pieces of sushi are eaten:   Roll a die that shows the numbers 1, 2, \ldots, N with equal probabilities, and let i be the outcome. If there are some pieces of sushi on Dish i , eat one of them; if there is none, do nothing.   Find the expected number of times the operation is performed before all the pieces of sushi are eaten.      Constraints  All values in input are integers.  1 \leq N \leq 300  1 \leq a_i \leq 3         Input Input is given from Standard Input in the following format:  N  a_1  a_2  \ldots  a_N       Output Print the expected number of times the operation is performed before all the pieces of sushi are eaten.
The output is considered correct when the relative difference is not greater than 10^{-9} .        Sample Input 1 3
1 1 1      Sample Output 1 5.5  The expected number of operations before the first piece of sushi is eaten, is 1 .
After that, the expected number of operations before the second sushi is eaten, is 1.5 .
After that, the expected number of operations before the third sushi is eaten, is 3 .
Thus, the expected total number of operations is 1 + 1.5 + 3 = 5.5 .       Sample Input 2 1
3      Sample Output 2 3  Outputs such as 3.00 , 3.000000003 and 2.999999997 will also be accepted.       Sample Input 3 2
1 2      Sample Output 3 4.5       Sample Input 4 10
1 3 2 3 3 2 3 2 1 3      Sample Output 4 54.48064457488221

Solution (Python):
from collections import Counter

n = int(input())
cnt = Counter(input().split())
c1, c2, c3 = cnt['1'], cnt['2'], cnt['3']
total = c1 + 2 * c2 + 3 * c3
c23 = c2 + c3
dp = [[[0.0] * (c3 + 1) for _ in range(c2 + c3 + 1)] for _ in range(n + 2)]

for k in range(1, n + 1):
    still = n / k
    for a1 in range(k, max(-1, 2 * k - total - 1, k - c23 - 1), -1):
        dpap, dpac, dpan = dp[a1 - 1], dp[a1], dp[a1 + 1]
        for a2 in range(min(k - a1, c2 + c3), max(-1, 3 * k - 2 * a1 - total - 1, k - a1 - c3 - 1), -1):
            a3 = k - a1 - a2
            tmp = 0
            if a1:
                tmp += dpap[a2][a3] * a1
            if a2:
                tmp += dpan[a2 - 1][a3] * a2
            if a3:
                tmp += dpac[a2 + 1][a3 - 1] * a3
            dpac[a2][a3] = tmp / k + still
print(dp[c1][c2][c3])

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = LI()
    t = [0] * 3
    for c in a:
        t[c-1] += 1
    A,B,C = t
    D = C + 1
    E = B + D
    F = A + E

    r = [[[0]*(F-k-j) for j in range(E-k)] for k in range(D)]
    for k in range(1):
        z = r[k]
        G = F - k
        for j in range(E-k):
            x = z[j]
            if j > 0:
                m = z[j-1]
            l = k + j
            for i in range(G-j):
                if not(i or l):
                    continue
                w = n
                if i > 0:
                    w += x[i-1] * i
                if j > 0:
                    w += m[i+1] * j
                x[i] = w / (i+l)


    y = r[0]
    for k in range(1, D):
        z = r[k]
        G = F - k

        x = z[0]
        p = y[1]
        x[0] = (n + p[0] * k) / k

        for i in range(1,G):
            x[i] = (n + x[i-1] * i + p[i] * k) / (i+k)

        for j in range(1,E-k):
            x = z[j]
            m = z[j-1]
            p = y[j+1]
            l = j + k
            x[0] = (n + m[1] * j + p[0] * k) / l

            for i in range(1,G-j):
                x[i] = (n + x[i-1] * i + m[i+1] * j + p[i] * k) / (i+l)

        y = z


    return "{:0.9f}".format(r[C][B][A])


print(main())

Solution (Python):
from collections import defaultdict
N = int(input())
 
counts = [0,0,0]
for a in map(int,input().split()):
  counts[a-1] += 1

A,B,C = counts





dp = [[[0]*(A+B+C+1-k-j) for j in range(B+C+1-k)] for k in range(C+1)]

t1 = None

for k in range(C+1):
  t0 = dp[k]
  for j in range(B+C+1-k):
    for i in range(A+B+C+1-k-j):
      try:
        dp[k][j][i] =((i*t0[j][i-1] if i else 0)+
                      (j*t0[j-1][i+1] if j else 0)+
                      (k*t1[j+1][i] if k else 0)+
                      N)/(i+j+k)
      except ZeroDivisionError:
        pass
  t1 = t0


print(dp[C][B][A])

Solution (Python):
n=int(input())

b=[0,0,0]
for i in map(int,input().split()):
    b[i-1]+=1
k1,k2,k3=b
dp=[[[None]*(k3+1) for _ in range(k2+k3+1)] for _ in range(k1+k2+k3+1)]
dp[0][0][0]=0

for k in range(k3+1):
    for j in range(k2+k3+1-k):
        for i in range(k1+k2+k3+1-j-k):
            if not i+j+k:continue
            dp[i][j][k]=((dp[i-1][j][k]*i if i else 0)+(dp[i+1][j-1][k]*j if j else 0)+(dp[i][j+1][k-1]*k if k else 0)+n)/(i+j+k)

print(dp[k1][k2][k3])

Solution (Python):
from collections import defaultdict
import sys
from functools import lru_cache
sys.setrecursionlimit(100000)
N = int(input())
 
counts = [0,0,0]
for a in map(int,input().split()):
  counts[a-1] += 1

A,B,C = counts





dp = [[[None]*(C+1) for _ in range(B+C+1)] for _ in range(A+B+C+1)]
dp[0][0][0] = 0

for k in range(C+1):
  for j in range(B+C+1-k):
    for i in range(A+B+C+1-k-j):
      try:
        dp[i][j][k] =((i*dp[i-1][j][k] if i else 0)+
                      (j*dp[i+1][j-1][k] if j else 0)+
                      (k*dp[i][j+1][k-1] if k else 0)+
                      N)/(i+j+k)
      except ZeroDivisionError:
        pass

print(dp[A][B][C])

Solution (Python):
import sys
input = sys.stdin.readline

N=int(input())
A=list(map(int,input().split()))

SC=[0,0,0]

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
I,J,K=0,0,0
for a in A:
    if a==1:
        I+=1
    if a==2:
        J+=1
    if a==3:
        K+=1
L=I+J+K
M=J+K
dp=[[[0.0]*(K+1) for i in range(M+1)] for i in range(L+1)]
for n in range(1,L+1):
    for m in range(min(n,M)+1):
        for k in range(min(m,K)+1):
            j=m-k
            i=n-m
            
            ret=N/(i+j+k)
            if i>0:
                ret+=dp[i-1][j][k]*(i/(i+j+k))
            if j>0:
                ret+=dp[i+1][j-1][k]*(j/(i+j+k))
            if k>0:
                ret+=dp[i][j+1][k-1]*(k/(i+j+k))
            dp[i][j][k]=ret
print(dp[I][J][K])

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
I = 0
J = 0
K = 0
for a in A:
    if a == 1:
        J += 1
        K += 1
    elif a == 2:
        K += 1

X = [[[0.0] * (j+1) for j in range(k+1)] for k in range(N+1)]

for k in range(N+1)[::-1]:
    for j in range(k+1)[::-1]:
        for i in range(j+1)[::-1]:
            if i == N:
                X[k][j][i] = 0
            else:
                X[k][j][i] = N/(N-i)
                if i < j:
                    X[k][j][i] += X[k][j][i+1] * (j-i) / (N-i)
                if j < k:
                    X[k][j][i] += X[k][j+1][i] * (k-j) / (N-i)
                if k < N:
                    X[k][j][i] += X[k+1][j][i] * (N-k) / (N-i)

print(X[K][J][I])

Solution (Python):
N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
dp = [[[0]*(c1+c2+c3+1) for i in range(c2+c3+2)] for j in range(c3+2)]
for c in range(c3+1):
    M1 = c2+c3-c
    T = dp[c-1]
    S = dp[c]
    for b in range(M1+1):
        M2 = N-b-c
        X = S[b-1]; Y = S[b]
        Z = T[b+1]
        for a in range(M2==N, M2+1):
            r = N
            if a:
                r += a*Y[a-1]
            if b:
                r += b*X[a+1]
            if c:
                r += c*Z[a]
            Y[a] = r / (a+b+c)
print("%.10f" % dp[c3][c2][c1])