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



Sample input:
3
1 1 1


Sample output:
5.5

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
al=list(map(int,input().split()))
arr=[0,0,0]
for i in al:
    arr[i-1]+=1
dp=[[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
vis=[[[False for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
 
def solve(a,b,c):
    if a==b==c==0:
        return 0
    if vis[a][b][c]:
        return dp[a][b][c]
    vis[a][b][c]=True
    if c:
        dp[a][b][c]+=solve(a,b+1,c-1)*c/n
    if b:
        dp[a][b][c]+=solve(a+1,b-1,c)*b/n
    if a:
        dp[a][b][c]+=solve(a-1,b,c)*a/n
    dp[a][b][c]=(dp[a][b][c]+1)*n/(a+b+c)
    return dp[a][b][c]

print(solve(arr[0],arr[1],arr[2]))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    A=list(map(int,input().split()))
    #1個がi個，2個がj個，3個がk個
    dp=[[[0.0]*(n+1) for j in range(n+1)]for i in range(n+1)]
    max_i,max_j=A.count(1),A.count(2)
    max_k=n-max_i-max_j
    for k in range(max_k+1):
        for j in range(max_j+max_k+1):
            for i in range(min(max_i+max_j+max_k+1,n-(j+k)+1)):
                if i+j+k==0:continue
                res=n
                if i>0:res+=dp[i-1][j][k]*i
                if j>0:res+=dp[i+1][j-1][k]*j
                if k>0:res+=dp[i][j+1][k-1]*k
                res/=i+j+k
                dp[i][j][k]=res
    print(dp[max_i][max_j][max_k])

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def rec(i,j,k):
    if dp[i][j][k]>=0:
        return dp[i][j][k]
    if i==0 and j==0 and k==0:
        return 0.0
    res=0.0
    if i>0:res+=rec(i-1,j,k)*i
    if j>0:res+=rec(i+1,j-1,k)*j
    if k>0:res+=rec(i,j+1,k-1)*k
    res+=n
    res/=i+j+k
    dp[i][j][k]=res
    return res

def main():
    global dp,n
    n=int(input())
    A=list(map(int,input().split()))
    ac=Counter(A)
    #1個がi個，2個がj個，3個がk個
    dp=[[[-1.0]*(n+1) for j in range(n+1)]for i in range(n+1)]
    print(rec(ac[1],ac[2],ac[3]))


if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))

dp = [[[-1] * (n + 1) for i in range(n + 1)] for i in range(n + 1)]

def rec(i, j, k):
    if dp[i][j][k] >= 0:
        return dp[i][j][k]
    if i == j == k == 0:
        return 0
    
    res = 0
    if i > 0:
        res += rec(i - 1, j, k) * i
    if j > 0:
        res += rec(i + 1, j - 1, k) * j
    if k > 0:
        res += rec(i, j + 1, k - 1) * k
    res += n
    res *= 1 / (i + j + k)
    
    dp[i][j][k] = res
    
    return dp[i][j][k]

one = 0
two = 0
three = 0
for i in a:
    if i == 1:
        one += 1
    elif i == 2:
        two += 1
    else:
        three += 1
    
rec(one, two, three)
print(dp[one][two][three])

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10 ** 7)

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

sushi = [0] * 4
for a in A:
    sushi[a] += 1
B, C, D = sushi[1:]


@lru_cache(maxsize=10 ** 6)
def rec(b, c, d):
    if (b, c, d) == (0, 0, 0):
        return 0
    if b < 0 or c < 0 or d < 0:
        return 0

    a = N - b - c - d
    x1 = rec(b - 1, c, d) * b / (N - a)
    x2 = rec(b + 1, c - 1, d) * c / (N - a)
    x3 = rec(b, c + 1, d - 1) * d / (N - a)
    return x1 + x2 + x3 + N / (N - a)


print(rec(B, C, D))

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10 ** 7)

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

sushi = [0] * 4
for a in A:
    sushi[a] += 1
B, C, D = sushi[1:]


@lru_cache(maxsize=10 ** 6)
def rec(a, b, c, d):
    if (b, c, d) == (0, 0, 0):
        return 0
    if b < 0 or c < 0 or d < 0:
        return 0

    x1 = rec(a + 1, b - 1, c, d) * b / (N - a)
    x2 = rec(a, b + 1, c - 1, d) * c / (N - a)
    x3 = rec(a, b, c + 1, d - 1) * d / (N - a)
    return x1 + x2 + x3 + N / (N - a)


print(rec(0, B, C, D))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
from collections import Counter
def dfs(i,j,k):
    global N
    if i==0 and j==0 and k==0:
        return 0
    if dp[i][j][k]>=0:
        return dp[i][j][k]
    else:
        res=N
        if(i>0):
            res+=dfs(i-1,j,k)*i
        if(j>0):
            res+=dfs(i+1,j-1,k)*j
        if(k>0):
            res+=dfs(i,j+1,k-1)*k
        res*=1/(i+j+k)
        dp[i][j][k]=res
        return res
dp=[[[-1]*301 for i in range(301)] for i in range(301)]
N=int(input())
a=list(map(int,input().split()))
c=Counter(a)
i,j,k=c[1],c[2],c[3]
print(dfs(i,j,k))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(float,input().split()))
one,two,three=0,0,0
for i in a:
    if i==1:
        one+=1
    elif i==2:
        two+=1
    else:
        three+=1
