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:
from functools import lru_cache
import sys
sys.setrecursionlimit(100000000)
n = int(input())
a = [int(i) for i in input().split(' ')]
z,o,tw,th = 0,0,0,0
for i in a:
    if i==0:
        z+=1
    elif i==1:
        o+=1
    elif i==2:
        tw+=1
    else:
        th+=1

def expectedValue(z,o,tw, th):
    if o+tw+th==0:
        return 0
    return (z+o+tw+th)/(o+th+tw)

def solve(z,o,tw,th):
    dp = [[[0]*(th+1) for i in range(th+tw+1)] for i in range(n+1)]
    dp[0][0][0] = 0 
    for th_1 in range(th+1):
        for tw_1 in range(th+tw+1):
            for o_1 in range(n+1):
                dp[o_1][tw_1][th_1] = expectedValue(n-o_1-tw_1-th_1, o_1,tw_1,th_1) 
                if o_1>0:
                    dp[o_1][tw_1][th_1] += ((o_1)/(o_1+tw_1+th_1))*dp[o_1-1][tw_1][th_1] 
                if tw_1>0 and o_1<n:
                    dp[o_1][tw_1][th_1] += ((tw_1)/(o_1+tw_1+th_1))*dp[o_1+1][tw_1-1][th_1] 
                if th_1>0 and tw_1<th+tw:
                    dp[o_1][tw_1][th_1] += ((th_1)/(o_1+tw_1+th_1))*dp[o_1][tw_1+1][th_1-1]
    
    return dp[o][tw][th]                
print(solve(z,o,tw,th))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
import sys
sys.setrecursionlimit(100000000)
n = int(input())
a = [int(i) for i in input().split(' ')]
z,o,tw,th = 0,0,0,0
for i in a:
    if i==0:
        z+=1
    elif i==1:
        o+=1
    elif i==2:
        tw+=1
    else:
        th+=1

def expectedValue(z,o,tw, th):
    if o+tw+th==0:
        return 0
    return (z+o+tw+th)/(o+th+tw)

def solve(z,o,tw,th):
    dp = [[[0]*(n+1) for i in range(n+1)] for i in range(n+1)]
    dp[0][0][0] = 0 
    for th_1 in range(n+1):
        for tw_1 in range(n+1):
            for o_1 in range(n+1):
                dp[o_1][tw_1][th_1] = expectedValue(n-o_1-tw_1-th_1, o_1,tw_1,th_1) 
                if o_1>0:
                    dp[o_1][tw_1][th_1] += ((o_1)/(o_1+tw_1+th_1))*dp[o_1-1][tw_1][th_1] 
                if tw_1>0 and o_1<n:
                    dp[o_1][tw_1][th_1] += ((tw_1)/(o_1+tw_1+th_1))*dp[o_1+1][tw_1-1][th_1] 
                if th_1>0 and tw_1<n:
                    dp[o_1][tw_1][th_1] += ((th_1)/(o_1+tw_1+th_1))*dp[o_1][tw_1+1][th_1-1]
    
    return dp[o][tw][th]                
print(solve(z,o,tw,th))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
import sys
sys.setrecursionlimit(100000000)
n = int(input())
a = [int(i) for i in input().split(' ')]
z,o,tw,th = 0,0,0,0
for i in a:
    if i==0:
        z+=1
    elif i==1:
        o+=1
    elif i==2:
        tw+=1
    else:
        th+=1

def expectedValue(z,o,tw, th):
    if o+tw+th==0:
        return 0
    return (z+o+tw+th)/(o+th+tw)

@lru_cache(None)
def solve(z,o,tw,th):
    ans = expectedValue(z,o,tw,th)
    # print(z,o,tw,th, ans)
    if(o>0):
        ans +=  ((o)/(o+tw+th))* (solve(z+1,o-1,tw, th))
    if(tw>0):
        ans +=  ((tw)/(o+tw+th))* (solve(z,o+1,tw-1, th))
    if(th>0):
        ans +=  ((th)/(o+tw+th))* (solve(z,o,tw+1, th-1))
    # print(z,o,tw,th,ans)
    return ans

