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())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(n-k+1):
        for i in range(n-k-j+1):
            if i+j+k == 0:
                continue
            DP[i][j][k] = n
            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] /= (i+j+k)
print(DP[a][b][c])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(n+1):
    for j in range(n-k+1):
        for i in range(n-k-j+1):
            if i+j+k == 0:
                continue
            DP[i][j][k] = n
            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] /= (i+j+k)
print(DP[a][b][c])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[None for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
DP[0][0][0] = 0.0
def dp(i, j, k):
    if i < 0 or j < 0 or k < 0:
        return 0.0
    if DP[i][j][k] != None:
        return DP[i][j][k]
    DP[i][j][k] = (n+dp(i-1, j, k)*i+dp(i+1, j-1, k)*j+dp(i, j+1, k-1)*k)/(i+j+k)
    return DP[i][j][k]
print(dp(a, b, c))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[None for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
DP[0][0][0] = 0
def dp(i, j, k):
    if i < 0 or j < 0 or k < 0:
        return 0
    if DP[i][j][k] != None:
        return DP[i][j][k]
    DP[i][j][k] = (n+dp(i-1, j, k)*i+dp(i+1, j-1, k)*j+dp(i, j+1, k-1)*k)/(i+j+k)
    return DP[i][j][k]
print(dp(a, b, c))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(float, input().split()))
def solve():
    dp = [[[0]*(N+1-i-j) for j in range(N+1-i)] for i in range(N+1)]
    dp[N][0][0] = 0
    for i in range(N-1,-1,-1):
        for j in range(N-i,-1,-1):
            for k in range(N-i-j,-1,-1):
                dp[i][j][k] = 1/(1-i/N)
                if j != 0:
                    dp[i][j][k] += dp[i+1][j-1][k]*(j/(N-i))
                if k != 0:
                    dp[i][j][k] += dp[i][j+1][k-1]*(k/(N-i))
                if i+j+k != N:
                    dp[i][j][k] += dp[i][j][k+1]*(1-(j+k)/(N-i))
    c = {}
    for i in range(1,3):
        c[i] = A.count(i)
    ans = dp[0][c[1]][c[2]]
    # print(*dp,sep='\n')
    return ans
print(solve())

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=N
        if i-1>=0:
            res+=rec(i-1,j,k)*i
        if j-1>=0:
            res+=rec(i+1,j-1,k)*j
        if k-1>=0:
            res+=rec(i,j+1,k-1)*k
        
        DP[i][j][k]=res/(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=[[[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:
import queue

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

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



#ノード(index1,index2,index3)の値が影響を与えるノード
def enable(index1,index2,index3):
    global N

    E=[]
    if index1+index2+index3<N:
        E.append((index1+1,index2,index3))
    if index1>0:
        E.append((index1-1,index2+1,index3))
    if index2>0:
        E.append((index1,index2-1,index3+1))
    
    return E


#Deg[i][j][k]=ノード(i,j,k)の出次数
#Deg=[[[0]*(three+1) for d1 in range(two+three+1)] for d2 in range(N+1)]
Deg=[[[0]*(N+1) for d1 in range(N+1)] for d2 in range(N+1)]
#G[i][j][k]=ノード(i,j,k)に来るエッジのもう一方の頂点
#G=[[[[] for g1 in range(three+1)] for g1 in range(two+three+1)] for g2 in range(N+1)]
G=[[[[] for g1 in range(N+1)] for g1 in range(N+1)] for g2 in range(N+1)]
#qはシンクを収納するキュー
q=queue.Queue()
#orderはトポロジカルソートの逆順にノードを収納するスタック
order=queue.LifoQueue()


for row1 in range(N+1):
    for row2 in range(N+1-row1):
        for row3 in range(N+1-row1-row2):
            Enables=enable(row1,row2,row3)
            if len(Enables)==0:
                #print('Yes')
                q.put((row1,row2,row3))
            Deg[row1][row2][row3]=len(Enables)
            for e in Enables:
                #print((row1,row2,row3),e)
                G[e[0]][e[1]][e[2]].append((row1,row2,row3))

while not q.empty():
    now=q.get()
    order.put(now)
    for next in G[now[0]][now[1]][now[2]]:
        Deg[next[0]][next[1]][next[2]]-=1
        if Deg[next[0]][next[1]][next[2]]==0:
            q.put(next)


DP=[[[0]*(N+1) for dp1 in range(N+1)] for dp2 in range(N+1)]
while True:
    now=order.get()
    i=now[0]
    j=now[1]
    k=now[2]
    if i==j==k==0:
        DP[i][j][k]=0
    else:
        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)
    
    if i==one and j==two and k==three:
        break

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

Submission status: Time Limit Exceeded

Solution code:
# 再帰回数の上限を引き上げる
import sys
sys.setrecursionlimit(10**7)
n=int(input())
a=list(map(int,input().split()))
a0,a1,a2,a3=0,0,0,0
for ai in a:
      if ai==0:
            a0+=1
      elif ai==1:
            a1+=1
      elif ai==2:
            a2+=1
      elif ai==3:
            a3+=1
aa=a0+a1+a2+a3
memo=[[[-1]*(aa+1) for _ in range(aa+1)] for _ in range(aa+1)]
#print(memo)
memo[1][0][0]=n
def dp(i,j,k):
      if memo[i][j][k]>0:
            return memo[i][j][k]
      else:
            m=i+j+k
            c=n
            if i>0:
                  c+=i*dp(i-1,j,k)
            if j>0:
                  c+=j*dp(i+1,j-1,k)
            if k>0:
                  c+=k*dp(i,j+1,k-1)
            memo[i][j][k]=c/m
            return c/m
print(dp(a1,a2,a3))

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=N
        if i-1>=0:
            res+=rec(i-1,j,k)*i
        if j-1>=0:
            res+=rec(i+1,j-1,k)*j
        if k-1>=0:
            res+=rec(i,j+1,k-1)*k
        
        DP[i][j][k]=res/(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

rec(one,two,three)
print(DP[one][two][three])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from functools import lru_cache
import sys

sys.setrecursionlimit(10 ** 7)

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


def submit():
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)

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

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

    print(rec(c[1], c[2], c[3], n))

submit()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from functools import lru_cache
import sys

sys.setrecursionlimit(10 ** 7)

@lru_cache(maxsize=None)
def dp(i, j, k, n):
    if i == j == k == 0:
        return 0
    ret = 0
    if i > 0:
        ret += i * dp(i - 1, j, k, n)
    if j > 0:
        ret += j * dp(i + 1, j - 1, k, n)
    if k > 0:
        ret += k * dp(i, j + 1, k - 1, n)
    ret += n
    ret /= i + j + k
    return ret


def submit():
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)
    print(dp(c[1], c[2], c[3], n))


submit()

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
# 許容する再帰処理の回数を変更
sys.setrecursionlimit(10**5+10)
# input処理を高速化する
input = lambda: sys.stdin.readline().rstrip()

N = int(input())
a = list(map(float, input().split()))
one = a.count(1)
two = a.count(2)
three = a.count(3)

#dp[i][j][k]は残1がi枚、残2がj枚、残3がk枚のときの残り回数の期待値
#dp = [[[0.0 for _ in range(N+1)] for _ in range(N+1)] for _ in range(N+1)]
dp = np.full((N+1, N+1, N+1), -1.0)
dp[0][0][0] = 0.0

def rec(i, j, k):
    if dp[i][j][k] > -1:
        return dp[i][j][k]

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

print(rec(one, two, three))
#print(dp)

Submission status: Time Limit Exceeded

Solution code:
import sys
# 許容する再帰処理の回数を変更
sys.setrecursionlimit(10**5+10)
# input処理を高速化する
input = lambda: sys.stdin.readline().rstrip()

N = int(input())
a = list(map(float, input().split()))
one = a.count(1)
two = a.count(2)
three = a.count(3)

#dp[i][j][k]は残1がi枚、残2がj枚、残3がk枚のときの残り回数の期待値
dp = [[[-1 for _ in range(N+1)] for _ in range(N+1)] for _ in range(N+1)]
dp[0][0][0] = 0
#メモ化再帰
def rec(i, j, k):
    if dp[i][j][k] > -1:
        return dp[i][j][k]

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

print(rec(one, two, three))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
N = int(input())
a = list(map(int, input().split()))

#期待値dp
#dp[i][j][k]:寿司が残り1個の皿がi枚、
#2個の皿がj枚、3枚の皿がk枚の時、寿司を全て無くすのに
#必要な操作回数の期待値
#dp[i][j][k] = ((dp[i][j][k]+1)*(N-i-j-k)/N+
#(dp[i-1][j][k]+1)*i/N+(dp[i+1][j-1][k]+1)*j/N+
#(dp[i][j+1][k-1]+1)*k/N)
#各項分子の+1は操作回数1回分の1
#自己ループがあるので式変形

dp = [[[-1 for i in range(N+1)] for j in range(N+1)] for k in range(N+1)]

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

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

ans = rec(one, two, three)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
c = Counter(map(int, input().split()))
dp = [[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]

for i in range(n+1):
    for j in range(n+1):
        for k in range(n+1):
            non_zero = i+j+k
            if non_zero == 0:
                continue
            dp[i][j][k] = n
            if 0 <= i-1 and j+1 <= n:
                dp[i][j][k] += dp[i-1][j+1][k]*i
            if 0 <= j-1 and k+1 <= n:
                dp[i][j][k] += dp[i][j-1][k+1]*j
            if 0 <= k-1:
                dp[i][j][k] += dp[i][j][k-1]*k
            dp[i][j][k] /= non_zero
print(dp[c[3]][c[2]][c[1]])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = [0]*4
for x in map(int, input().split()):
    c[x] += 1
dp = [[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]

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

print(dp[c[3]][c[2]][c[1]])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
c = Counter(map(int, input().split()))
dp = [[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
for i in range(n+1):
    for j in range(n+1):
        for k in range(n+1):
            non_zero = i+j+k
            if non_zero == 0:
                continue
            dp[i][j][k] = n/non_zero
            if 0 <= i-1 and j+1 <= n:
                dp[i][j][k] += dp[i-1][j+1][k]*i/non_zero
            if 0 <= j-1 and k+1 <= n:
                dp[i][j][k] += dp[i][j-1][k+1]*j/non_zero
            if 0 <= k-1:
                dp[i][j][k] += dp[i][j][k-1]*k/non_zero
print(dp[c[3]][c[2]][c[1]])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
c = Counter(map(int, input().split()))
dp = [[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
for i in (range(n+1)):
    for j in (range(n+1)):
        for k in (range(n+1)):
            non_zero = i+j+k
            if non_zero == 0:
                continue
            dp[i][j][k] = n/non_zero
            if 0 <= i-1 and j+1 <= n:
                dp[i][j][k] += dp[i-1][j+1][k]*i/non_zero
            if 0 <= j-1 and k+1 <= n:
                dp[i][j][k] += dp[i][j-1][k+1]*j/non_zero
            if 0 <= k-1:
                dp[i][j][k] += dp[i][j][k-1]*k/non_zero
print(dp[c[3]][c[2]][c[1]])

Submission status: Time Limit Exceeded

Solution code:
#解説AC
n=int(input())
a=list(map(int,input().split()))
dp=[[[-1]*(301) for i in range(301)] for j in range(301)]
# dp[a][b][c]=(1個の皿がa枚,2個の皿がb枚,3個の皿がc枚ある時, 全て食べ終わるまでの操作回数の期待値)
# dp[a][b][c]の遷移
#               確率          行先
# 1枚が引かれる  a/N　　　    dp[a-1][b][c]
# 2枚が引かれる  b/N　　　    dp[a+1][b-1][c]
# 3枚が引かれる  c/N　　　    dp[a][b+1][c-1]
# 0枚が引かれる  (N-a-b-c)/N　dp[a][b][c]

#dp[a][b][c]=dp[a-1][b][c]*a/n+dp[a+1][b-1][c]*b/n+dp[a][b+1][c-1]*c/n+dp[a][b][c]*(n-a-b-c)/n+1
#dp[a][b][c]=(dp[a-1][b][c]*a+dp[a+1][b-1][c]*b+dp[a][b+1][c-1]*c+n)/(a+b+c)

def memo(i,j,k):
    if dp[i][j][k]+1:
        return dp[i][j][k]
    ans=n
    if i>0:
        ans+=memo(i-1,j,k)*i
    if j>0:
        ans+=memo(i+1,j-1,k)*j
    if k>0:
        ans+=memo(i,j+1,k-1)*k
    dp[i][j][k]=ans/(i+j+k)
    return ans/(i+j+k)
dp[0][0][0]=0
x,y,z=0,0,0
for i in a:
    if i==1:
        x+=1
    elif i==2:
        y+=1
    else:
        z+=1
print(memo(x,y,z))

Submission status: Time Limit Exceeded

Solution code:
#解説AC
n=int(input())
a=list(map(int,input().split()))
dp=[[[-1]*(n+1) for i in range(n+1)] for j in range(n+1)]
# dp[a][b][c]=(1個の皿がa枚,2個の皿がb枚,3個の皿がc枚ある時, 全て食べ終わるまでの操作回数の期待値)
# dp[a][b][c]の遷移
#               確率          行先
# 1枚が引かれる  a/N　　　    dp[a-1][b][c]
# 2枚が引かれる  b/N　　　    dp[a+1][b-1][c]
# 3枚が引かれる  c/N　　　    dp[a][b+1][c-1]
# 0枚が引かれる  (N-a-b-c)/N　dp[a][b][c]

#dp[a][b][c]=dp[a-1][b][c]*a/n+dp[a+1][b-1][c]*b/n+dp[a][b+1][c-1]*c/n+dp[a][b][c]*(n-a-b-c)/n+1
#dp[a][b][c]=(dp[a-1][b][c]*a+dp[a+1][b-1][c]*b+dp[a][b+1][c-1]*c+n)/(a+b+c)

def memo(i,j,k):
    if dp[i][j][k]+1:
        return dp[i][j][k]
    ans=n
    if i>0:
        ans+=memo(i-1,j,k)*i
    if j>0:
        ans+=memo(i+1,j-1,k)*j
    if k>0:
        ans+=memo(i,j+1,k-1)*k
    dp[i][j][k]=ans/(i+j+k)
    return ans/(i+j+k)
dp[0][0][0]=0
x,y,z=0,0,0
for i in a:
    if i==1:
        x+=1
    elif i==2:
        y+=1
    else:
        z+=1
print(memo(x,y,z))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k == 0:
                continue
            tmp = n
            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 < n:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k == 0:
                continue
            tmp = n
            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 < n:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            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
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())

# メモ化再帰
DP = [[[-1 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]


def f(i, j, k):
    if DP[i][j][k] != -1:
        return DP[i][j][k]
    if i+j+k == 0:
        return 0
    tmp = 0.0
    if i > 0:
        tmp += i*f(i-1, j, k)
    if j > 0:
        tmp += j*f(i+1, j-1, k)
    if k > 0:
        tmp += k*f(i, j+1, k-1)
    tmp += n
    tmp /= i+j+k
    DP[i][j][k] = tmp
    return tmp


i, j, k = A.count(1), A.count(2), A.count(3)
print(f(i, j, k))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
a, b, c = 0, 0, 0
for x in A:
    if x == 1:
        a += 1
    if x == 2:
        b += 1
    if x == 3:
        c += 1
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            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
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            tmp = 0
            if i+j+k > 0:
                tmp += n/(i+j+k)
            if i > 0:
                tmp += DP[i-1][j][k]*i/(i+j+k)
            if j > 0:
                tmp += DP[i+1][j-1][k]*j/(i+j+k)
            if k > 0:
                tmp += DP[i][j+1][k-1]*k/(i+j+k)
            DP[i][j][k] = tmp
print(DP[a][b][c])

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    *A, = map(int, input().split())
    a, b, c = A.count(1), A.count(2), A.count(3)
    DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
    for k in range(c+1):
        for j in range(b+c-k+1):
            for i in range(n-j-k+1):
                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[a][b][c])


main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            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[a][b][c])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c+1):
        for i in range(n-j-k+1):
            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[a][b][c])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(n+1):
    for j in range(n+1-k):
        for i in range(n+1-k-j):
            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)
*A, = map(int, input().split())
i, j, k = A.count(1), A.count(2), A.count(3)
print(DP[i][j][k])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+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)
*A, = map(int, input().split())
i, j, k = A.count(1), A.count(2), A.count(3)
print(DP[i][j][k])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
DP = [[[-1 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]


def f(i, j, k):
    if DP[i][j][k] != -1:
        return DP[i][j][k]
    if i+j+k == 0:
        return 0
    tmp = 0
    if i > 0:
        tmp += i*f(i-1, j, k)
    if j > 0:
        tmp += j*f(i+1, j-1, k)
    if k > 0:
        tmp += k*f(i, j+1, k-1)
    tmp += n
    tmp /= i+j+k
    DP[i][j][k] = tmp
    return tmp


*A, = map(int, input().split())
i, j, k = A.count(1), A.count(2), A.count(3)
print(f(i, j, k))

Submission status: Time Limit Exceeded

Solution code:
def solve(a):
    n = len(a)
    dp = [[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]

    def _solve(c1, c2, c3):
        if c1 == c2 == c3 == 0:
            return 0
        if dp[c1][c2][c3] == 0:
            s = n
            if c1 > 0:
                s += _solve(c1-1, c2, c3) * c1
            if c2 > 0:
                s += _solve(c1+1, c2-1, c3) * c2
            if c3 > 0:
                s += _solve(c1, c2+1, c3-1) * c3
            dp[c1][c2][c3] = s / (c1 + c2 + c3)
        return dp[c1][c2][c3]

    c1 = c2 = c3 = 0
    for x in a:
        c1 += int(x == 1)
        c2 += int(x == 2)
        c3 += int(x == 3)
    return _solve(c1, c2, c3)

input()
a = list(map(float, input().split()))
print(solve(a))

Submission status: Time Limit Exceeded

Solution code:
def solve(a):
    n = len(a)
    dp = [[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]

    def _solve(c1, c2, c3):
        if c1 == c2 == c3 == 0:
            return 0
        if dp[c1][c2][c3] == 0:
            s = n
            if c1 > 0:
                s += _solve(c1-1, c2, c3) * c1
            if c2 > 0:
                s += _solve(c1+1, c2-1, c3) * c2
            if c3 > 0:
                s += _solve(c1, c2+1, c3-1) * c3
            dp[c1][c2][c3] = s / (c1 + c2 + c3)
        return dp[c1][c2][c3]

    return _solve(a.count(1), a.count(2), a.count(3))

input()
a = list(map(float, input().split()))
print(solve(a))

Submission status: Time Limit Exceeded

Solution code:
def solve(a):
    c1 = a.count(1)
    c2 = a.count(2)
    c3 = a.count(3)
    dp = {}

    def _solve(c1, c2, c3):
        if c1 == c2 == c3 == 0:
            return 0
        k = (c1, c2, c3)
        if k not in dp:
            s = len(a)
            if c1 > 0:
                s += _solve(c1-1, c2, c3) * c1
            if c2 > 0:
                s += _solve(c1+1, c2-1, c3) * c2
            if c3 > 0:
                s += _solve(c1, c2+1, c3-1) * c3
            dp[k] = s / (c1 + c2 + c3)
        return dp[k]

    return _solve(c1, c2, c3)

input()
a = list(map(float, input().split()))
print(solve(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(v) for v in input().split()]

memo = [[[-1.0  for _ in range(301)] for _ in range(301)] for _ in range(301)]

one,two,three = 0,0,0
for a in A:
    if a == 1: one += 1
    elif a == 2: two += 1
    else: three += 1

def dfs(i,j,k):
    if memo[i][j][k] != -1.0: return memo[i][j][k]
    if i == 0 and j == 0 and k == 0: return 0.0

    ret = 0.0
    if i != 0: ret += i * dfs(i-1,j,k)
    if j != 0: ret += j * dfs(i+1,j-1,k)
    if k != 0: ret += k * dfs(i,j+1,k-1)
    ret += N
    ret *= 1.0/(i+j+k)
    memo[i][j][k] = ret
    return ret

print(dfs(one,two,three))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(v) for v in input().split()]

memo = [[[-1.0  for _ in range(301)] for _ in range(301)] for _ in range(301)]

one,two,three = 0,0,0
for a in A:
    if a == 1: one += 1
    elif a == 2: two += 1
    else: three += 1

def dfs(i,j,k):
    if memo[i][j][k] != -1.0: return memo[i][j][k]
    if i == 0 and j == 0 and k == 0: return 0

    ret = 0
    if i != 0: ret += i * dfs(i-1,j,k)
    if j != 0: ret += j * dfs(i+1,j-1,k)
    if k != 0: ret += k * dfs(i,j+1,k-1)
    ret += N
    ret *= 1.0/(i+j+k)
    memo[i][j][k] = ret
    return ret

print(dfs(one,two,three))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
 
def rec(i, j, k):
    global dp
    if dp[i][j][k] != -1.0:
        return dp[i][j][k]
    if i+j+k == 0:
        return 0.0
 
    num = i+j+k
    ans = N_f
    if i > 0:
        ans += (rec(i-1, j, k) * i)
    if j > 0:
        ans += (rec(i+1, j-1, k) * j)
    if k > 0:
        ans += (rec(i, j+1, k-1) * k)
    ans /= num
    dp[i][j][k] = ans
    return ans
 
N = ii()
N_f = float(N)
A = li()
 
cnt = [0, 0, 0]
for a in A:
    cnt[a-1] += 1
 
dp = dp3(-1.0, cnt[2]+1, cnt[1]+cnt[2]+1, N+1)
dp[0][0][0] = 0
 
print(rec(cnt[0], cnt[1], cnt[2]))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

def rec(k, j, i):
    global dp
    if dp[k][j][i] != -1.0:
        return dp[k][j][i]
    if i+j+k == 0:
        return 0.0

    num = i+j+k
    ans = N_f / num
    if i > 0:
        ans += (rec(k, j, i-1) * i) / num
    if j > 0:
        ans += (rec(k, j-1, i+1) * j) / num
    if k > 0:
        ans += (rec(k-1, j+1, i) * k) / num
    dp[k][j][i] = ans
    return ans

N = ii()
N_f = float(N)
A = li()

cnt = [0, 0, 0]
for a in A:
    cnt[a-1] += 1

#dp = dp3(-1.0, cnt[2]+1, cnt[1]+cnt[2]+1, N+1)
dp = dp3(-1.0, N+1, cnt[1]+cnt[2]+1, cnt[2]+1)

print(rec(cnt[2], cnt[1], cnt[0]))