dp=[[[-1 for _ in range(one+two+three+1)] for _ in range(one+two+three+1)] for _ in range(one+two+three+1)]
def calc(a,b,c):
    if dp[a][b][c]>=0:
        return dp[a][b][c]
    if a==0 and b==0 and c==0:
        return 0
    x=0
    if a>0:
        x+=calc(a-1,b,c)*a
    if b>0:
        x+=calc(a+1,b-1,c)*b
    if c>0:
        x+=calc(a,b+1,c-1)*c
    x+=n
    x*=1/(a+b+c)
    dp[a][b][c]=x
    return x

print(calc(one,two,three))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]


def memo_dp(dp_list, i, j, k):
    if (i < 0) or (j < 0) or (k < 0):
        return 0
    if i + j + k == 0:
        return dp_list[i][j][k]
    else:
        pt1 = i * memo_dp(dp_list, i - 1, j, k)
        pt2 = j * memo_dp(dp_list, i + 1, j - 1, k)
        pt3 = k * memo_dp(dp_list, i, j + 1, k - 1)
        dp_list[i][j][k] = (n + pt1 + pt2 + pt3) / (i + j + k)
        return dp_list[i][j][k]


dp = [[[0.0] * (301) for _ in range(301)] for _ in range(301)]

ans = memo_dp(dp, p, q, r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]

dp = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if not (0 < i + j + k <= n):
                continue
            if i > 0:
                dp[i][j][k] += dp[i - 1][j][k] * i
            if j > 0:
                dp[i][j][k] += dp[i + 1][j - 1][k] * j
            if k > 0:
                dp[i][j][k] += dp[i][j + 1][k - 1] * k
            dp[i][j][k] += n
            dp[i][j][k] /= (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
cnt = [0, 0, 0, 0]
for ai in list(map(int, input().split())):
    cnt[ai] += 1
p, q, r = cnt[1], cnt[2], cnt[3]

dp = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if not (0 < i + j + k <= n):
                continue
            tmp = 0
            if i > 0:
                tmp += dp[i - 1][j][k] * i
            if j > 0:
                tmp += dp[i + 1][j - 1][k] * j
            if k > 0:
                tmp += dp[i][j + 1][k - 1] * k
            tmp += n
            dp[i][j][k] = tmp / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
cnt = [0, 0, 0, 0]
for x in a:
    cnt[x] += 1
p, q, r = cnt[1], cnt[2], cnt[3]

dp = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(n+1)]

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if not (0 < i + j + k <= n): continue
            tmp = 0
            if i > 0: tmp += dp[i - 1][j][k] * i
            if j > 0: tmp += dp[i + 1][j - 1][k] * j
            if k > 0: tmp += dp[i][j + 1][k - 1] * k
            tmp += n
            dp[i][j][k] = tmp / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
cnt = [0, 0, 0, 0]
for x in a:
    cnt[x] += 1
p, q, r = cnt[1], cnt[2], cnt[3]