print(solve(z,o,tw,th))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)

N = int(input())
if N == 1:
    d = [int(input())]
else:
    d = list(map(int, input().split()))

d = Counter(d)

a = d[1]
b = d[2]
c = d[3]


# flag = [[[0]*301 for j in range(301)] for i in range(301)]
# dp = [[[0]*301 for j in range(301)] for i in range(301)]

@functools.lru_cache(None)
def f(x,y,z):
    # if flag[x][y][z] == 1:
    #     return dp[x][y][z]

    # flag[x][y][z] = 1

    w = x + y + z

    if w == 0:
        # dp[x][y][z] = 0.0
        return 0
    else:
        v = float(N) / w
        if x >= 1: v += f(x - 1, y, z) * float(x) / w
        if y >= 1: v += f(x + 1, y - 1, z) * float(y) / w
        if z >= 1: v += f(x, y + 1, z - 1) * float(z) / w
        # dp[x][y][z] = v
        return v

print(f(a,b,c))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)

N = int(input())
if N == 1:
    d = [int(input())]
else:
    d = list(map(int, input().split()))

d = Counter(d)

a = d[1]
b = d[2]
c = d[3]


flag = [[[0]*301 for j in range(301)] for i in range(301)]
dp = [[[0]*301 for j in range(301)] for i in range(301)]

# @functools.lru_cache(None)
def f(x,y,z):
    if flag[x][y][z] == 1:
        return dp[x][y][z]

    flag[x][y][z] = 1

    w = x + y + z

    if w == 0:
        dp[x][y][z] = 0.0
        return 0
    else:
        v = float(N) / w
        if x >= 1: v += f(x - 1, y, z) * float(x) / w
        if y >= 1: v += f(x + 1, y - 1, z) * float(y) / w
        if z >= 1: v += f(x, y + 1, z - 1) * float(z) / w
        dp[x][y][z] = v
        return v

print(f(a,b,c))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)

N = int(input())
if N == 1:
    d = [int(input())]
else:
    d = list(map(int, input().split()))

d = Counter(d)

a = d[1]
b = d[2]
c = d[3]



@functools.lru_cache(None)
def f(x,y,z):
    w = x + y + z

    if w == 0:
        return 0
    else:
        v = float(N) / w
        if x >= 1: v += f(x - 1, y, z) * float(x) / w
        if y >= 1: v += f(x + 1, y - 1, z) * float(y) / w
        if z >= 1: v += f(x, y + 1, z - 1) * float(z) / w
        return v

print(f(a,b,c))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

N = int(input())
if N == 1:
    d = [int(input())]
else:
    d = list(map(int, input().split()))

d = Counter(d)

a = d[1]
b = d[2]
c = d[3]

@functools.lru_cache(None)
def f(x,y,z):
    p = float(x)/N
    q = float(y)/N
    r = float(z)/N
    w = p+q+r

    if x+y+z == 0:
        return 0
    else:
        v = 1.0/w
        if x >= 1: v += f(x - 1, y, z) * p / w
        if y >= 1: v += f(x + 1, y - 1, z) * q / w
        if z >= 1: v += f(x, y + 1, z - 1) * r / w
        return v

print(f(a,b,c))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)

## a, bを無向辺として，隣接リストを作成
N = int(input())
if N == 1:
    d = [int(input())]
else:
    d = list(map(int, input().split()))

d = Counter(d)

a = d[1]
b = d[2]
c = d[3]

@functools.lru_cache(None)
def f(x,y,z):
    p = float(x)/N
    q = float(y)/N
    r = float(z)/N
    w = p+q+r

    if x+y+z == 0:
        return 0
    else:
        v = 1.0/w
        if x >= 1: v += f(x - 1, y, z) * p / w
        if y >= 1: v += f(x + 1, y - 1, z) * q / w
        if z >= 1: v += f(x, y + 1, z - 1) * r / w
        return v

print(f(a,b,c))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
num1,num2,num3=0,0,0
for i in range(n):
    if a[i]==1:
        num1+=1
    elif a[i]==2:
        num2+=1
    else:
        num3+=1