dp = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(n+1)]

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if not (0 < i + j + k <= n):
                continue
            tmp = 0
            if i > 0:
                tmp += dp[i - 1][j][k] * i
            if j > 0:
                tmp += dp[i + 1][j - 1][k] * j
            if k > 0:
                tmp += dp[i][j + 1][k - 1] * k
            tmp += n
            dp[i][j][k] = tmp / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]

dp = []
for _ in range(n + 1):
    dp.append([[0] * (n + 1) for _ in range(n + 1)])
dp[0][0][0] = 0

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if not (0 < (i + j + k) <= n):
                continue
            tmp = 0
            if i > 0:
                tmp += dp[i - 1][j][k] * i
            if j > 0:
                tmp += dp[i + 1][j - 1][k] * j
            if k > 0:
                tmp += dp[i][j + 1][k - 1] * k
            tmp += n
            dp[i][j][k] = tmp / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]

dp = []
for _ in range(n + 1):
    dp.append([[0] * (n + 1) for _ in range(n + 1)])
dp[0][0][0] = 0

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if (i + j + k == 0) or (i + j + k) > n:
                continue
            tmp = 0
            if i > 0:
                tmp += dp[i - 1][j][k] * i
            if j > 0:
                tmp += dp[i + 1][j - 1][k] * j
            if k > 0:
                tmp += dp[i][j + 1][k - 1] * k
            tmp += n
            dp[i][j][k] = tmp / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]

dp = []
for _ in range(n + 1):
    dp.append([[0] * (r + 1) for _ in range(r + q + 1)])
dp[0][0][0] = 0

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if (i + j + k == 0) or (i + j + k) > n:
                continue
            tmp = 0
            if i > 0:
                tmp += dp[i - 1][j][k] * i
            if (j > 0) and (i < n):
                tmp += dp[i + 1][j - 1][k] * j
            if (k > 0) and (j < r + q):
                tmp += dp[i][j + 1][k - 1] * k
            tmp += n
            dp[i][j][k] = tmp / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]

dp = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if (i + j + k == 0) or (i + j + k) > n:
                continue
            tmp = 0
            if i > 0:
                tmp += dp[i - 1][j][k] * i
            if j > 0:
                tmp += dp[i + 1][j - 1][k] * j
            if k > 0:
                tmp += dp[i][j + 1][k - 1] * k
            tmp += n
            dp[i][j][k] = tmp / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]

dp = []
for _ in range(n + 1):
    dp.append([[0] * (r + 1) for _ in range(r + q + 1)])
dp[0][0][0] = 0

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if (i + j + k == 0) or (i + j + k) > n:
                continue
            tmp=0
            if i != 0:
                tmp += dp[i - 1][j][k] * i
            if (j != 0) and (i != n):
                tmp += dp[i + 1][j - 1][k] * j
            if (k != 0) and (j != r + q):
                tmp += dp[i][j + 1][k - 1] * k
            tmp += n
            dp[i][j][k] = tmp / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]

dp = []
for _ in range(n + 1):
    dp.append([[0] * (r + 1) for _ in range(r + q + 1)])
dp[0][0][0] = 0

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if (i + j + k == 0) or (i + j + k) > n:
                continue
            tmp=0
            if i != 0:
                tmp += dp[i - 1][j][k] * i
            if (j != 0) and (i != n):
                tmp += dp[i + 1][j - 1][k] * j
            if (k != 0) and (j != r + q):
                tmp += dp[i][j + 1][k - 1] * k
            tmp += n
            dp[i][j][k] = tmp / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]

dp = []
for _ in range(n + 1):
    dp.append([[0] * (r + 1) for _ in range(r + q + 1)])
dp[0][0][0] = 0

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if i != 0:
                dp[i][j][k] += dp[i - 1][j][k] * i
            if (j != 0) and (i != n):
                dp[i][j][k] += dp[i + 1][j - 1][k] * j
            if (k != 0) and (j != r + q):
                dp[i][j][k] += dp[i][j + 1][k - 1] * k
            if (i + j + k) != 0:
                dp[i][j][k] += n
                dp[i][j][k] /= (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]

dp = []
for _ in range(n + 1):
    dp.append([[0] * (r + 1) for _ in range(r + q + 1)])