dp=[[[-1 for k in range(n+1)] for i in range(n+1)] for j in range(n+1)]
def f(n1,n2,n3):
    if dp[n1][n2][n3]>=0:
        return dp[n1][n2][n3]
    if n1==n2==n3==0:
        return 0.0
    ret=0.0
    if n1>0:
        ret+=f(n1-1,n2,n3)*n1
    if n2>0:
        ret+=f(n1+1,n2-1,n3)*n2
    if n3>0:
        ret+=f(n1,n2+1,n3-1)*n3
    ret+=n
    ret*=1.0/(n1+n2+n3)
    dp[n1][n2][n3]=ret
    return dp[n1][n2][n3]
print(f(num1,num2,num3))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**4)
n=int(input())
a=list(map(int,input().split()))
num1,num2,num3=0,0,0
for i in range(n):
    if a[i]==1:
        num1+=1
    elif a[i]==2:
        num2+=1
    else:
        num3+=1
dp=[[[-1 for k in range(n+1)] for i in range(n+1)] for j in range(n+1)]
def f(n1,n2,n3):
    if dp[n1][n2][n3]>=0:
        return dp[n1][n2][n3]
    if n1==n2==n3==0:
        return 0.0
    ret=0.0
    if n1>0:
        ret+=f(n1-1,n2,n3)*n1
    if n2>0:
        ret+=f(n1+1,n2-1,n3)*n2
    if n3>0:
        ret+=f(n1,n2+1,n3-1)*n3
    ret+=n
    ret*=1.0/(n1+n2+n3)
    dp[n1][n2][n3]=ret
    return dp[n1][n2][n3]
print(f(num1,num2,num3))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**4)
n=int(input())
a=list(map(int,input().split()))
num1,num2,num3=0,0,0
for i in range(n):
    if a[i]==1:
        num1+=1
    elif a[i]==2:
        num2+=1
    else:
        num3+=1
dp=[[[-1 for k in range(n+1)] for i in range(n+1)] for j in range(n+1)]
def f(n1,n2,n3):
    if dp[n1][n2][n3]>=0:
        return dp[n1][n2][n3]
    if n1==n2==n3==0:
        return 0
    ret=0
    if n1>0:
        ret+=f(n1-1,n2,n3)*n1
    if n2>0:
        ret+=f(n1+1,n2-1,n3)*n2
    if n3>0:
        ret+=f(n1,n2+1,n3-1)*n3
    ret+=n
    ret*=1/(n1+n2+n3)
    dp[n1][n2][n3]=ret
    return dp[n1][n2][n3]