dp[0][0][0] = 0

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if i != 0:
                dp[i][j][k] += dp[i - 1][j][k] * i / (i + j + k)
            if (j != 0) and (i != n):
                dp[i][j][k] += dp[i + 1][j - 1][k] * j / (i + j + k)
            if (k != 0) and (j != r + q):
                dp[i][j][k] += dp[i][j + 1][k - 1] * k / (i + j + k)
            if (i + j + k) != 0:
                dp[i][j][k] += n / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]


dp = [[[0] * (r + 1) for _ in range(r + q + 1)] for _ in range(n + 1)]
dp[0][0][0] = 0

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if i != 0:
                dp[i][j][k] += dp[i - 1][j][k] * i / (i + j + k)
            if (j != 0) and (i != n):
                dp[i][j][k] += dp[i + 1][j - 1][k] * j / (i + j + k)
            if (k != 0) and (j != r + q):
                dp[i][j][k] += dp[i][j + 1][k - 1] * k / (i + j + k)
            if (i + j + k) != 0:
                dp[i][j][k] += n / (i + j + k)

print(dp[p][q][r])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]


def memo_dp(dp_list, i, j, k):
    if (i < 0) or (j < 0) or (k < 0):
        return 0
    if dp_list[i][j][k] != -1:
        return dp_list[i][j][k]
    else:
        pt1 = i * memo_dp(dp_list, i - 1, j, k)
        pt2 = j * memo_dp(dp_list, i + 1, j - 1, k)
        pt3 = k * memo_dp(dp_list, i, j + 1, k - 1)
        dp_list[i][j][k] = (n + pt1 + pt2 + pt3) / (i + j + k)
        return dp_list[i][j][k]


dp = [[[-1] * (301) for _ in range(301)] for _ in range(301)]
dp[0][0][0] = 0

ans = memo_dp(dp, p, q, r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]


def memo_dp(dp_list, i, j, k):
    if (i < 0) or (j < 0) or (k < 0):
        return 0
    if dp_list[i][j][k] != -1:
        return dp_list[i][j][k]
    else:
        pt1 = i * memo_dp(dp_list, i - 1, j, k)
        pt2 = j * memo_dp(dp_list, i + 1, j - 1, k)
        pt3 = k * memo_dp(dp_list, i, j + 1, k - 1)
        dp_list[i][j][k] = (n + pt1 + pt2 + pt3) / (i + j + k)
        return dp_list[i][j][k]


dp = [[[-1] * (301) for _ in range(301)] for _ in range(301)]
dp[0][0][0] = 0

ans = memo_dp(dp, p, q, r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7 # 998244353
input = lambda:sys.stdin.readline().rstrip()
from functools import lru_cache
def resolve():
    n = int(input())
    counter = [0] * 3
    for a in map(int, input().split()):
        counter[a - 1] += 1

    @lru_cache(None)
    def dfs(i, j, k):
        s = i + j + k
        # base case
        if s == 0 or min(i, j, k) < 0:
            return 0
        # recursion
        res = n + i * dfs(i - 1, j, k) + j * dfs(i + 1, j - 1, k) + k * dfs(i, j + 1, k - 1)
        res /= s
        return res

    print(dfs(*counter))
resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7 # 998244353
input = lambda:sys.stdin.readline().rstrip()
from functools import lru_cache
def resolve():
    n = int(input())
    counter = [0] * 3
    for a in map(int, input().split()):
        counter[a - 1] += 1

    @lru_cache(None)
    def dfs(i, j, k):
        s = i + j + k
        # base case
        if s == 0 or min(i, j, k) < 0:
            return 0
        # recursion
        res = n + i * dfs(i - 1, j, k) + j * dfs(i + 1, j - 1, k) + k * dfs(i, j + 1, k - 1)
        res /= s
        return res

    print(dfs(*counter))
resolve()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
d={1:0,2:0,3:0}
for a in A:
    d[a]+=1
dp={(0,0,0):0}
from collections import defaultdict
while (d[3],d[2],d[1]) not in dp:
    dp2=defaultdict(float)
    for i,j,k in dp:
        if j>0 and i<d[3]:
            dp2[(i+1,j-1,k)]+=dp[(i,j,k)]*(i+1)/(i+j+k)
        if k>0 and i+j<d[2]+d[3]:
            dp2[(i,j+1,k-1)]+=dp[(i,j,k)]*(j+1)/(i+j+k)
        if i+j+k<N:
            dp2[(i,j,k+1)]+=dp[(i,j,k)]*(k+1)/(i+j+k+1)
    for i,j,k in dp2:
        dp2[(i,j,k)]+=N/(i+j+k)
    dp=dp2
print(dp[(d[3],d[2],d[1])])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
d={1:0,2:0,3:0}
for a in A:
    d[a]+=1
dp={(0,0,0):0}
import sys
sys.setrecursionlimit(10**9)
def f(i,j,k):
    if (i,j,k) in dp:
        return dp[(i,j,k)]
    dp[(i,j,k)]=N
    if i>0:
        dp[(i,j,k)]+=f(i-1,j+1,k)*i
    if j>0:
        dp[(i,j,k)]+=f(i,j-1,k+1)*j
    if k>0:
        dp[(i,j,k)]+=f(i,j,k-1)*k
    dp[(i,j,k)]/=i+j+k
    return dp[(i,j,k)]
print(f(d[3],d[2],d[1]))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)


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