print(f(num1,num2,num3))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**4)
    input=sys.stdin.readline
    n=int(input())
    a=list(map(int,input().split()))
    num1,num2,num3=0,0,0
    for i in range(n):
        if a[i]==1:
            num1+=1
        elif a[i]==2:
            num2+=1
        else:
            num3+=1
    dp=[[[-1 for k in range(n+1)] for i in range(n+1)] for j in range(n+1)]
    def f(n1,n2,n3):
        if dp[n1][n2][n3]>=0:
            return dp[n1][n2][n3]
        if n1==n2==n3==0:
            return 0
        ret=0
        if n1>0:
            ret+=f(n1-1,n2,n3)*n1
        if n2>0:
            ret+=f(n1+1,n2-1,n3)*n2
        if n3>0:
            ret+=f(n1,n2+1,n3-1)*n3
        ret+=n
        ret*=1/(n1+n2+n3)
        dp[n1][n2][n3]=ret
        return dp[n1][n2][n3]
    print(f(num1,num2,num3))
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**8)
    input=sys.stdin.readline
    n=int(input())
    a=list(map(int,input().split()))
    num1,num2,num3=0,0,0
    for i in range(n):
        if a[i]==1:
            num1+=1
        elif a[i]==2:
            num2+=1
        else:
            num3+=1
    dp=[[[-1 for k in range(n+1)] for i in range(n+1)] for j in range(n+1)]
    def f(n1,n2,n3):
        if dp[n1][n2][n3]>=0:
            return dp[n1][n2][n3]
        if n1==n2==n3==0:
            return 0
        ret=0
        if n1>0:
            ret+=f(n1-1,n2,n3)*n1
        if n2>0:
            ret+=f(n1+1,n2-1,n3)*n2
        if n3>0:
            ret+=f(n1,n2+1,n3-1)*n3
        ret+=n
        ret*=1/(n1+n2+n3)
        dp[n1][n2][n3]=ret
        return dp[n1][n2][n3]
    print(f(num1,num2,num3))
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**8)
    input=sys.stdin.readline
    n=int(input())
    a=list(map(int,input().split()))
    num1,num2,num3=0,0,0
    for i in range(n):
        if a[i]==1:
            num1+=1
        elif a[i]==2:
            num2+=1
        else:
            num3+=1
    dp=[[[-1 for k in range(n+1)] for i in range(n+1)] for j in range(n+1)]
    def f(n1,n2,n3):
        if dp[n1][n2][n3]>=0:
            return dp[n1][n2][n3]
        if n1==n2==n3==0:
            return 0
        ret=1/(1-(n-n1-n2-n3)/n)
        if n1>0:
            ret+=f(n1-1,n2,n3)*(n1/n)/(1-(n-n1-n2-n3)/n)
        if n2>0:
            ret+=f(n1+1,n2-1,n3)*(n2/n)/(1-(n-n1-n2-n3)/n)
        if n3>0:
            ret+=f(n1,n2+1,n3-1)*(n3/n)/(1-(n-n1-n2-n3)/n)
        dp[n1][n2][n3]=ret
        return dp[n1][n2][n3]
    print(f(num1,num2,num3))
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**8)
    input=sys.stdin.readline
    n=int(input())
    a=list(map(int,input().split()))
    num1,num2,num3=0,0,0
    for i in range(n):
        if a[i]==1:
            num1+=1
        elif a[i]==2:
            num2+=1
        else:
            num3+=1
    dp=[[[0 for k in range(n+1)] for i in range(n+1)] for j in range(n+1)]
    flag=[[[0 for k in range(n+1)] for i in range(n+1)] for j in range(n+1)]
    def f(n1,n2,n3):
        if flag[n1][n2][n3]:
            return dp[n1][n2][n3]
        if n1==n2==n3==0:
            return 0
        flag[n1][n2][n3]=1
        ret=1/(1-(n-n1-n2-n3)/n)
        if n1>0:
            ret+=f(n1-1,n2,n3)*(n1/n)/(1-(n-n1-n2-n3)/n)
        if n2>0:
            ret+=f(n1+1,n2-1,n3)*(n2/n)/(1-(n-n1-n2-n3)/n)
        if n3>0:
            ret+=f(n1,n2+1,n3-1)*(n3/n)/(1-(n-n1-n2-n3)/n)
        dp[n1][n2][n3]=ret
        return dp[n1][n2][n3]
    print(f(num1,num2,num3))
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**8)
    input=sys.stdin.readline
    n=int(input())
    a=list(map(int,input().split()))
    num1,num2,num3=0,0,0
    for i in range(n):
        if a[i]==1:
            num1+=1
        elif a[i]==2:
            num2+=1
        else:
            num3+=1
    dp=[[[0]*(n+1) for i in range(n+1)] for j in range(n+1)]
    flag=[[[0]*(n+1) for i in range(n+1)] for j in range(n+1)]
    def f(n1,n2,n3):
        if flag[n1][n2][n3]:
            return dp[n1][n2][n3]
        if n1==n2==n3==0:
            return 0
        flag[n1][n2][n3]=1
        ret=1/(1-(n-n1-n2-n3)/n)
        if n1>0:
            ret+=f(n1-1,n2,n3)*(n1/n)/(1-(n-n1-n2-n3)/n)
        if n2>0:
            ret+=f(n1+1,n2-1,n3)*(n2/n)/(1-(n-n1-n2-n3)/n)
        if n3>0:
            ret+=f(n1,n2+1,n3-1)*(n3/n)/(1-(n-n1-n2-n3)/n)
        dp[n1][n2][n3]=ret
        return dp[n1][n2][n3]
    print(f(num1,num2,num3))
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**8)
input=sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
num1,num2,num3=0,0,0
for i in range(n):
    if a[i]==1:
        num1+=1
    elif a[i]==2:
        num2+=1
    else:
        num3+=1
dp=[[[0]*(n+1) for i in range(n+1)] for j in range(n+1)]
flag=[[[0]*(n+1) for i in range(n+1)] for j in range(n+1)]
def f(n1,n2,n3):
    if flag[n1][n2][n3]:
        return dp[n1][n2][n3]
    if n1==n2==n3==0:
        return 0
    flag[n1][n2][n3]=1
    ret=1/(1-(n-n1-n2-n3)/n)
    if n1>0:
        ret+=f(n1-1,n2,n3)*(n1/n)/(1-(n-n1-n2-n3)/n)
    if n2>0:
        ret+=f(n1+1,n2-1,n3)*(n2/n)/(1-(n-n1-n2-n3)/n)
    if n3>0:
        ret+=f(n1,n2+1,n3-1)*(n3/n)/(1-(n-n1-n2-n3)/n)
    dp[n1][n2][n3]=ret
    return dp[n1][n2][n3]
print(f(num1,num2,num3))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**8)
n=int(input())
a=list(map(int,input().split()))
num1,num2,num3=0,0,0
for i in range(n):
    if a[i]==1:
        num1+=1
    elif a[i]==2:
        num2+=1
    else:
        num3+=1
dp=[[[0]*(n+1) for i in range(n+1)] for j in range(n+1)]
flag=[[[0]*(n+1) for i in range(n+1)] for j in range(n+1)]
def f(n1,n2,n3):
    if flag[n1][n2][n3]:
        return dp[n1][n2][n3]
    if n1==n2==n3==0:
        return 0
    flag[n1][n2][n3]=1
    ret=1/(1-(n-n1-n2-n3)/n)
    if n1>0:
        ret+=f(n1-1,n2,n3)*(n1/n)/(1-(n-n1-n2-n3)/n)
    if n2>0:
        ret+=f(n1+1,n2-1,n3)*(n2/n)/(1-(n-n1-n2-n3)/n)
    if n3>0:
        ret+=f(n1,n2+1,n3-1)*(n3/n)/(1-(n-n1-n2-n3)/n)
    dp[n1][n2][n3]=ret
    return dp[n1][n2][n3]
print(f(num1,num2,num3))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
num1,num2,num3=0,0,0
for i in range(n):
    if a[i]==1:
        num1+=1
    elif a[i]==2:
        num2+=1
    else:
        num3+=1
dp=[[[0]*(n+1) for i in range(n+1)] for j in range(n+1)]
flag=[[[0]*(n+1) for i in range(n+1)] for j in range(n+1)]
def f(n1,n2,n3):
    if flag[n1][n2][n3]:
        return dp[n1][n2][n3]
    if n1==n2==n3==0:
        return 0
    flag[n1][n2][n3]=1
    ret=1/(1-(n-n1-n2-n3)/n)
    if n1>0:
        ret+=f(n1-1,n2,n3)*(n1/n)/(1-(n-n1-n2-n3)/n)
    if n2>0:
        ret+=f(n1+1,n2-1,n3)*(n2/n)/(1-(n-n1-n2-n3)/n)
    if n3>0:
        ret+=f(n1,n2+1,n3-1)*(n3/n)/(1-(n-n1-n2-n3)/n)
    dp[n1][n2][n3]=ret
    return dp[n1][n2][n3]
print(f(num1,num2,num3))

Submission status: Time Limit Exceeded

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

# dp[i][j][k] = 寿司が3つの皿がi個、2つの皿がj個、1つの皿がk個からの操作回数の期待値
# dp[0][0][0] = 0で正しいので、ここから順に埋めていけばよい。

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

cnt = [0] * 4

for a in A:
  cnt[a] += 1

dp = [[[0 for k in range(N + 2)] for j in range(N + 2)] for i in range(N + 2)]