#DP=[[[0]*(N+1) for index1 in range(N+1)] for index2 in range(N+1)]

def rec(i,j,k):
    global N
    global DP

    if DP[i][j][k]!=0:
        return DP[i][j][k]
    elif i==0 and j==0 and k==0:
        return DP[i][j][k]
    else:
        res=float(N)
        if i-1>=0:
            res+=rec(i-1,j,k)*float(i)
        if j-1>=0:
            res+=rec(i+1,j-1,k)*float(j)
        if k-1>=0:
            res+=rec(i,j+1,k-1)*float(k)
        
        DP[i][j][k]=res/float(i+j+k)
        return DP[i][j][k]

one=0
two=0
three=0
for l in range(N):
    if A[l]==1:
        one+=1
    elif A[l]==2:
        two+=1
    else:
        three+=1

DP=[[[float(0)]*(three+1) for index1 in range(two+three+1)] for index2 in range(N+1)]
rec(one,two,three)
print(DP[one][two][three])

Submission status: Time Limit Exceeded

Solution code:
'''
N枚の皿それぞれにa_i個の寿司があり、等確率で皿を選んだ時その皿の寿司を一つ食べる。
その皿に寿司が無い場合は何も行わない。
すべての皿がなくなるまでの操作回数の期待値を求めよ。

terms:
    1<=N<=300
    1<=a_i<=3

input:
    3
    1 1 1

output:
    5.5

N<=300: O(N^3)が通る

期待値DP。
同じ寿司の量の皿の数にさえ注目すればいいところが鍵。
'''

from collections import defaultdict,deque
import sys, bisect, math, itertools
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
    '''
    一つの整数
    '''
    return int(input())
def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


n = inp()
a = inpl()
'''
dp[i][j][k] : 
残り1個の皿がi枚、残り2個の皿がj枚、残り3個の皿がk枚の状態から
すべての寿司をなくすのに必要な回数の期待値
'''
one,two,three=0,0,0
for i in a:
    if i == 1:
        one += 1
    elif i == 2:
        two += 1
    else:
        three+=1

@lru_cache(maxsize=None)
def rec(i, j, k):
    if i == 0 and j == 0 and k == 0:
        return 0.0
    
    res = 0.0
    if i != 0:
        res += rec(i - 1, j, k) * i
    if j != 0:
        res += rec(i + 1, j - 1, k) * j
    if k != 0:
        res += rec(i, j + 1, k - 1) * k
    res += n
    res *= 1.0 / (i + j + k)
    return res

print(rec(one, two, three))


'''
dp遷移は
次に引く皿の寿司 = 次の状態 × 確率 として
zero = dp[i][j][k] * (N-i-j-k)/N
one =  dp[i-1][j][k] * i/N
two =  dp[i+1][j-1][k] * j/N
three= dp[i][j+1][k-1] * k/N
dp[i][j][k]= zero + one + two + three + 1
(最後の1で追加しているイメージ)
となる。しかし両辺にdp[i][j][k]があると更新できない。(自己ループ)
よってこれをdp[i][j][k]について解くと
dp[i][j][k] = (one + two + three + 1)*N/(i+j+k)

dp[i][j][k] = (dp[i-1][j][k]*i + dp[i+1][j-1][k]*j + dp[i][j+1][k-1]*k + N)/(i+j+k)

ただし、それぞれが0の時に例外処理が必要。
'''