for i in range(N + 1):
  for j in range(N + 1):
    for k in range(N + 1):
      nonzero = i + j + k
      if nonzero == 0:
        continue
      dp[i][j][k] = N / nonzero
      if i - 1 >= 0:
        dp[i][j][k] += dp[i - 1][j + 1][k] * i / nonzero
      if j - 1 >= 0:
        dp[i][j][k] += dp[i][j - 1][k + 1] * j / nonzero
      if k - 1 >= 0:
        dp[i][j][k] += dp[i][j][k - 1] * k / nonzero

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def main():
    n=int(input())
    a=list(map(int,input().split()))
    # 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
    # x0=n-x1-x2-x3
    ca={1:a.count(1),2:a.count(2),3:a.count(3)}
    memo=[[[-1]*(n+2) for _ in range(n+2)] for _ in range(n+2)]
    memo[0][0][0]=0
    for x3 in range(ca[3]+1):
        for x2 in range(ca[2]+ca[3]+1):
            for x1 in range(ca[1]+ca[2]+ca[3]+1):
                if (x1,x2,x3)==(0,0,0):continue
                tmp=0
                if x1>0:tmp+=memo[x1-1][x2][x3]*x1
                if x2>0:tmp+=memo[x1+1][x2-1][x3]*x2
                if x3>0:tmp+=memo[x1][x2+1][x3-1]*x3
                tmp+=n
                tmp/=x1+x2+x3
                memo[x1][x2][x3]=tmp
    print(memo[ca[1]][ca[2]][ca[3]])
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
dp=[[0]*4 for _ in range(n+1)]
# 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
# x0=n-x1-x2-x3
from collections import Counter
a.append(1)
a.append(2)
a.append(3)
ca=dict(Counter(a))
import sys
sys.setrecursionlimit(10**7)
memo={}
memo[(0,0,0)]=0
def func(x1,x2,x3):
    if (x1,x2,x3) in memo:return memo[(x1,x2,x3)]
    ret=0
    s=x1+x2+x3
    if x1>0:ret+=((n/s)+func(x1-1,x2,x3))*(x1/s)
    if x2>0:ret+=((n/s)+func(x1+1,x2-1,x3))*(x2/s)
    if x3>0:ret+=((n/s)+func(x1,x2+1,x3-1))*(x3/s)
    #print(x1,x2,x3,ret)
    memo[(x1,x2,x3)]=ret
    return ret
print(func(ca[1]-1,ca[2]-1,ca[3]-1))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

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

one = a.count(1)
two = a.count(2)
three = a.count(3)

dp = [[[0.0] * (N + 1) for _ in range((N + 1))] for _ in range((N + 1))]

max_one = one + two + three
max_two = two + three
max_three = three

for k in range(max_three + 1):
    for j in range(max_two + 1 - k):
        for i in range(max_one + 1 - j - k):
            if i + j + k == 0:
                continue
            else:
                r = N

            if i > 0:
                r += dp[i - 1][j][k] * i
            if j > 0:
                r += dp[i + 1][j - 1][k] * j
            if k > 0:
                r += dp[i][j + 1][k - 1] * k

            dp[i][j][k] = r / (i + j + k)

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

Submission status: Time Limit Exceeded

Solution code:
#import
#import math
#import numpy as np
N = int(input())
#= input()
#= map(int, input().split())
a = list(map(int, input().split()))
#= [input(), input()]
#= [list(map(int, input().split())) for _ in range(N)]
#= {i:[] for i in range(N)}

one = a.count(1)
two = a.count(2)
three = a.count(3)

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

def dfs(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 += 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 += N

    res /= i + j + k

    dp[i][j][k] = res
    return res

res = dfs(one, two, three)

print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**8)
def calc(N,i,j,k):
  if dp[i][j][k]>=0:
    return dp[i][j][k]
  ans = 1
  if k-1>=0 and j+1<=N:
    ans += calc(N,i,j+1,k-1)*k/N
  if j-1>=0 and i+1<=N:
    ans += calc(N,i+1,j-1,k)*j/N
  if i-1>=0:
    ans += calc(N,i-1,j,k)*i/N
  if i+j+k>0:
    ans *= N/(i+j+k)
  dp[i][j][k] = ans
  return ans