Submission status: Time Limit Exceeded

Solution code:
'''
N枚の皿それぞれにa_i個の寿司があり、等確率で皿を選んだ時その皿の寿司を一つ食べる。
その皿に寿司が無い場合は何も行わない。
すべての皿がなくなるまでの操作回数の期待値を求めよ。

terms:
    1<=N<=300
    1<=a_i<=3

input:
    3
    1 1 1

output:
    5.5

N<=300: O(N^3)が通る

期待値DP。
同じ寿司の量の皿の数にさえ注目すればいいところが鍵。
'''

from collections import defaultdict,deque
import sys, bisect, math, itertools
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
    '''
    一つの整数
    '''
    return int(input())
def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


n = inp()
a = inpl()
'''
dp[i][j][k] : 
残り1個の皿がi枚、残り2個の皿がj枚、残り3個の皿がk枚の状態から
すべての寿司をなくすのに必要な回数の期待値
'''
one,two,three=[a.count(i+1) for i in range(3)]

@lru_cache(maxsize=None)
def rec(i, j, k):
    if i == 0 and j == 0 and k == 0:
        return 0.0
    
    res = 0.0
    if i != 0:
        res += rec(i - 1, j, k) * i
    if j != 0:
        res += rec(i + 1, j - 1, k) * j
    if k != 0:
        res += rec(i, j + 1, k - 1) * k
    res += n
    res *= 1.0 / (i + j + k)
    return res

print(rec(one, two, three))