N = int(input())
A = list(map(int, input().split()))
one = A.count(1)
two = A.count(2)
three = A.count(3)
dp = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]
dp[0][0][0] = 0
ans = calc(N,one,two,three)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

def calc(one,two,three):
  if dp[one][two][three]!=-1:
    return dp[one][two][three]
  zero=n-one-two-three
  ret=0
  if zero!=n:
    ret=1/(1-zero/n)
  if one!=0:
    ret+=(calc(one-1,two,three)*one/n)/(1-zero/n)
  if two!=0:
    ret+=(calc(one+1,two-1,three)*two/n)/(1-zero/n)
  if three!=0:
    ret+=(calc(one,two+1,three-1)*three/n)/(1-zero/n)
  dp[one][two][three]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
dp=[[[-1]*301 for _ in range(301)] for _ in range(301)]
cnt=[0,0,0,0]
for val in arr:
  cnt[val]+=1
calc(cnt[1],cnt[2],cnt[3])
print(dp[cnt[1]][cnt[2]][cnt[3]])

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys
sys.setrecursionlimit(10**6)

def calc(one,two,three):
  if dp[one][two][three]!=-1:
    return dp[one][two][three]
  zero=n-one-two-three
  if zero==n:
    return 0
  ret=n
  if one!=0:
    ret+=calc(one-1,two,three)*one
  if two!=0:
    ret+=calc(one+1,two-1,three)*two
  if three!=0:
    ret+=calc(one,two+1,three-1)*three
  ret/=n-zero
  dp[one][two][three]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
dp=[[[-1]*301 for _ in range(301)] for _ in range(301)]
cnt=collections.Counter(arr)
print(calc(cnt[1],cnt[2],cnt[3]))

Submission status: Time Limit Exceeded

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

def calc(one,two,three):
  if dp[one][two][three]!=-1:
    return dp[one][two][three]
  zero=n-one-two-three
  ret=0
  if zero!=n:
    ret=1/(1-zero/n)
  if one!=0:
    ret+=(calc(one-1,two,three)*one/n)/(1-zero/n)
  if two!=0:
    ret+=(calc(one+1,two-1,three)*two/n)/(1-zero/n)
  if three!=0:
    ret+=(calc(one,two+1,three-1)*three/n)/(1-zero/n)
  dp[one][two][three]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
dp=[[[-1]*301 for _ in range(301)] for _ in range(301)]
cnt=[0,0,0,0]
for val in arr:
  cnt[val]+=1
calc(cnt[1],cnt[2],cnt[3])
print(dp[cnt[1]][cnt[2]][cnt[3]])

Submission status: Time Limit Exceeded

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

def calc(one,two,three):
  zero=n-one-two-three
  if (one,two,three) in memo:
    return memo[(one,two,three)]
  ret=0
  if zero!=n:
    ret=1/(1-zero/n)
  if one!=0:
    ret+=(calc(one-1,two,three)*one/n)/(1-zero/n)
  if two!=0:
    ret+=(calc(one+1,two-1,three)*two/n)/(1-zero/n)
  if three!=0:
    ret+=(calc(one,two+1,three-1)*three/n)/(1-zero/n)
  memo[(one,two,three)]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
memo=collections.defaultdict(int)
cnt=[0,0,0,0]
for val in arr:
  cnt[val]+=1
calc(cnt[1],cnt[2],cnt[3])
print(memo[(cnt[1],cnt[2],cnt[3])])

Submission status: Time Limit Exceeded

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

def calc(zero,one,two,three):
  n=zero+one+two+three
  if (zero,one,two,three) in memo:
    return memo[(zero,one,two,three)]
  ret=0
  if zero!=n:
    ret=1/(1-zero/n)
  if one!=0:
    ret+=(calc(zero+1,one-1,two,three)*one/n)/(1-zero/n)
  if two!=0:
    ret+=(calc(zero,one+1,two-1,three)*two/n)/(1-zero/n)
  if three!=0:
    ret+=(calc(zero,one,two+1,three-1)*three/n)/(1-zero/n)
  memo[(zero,one,two,three)]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
memo=collections.defaultdict(int)
cnt=[0,0,0,0]
for val in arr:
  cnt[val]+=1
calc(cnt[0],cnt[1],cnt[2],cnt[3])
print(memo[(cnt[0],cnt[1],cnt[2],cnt[3])])

Submission status: Time Limit Exceeded

Solution code:
import collections

def calc(zero,one,two,three):
  n=zero+one+two+three
  if (zero,one,two,three) in memo:
    return memo[(zero,one,two,three)]
  ret=0
  if zero!=n:
    ret=1/(1-zero/n)
  if one!=0:
    ret+=(calc(zero+1,one-1,two,three)*one/n)/(1-zero/n)
  if two!=0:
    ret+=(calc(zero,one+1,two-1,three)*two/n)/(1-zero/n)
  if three!=0:
    ret+=(calc(zero,one,two+1,three-1)*three/n)/(1-zero/n)
  memo[(zero,one,two,three)]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
memo=collections.defaultdict(int)
cnt=[0,0,0,0]
for val in arr:
  cnt[val]+=1
calc(cnt[0],cnt[1],cnt[2],cnt[3])
print(memo[(cnt[0],cnt[1],cnt[2],cnt[3])])

Submission status: Time Limit Exceeded

Solution code:

def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    one = A.count(1)
    two = A.count(2)
    three = A.count(3)

    dp = [[[0.0] * (303) for _ in range(303)] for _ in range(303)]
    for c3 in range(N + 1):
        for c2 in range(N + 1):
            for c1 in range(N + 1):
                sm = c1 + c2 + c3
                if sm == 0:
                    continue
                dp[c1][c2][c3] = 1.0 * N / sm
                if c1:
                    dp[c1][c2][c3] += dp[c1 - 1][c2][c3] * c1 / sm
                if c2:
                    dp[c1][c2][c3] += dp[c1 + 1][c2 - 1][c3] * c2 / sm
                if c3:
                    dp[c1][c2][c3] += dp[c1][c2 + 1][c3 - 1] * c3 / sm
    print(dp[one][two][three])


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

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

def resolve():
    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 *= 1.0 / (i + j + k)
        dp[i][j][k] = res

        return dp[i][j][k]

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

    dp = [[[-1] * (N + 1) for _ in range(N + 1)] for _ in range(N + 1)]
    dp[0][0][0] = 0.0
    print(rec(one, two, three))


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

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

def resolve():
    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 *= 1.0 / (i + j + k)
        dp[i][j][k] = res

        return dp[i][j][k]

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

    dp = [[[-1] * (N + 1) for _ in range(N + 1)] for _ in range(N + 1)]
    print(rec(one, two, three))


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
n = int(input())
a = list(map(int, input().split()))
c1, c2, c3 = map(a.count, [1,2,3])
@lru_cache(None)
def f(c1, c2, c3):
    if c1 == c2 == c3 == 0:
        return 0
    c = c1 + c2 + c3
    rv = n / c
    if c1 > 0:
        rv += f(c1-1, c2, c3) * c1 / c
    if c2 > 0:
        rv += f(c1+1, c2-1, c3) * c2 / c
    if c3 > 0:
        rv += f(c1, c2+1, c3-1) * c3 / c
    return rv
print(f(c1, c2, c3))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
dp = [[[0 for _ in range(n+1)] for _ in range(n+1)] for _ in range(n+1)]
c1, c2, c3 = map(a.count, [1,2,3])
dp[c1][c2][c3] = 1
q = [(c1, c2, c3)]
ans = 0
for _ in range(c1 + c2*2 + c3*3):
    nxt = set()
    for c1, c2, c3 in q:
        c = c1 + c2 + c3
        p = dp[c1][c2][c3]
        ans += p * n / c
        if c1 > 0:
            dp[c1-1][c2][c3] += p * c1 / c
            nxt.add((c1-1,c2,c3))
        if c2 > 0:
            dp[c1+1][c2-1][c3] += p * c2 / c
            nxt.add((c1+1,c2-1,c3))
        if c3 > 0:
            dp[c1][c2+1][c3-1] += p * c3 / c
            nxt.add((c1,c2+1,c3-1))
    q = nxt
print(ans)