'''
dp遷移は
次に引く皿の寿司 = 次の状態 × 確率 として
zero = dp[i][j][k] * (N-i-j-k)/N
one =  dp[i-1][j][k] * i/N
two =  dp[i+1][j-1][k] * j/N
three= dp[i][j+1][k-1] * k/N
dp[i][j][k]= zero + one + two + three + 1
(最後の1で追加しているイメージ)
となる。しかし両辺にdp[i][j][k]があると更新できない。(自己ループ)
よってこれをdp[i][j][k]について解くと
dp[i][j][k] = (one + two + three + 1)*N/(i+j+k)

dp[i][j][k] = (dp[i-1][j][k]*i + dp[i+1][j-1][k]*j + dp[i][j+1][k-1]*k + N)/(i+j+k)

ただし、それぞれが0の時に例外処理が必要。
'''

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
dp = [[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
I,J,K = 0,0,0
for i in a:
  if i == 1:
    I += 1
  elif i == 2:
    J += 1
  else:
    K += 1 
for k in range(n+1):
  for j in range(n+1):
    for i in range(n+1):
      if (i+j+k) <= n:
        if (i+j+k) != 0:
          dp[i][j][k] = n/(i+j+k)
        if i > 0:
          dp[i][j][k] += dp[i-1][j][k]*i / (i+j+k)
        if j > 0:
          dp[i][j][k] += dp[i+1][j-1][k]*j / (i+j+k)
        if k > 0:
          dp[i][j][k] += dp[i][j+1][k-1]*k / (i+j+k)
print(dp[I][J][K])

Submission status: Time Limit Exceeded

Solution code:
#input
N = int(input())
n1 = 0
n2 = 0
n3 = 0
for s in input().split():
    if s == "1":
        n1 += 1
    elif s == "2":
        n2 += 1
    else:
        n3 += 1
#output

#experiment
import numpy as np

def sushi(dp, N):
    for l in range(1, N+1):
        for i in range(l, -1, -1):
            for j in range(l-i, -1, -1):
                k = l-i-j

                my_sum = 0
                all = (i+j+k)/N
                loop = 1/all

                if i != 0:
                    my_sum += dp[i-1, j, k] * (i/N)
                if j != 0:
                    my_sum += dp[i+1, j-1, k] * (j/N)
                if k != 0:
                    my_sum += dp[i, j+1, k-1] * (k/N)

                dp[i, j, k] = my_sum/all + loop 

dp = np.zeros((N+1, N+1, N+1))
sushi(dp, N)

print(dp[n1, n2, n3])

Submission status: Time Limit Exceeded

Solution code:
#input
N = int(input())
a = list(map(int, input().split()))

#output

#experiment
import numpy as np
from collections import Counter

def sushi(dp, N):
    for l in range(1, N+1):
        for i in range(l, -1, -1):
            for j in range(l-i, -1, -1):
                k = l-i-j

                my_sum = 0
                all = (i+j+k)/N
                loop = 1/all

                if i != 0:
                    my_sum += dp[i-1, j, k] * (i/N)
                if j != 0:
                    my_sum += dp[i+1, j-1, k] * (j/N)
                if k != 0:
                    my_sum += dp[i, j+1, k-1] * (k/N)

                dp[i, j, k] = my_sum/all + loop 

dp = np.zeros((N+1, N+1, N+1))
sushi(dp, N)

b = Counter(a)
print(dp[b[1], b[2], b[3]])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from itertools import product
import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read():
    N = int(input().strip())
    A = map(int, input().strip().split())
    return N, A


def dfs(I, J, K, N, dp):
    for k in range(0, K+1):
        for j in range(0, J+K+1):
            for i in range(0, N+1):
                x = 0.0
                if not(i + j + k > N or i == 0 and j == 0 and k == 0):
                    x += dp[i-1][j] * (i / N) if i > 0 else 0.0
                    x += dp[i+1][j-1] * (j / N) if j > 0 else 0.0
                    x += dp[i][j+1] * (k / N) if k > 0 else 0.0
                    x += 1.0
                    x *= N / (i + j + k)
                dp[i][j] = x
    return dp[I][J]


def solve(N, A):
    dp = [[0.0 for j in range(N + 2)] for i in range(N + 2)]
    C = [0 for i in range(4)]
    for a in A:
        C[a] += 1
    return dfs(C[1], C[2], C[3], N, dp)


if __name__ == "__main__":
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys
import numpy as np

sys.setrecursionlimit(10 ** 8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = [int(x) for x in readline().split()]
ac = collections.Counter(A)
assert set(ac.keys()).issubset({1, 2, 3})
C1, C2, C3 = ac[1], ac[2], ac[3]
K = C1 + C2 + C3
dp = np.zeros((K + 3, C2 + C3 + 2, C3 + 1), dtype=np.float64)


def solve(c1, c2, c3):
    for i3 in range(c3 + 1):
        for i2 in range(c2 + c3 + 1):
            for i1 in range(N - i2 - i3 + 1):
                isum = i1 + i2 + i3
                if isum == 0:
                    continue
                i0 = N - i1 - i2 - i3
                if i0 < 0:
                    continue
                e = i0
                if i1 > 0:
                    e += i1 * (1.0 + dp[i1 - 1, i2, i3])
                if i2 > 0 and i1 <= K:
                    e += i2 * (1.0 + dp[i1 + 1, i2 - 1, i3])
                if i3 > 0 and i2 + 1 <= c2 + c3:
                    e += i3 * (1.0 + dp[i1, i2 + 1, i3 - 1])
                dp[i1, i2, i3] = e / isum
    return dp[c1, c2, c3]


if __name__ == "__main__":
    print(solve(C1, C2, C3))

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys

sys.setrecursionlimit(10 ** 8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = [int(x) for x in readline().split()]
ac = collections.Counter(A)
assert set(ac.keys()).issubset({1, 2, 3})
K = sum(ac.values())


dp = [[[None] * (K + 1) for _ in range(K + 1)] for _ in range(K + 1)]


def solve(c1, c2, c3):
    csum = c1 + c2 + c3
    if csum == 0:
        return 0.0
    cached = dp[c1][c2][c3]
    if cached is not None:
        return cached
    ans = 0.0
    if c1 > 0:
        ans += c1 / csum * (1.0 + solve(c1 - 1, c2, c3))
    if c2 > 0:
        ans += c2 / csum * (1.0 + solve(c1 + 1, c2 - 1, c3))
    if c3 > 0:
        ans += c3 / csum * (1.0 + solve(c1, c2 + 1, c3 - 1))
    c0 = N - csum
    ans += c0 / csum
    dp[c1][c2][c3] = ans
    return ans


if __name__ == "__main__":
    print(solve(ac[1], ac[2], ac[3]))