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:
# coding: utf-8
# Your code here!
n=int(input())
a = [int(i) for i in input().split()]
import sys
sys.setrecursionlimit(10**7)

ko = [0]*4
for i in a:
    ko[i] += 1

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

cc=ko[1]+ko[2]+ko[3]
ccc=ko[2]+ko[3]
for k in range(ko[3]+1):
    for j in range(ccc+1):
        for i in range(cc+1):
            t=i+j+k
#            print(i,j,k)
            if i != 0:
                dp[i][j][k] += (dp[i-1][j][k] + n/t)*i/t
            if j != 0 and i < n:
                dp[i][j][k] += (dp[i+1][j-1][k] + n/t)*j/t
            if k != 0 and j < n:
                dp[i][j][k] += (dp[i][j+1][k-1] + n/t)*k/t
#print(dp)
print(dp[ko[1]][ko[2]][ko[3]])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
a = [int(i) for i in input().split()]
import sys
sys.setrecursionlimit(10**7)

ko = [0]*4
for i in a:
    ko[i] += 1

memo = [[[-1.0]*(n+1) for i in range(n+1)] for j in range(n+1)]
memo[0][0][0] = 0
"""
#print(dp,ko)
for i in range(n+1):
    for j in range(n+1-i):
        for k in range(ko[3]+1):
            t=i+j+k
#            print(i,j,k)
            if i != 0:
                dp[i][j][k] += (dp[i-1][j][k] + n/t)*i/t
            if j != 0:
                dp[i][j][k] += (dp[i+1][j-1][k] + n/t)*j/t
            if k != 0:
                dp[i][j][k] += (dp[i][j+1][k-1] + n/t)*k/t
        print(dp)
"""
def dfs(i,j,k):
    t=i+j+k
    ans = 0
    if memo[i][j][k] >= -0.5:
        return memo[i][j][k]
    if i != 0:
        ans += (dfs(i-1,j,k) + n/t)*i/t
    if j != 0:
        ans += (dfs(i+1,j-1,k) + n/t)*j/t
    if k != 0:
        ans += (dfs(i,j+1,k-1) + n/t)*k/t
    memo[i][j][k] = ans
    return ans
    
print(dfs(ko[1],ko[2],ko[3]))
#print(memo)

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache
input = sys.stdin.readline
inf = float('inf')
mod = 10**9+7


def INT_(n): return int(n)-1


def MI(): return map(int, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LIN(n: int): return [input() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')


def main():
    N = I()
    A = LI_()
    cnt = [0, 0, 0]
    for a in A:
        cnt[a] += 1

    def recursion(i, j, k, dp):
        if dp[i][j][k] != -1:
            return dp[i][j][k]
        if (i, j, k) == (0, 0, 0):
            dp[i][j][j] = 0
            return 0
        else:
            dp[i][j][k] = 0
            if i != 0:
                #print(i, j, j, recursion(i-1, j, k))
                if dp[i-1][j][k] == -1:
                    dp[i][j][k] += recursion(i - 1, j, k, dp) * (i / N)
                else:
                    dp[i][j][k] += dp[i-1][j][k] * (i / N)
            if j != 0:
                if dp[i+1][j-1][k] == -1:
                    dp[i][j][k] += recursion(i + 1, j - 1, k, dp) * (j / N)
                else:
                    dp[i][j][k] += dp[i+1][j-1][k] * (j / N)
            if k != 0:
                if dp[i][j+1][k-1] == -1:
                    dp[i][j][k] += recursion(i, j + 1, k - 1, dp) * (k / N)
                else:
                    dp[i][j][k] += dp[i][j+1][k-1] * (k / N)

            dp[i][j][k] += 1
            dp[i][j][k] /= (i+j+k)/N
            return dp[i][j][k]
    # dp[i][j][k]:=1貫の皿がi,２貫がj,3貫がk皿有るときのなくなるまでの期待値
    dp = [[[-1]*(N+1) for _ in range(N+1)]for _ in range(N+1)]
    ans = recursion(cnt[0], cnt[1], cnt[2], dp)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache
input = sys.stdin.readline
inf = float('inf')
mod = 10**9+7


def INT_(n): return int(n)-1


def MI(): return map(int, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LIN(n: int): return [input() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')


def main():
    N = I()
    A = LI_()
    cnt = [0, 0, 0]
    for a in A:
        cnt[a] += 1

    def recursion(i, j, k, dp):
        if dp[i][j][k] != -1:
            return dp[i][j][k]
        if (i, j, k) == (0, 0, 0):
            dp[i][j][j] = 0
            return 0
        else:
            dp[i][j][k] = 0
            if i != 0:
                #print(i, j, j, recursion(i-1, j, k))
                if dp[i][j][k] != -1:
                    dp[i][j][k] += recursion(i-1, j, k, dp) * (i / N)
            if j != 0:
                if dp[i][j][k] != -1:
                    dp[i][j][k] += recursion(i+1, j-1, k, dp) * (j / N)
            if k != 0:
                if dp[i][j][k] != -1:
                    dp[i][j][k] += recursion(i, j+1, k-1, dp) * (k / N)
            dp[i][j][k] += 1
            dp[i][j][k] /= (i+j+k)/N
            return dp[i][j][k]
    # dp[i][j][k]:=1貫の皿がi,２貫がj,3貫がk皿有るときのなくなるまでの期待値
    dp = [[[-1]*(N+1) for _ in range(N+1)]for _ in range(N+1)]
    ans = recursion(cnt[0], cnt[1], cnt[2], dp)
    print(ans)


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 _ in range(n+1)] for _ in range(n+1)]
dp[0][0][0] = 0

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

cnt1 = a.count(1)
cnt2 = a.count(2)
cnt3 = a.count(3)

dfs(cnt3, cnt2, cnt1, n)
print(dp[cnt3][cnt2][cnt1])

Submission status: Time Limit Exceeded

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

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

def dfs(i, j, k, n):
    if dp[i][j][k] != -1:
        return dp[i][j][k]
    if k == 0:
        res1 = 0
    else:
        res1 = (k/n) * dfs(i, j, k-1, n)
    if j == 0:
        res2 = 0
    else:
        res2 = (j/n) * dfs(i, j-1, k+1, n)
    if i == 0:
        res3 = 0
    else:
        res3 = (i/n) * dfs(i-1, j+1, k, n)
    dp[i][j][k] = (n/(i+k+j)) * (1 + res1 + res2 + res3)
    return dp[i][j][k]

cnt1 = a.count(1)
cnt2 = a.count(2)
cnt3 = a.count(3)

dfs(cnt3, cnt2, cnt1, n)
print(dp[cnt3][cnt2][cnt1])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

from collections import defaultdict
import numpy as np

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

dp = np.zeros((N+1, N+1, N+1)) - 1
dp[0][0][0] = 0
def rec(i, j, k):
    if dp[i][j][k] != -1:
        return dp[i][j][k]
        
    temp = N / (i + j + k)
    if i > 0:
        temp += i / (i + j + k) * rec(i - 1, j, k)
    if j > 0:
        temp += j / (i + j + k) * rec(i + 1, j - 1, k)
    if k > 0:
        temp += k / (i + j + k) * rec(i, j + 1, k - 1)
        
    dp[i][j][k] = temp
    
    return temp
    
count_1 = sum([x == 1 for x in A])
count_2 = sum([x == 2 for x in A])
count_3 = sum([x == 3 for x in A])

print(rec(count_1, count_2, count_3))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

from collections import defaultdict

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

dp = defaultdict(lambda: defaultdict(lambda:defaultdict(lambda: -1)))
dp[0][0][0] = 0
def rec(i, j, k):
    if dp[i][j][k] != -1:
        return dp[i][j][k]
        
    temp = N / (i + j + k)
    if i > 0:
        temp += i / (i + j + k) * rec(i - 1, j, k)
    if j > 0:
        temp += j / (i + j + k) * rec(i + 1, j - 1, k)
    if k > 0:
        temp += k / (i + j + k) * rec(i, j + 1, k - 1)
        
    dp[i][j][k] = temp
    
    return temp
    
count_1 = sum([x == 1 for x in A])
count_2 = sum([x == 2 for x in A])
count_3 = sum([x == 3 for x in A])

print(rec(count_1, count_2, count_3))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

def dump(memo, M):
    for i in range(M):
        for j in range(M):
            for k in range(M):
                if abs(memo[i][j][k] + 1) < 10 ** -9:
                    continue
                print("({0}, {1}, {2}, {3:5.2f})".format(i, j, k, memo[i][j][k]))
def main():
    import sys
    raw = [sys.stdin.readline(),
           sys.stdin.readline()]
    #raw = ["3", "1 1 1"]
    N = int(raw[0].split()[0])
    xs = list(map(int, raw[1].split()))

    M = N + 1
    memo = [[[-1] * M for _ in range(M)] for _ in range(M)]
    def rec(i, j, k):
        if memo[i][j][k] >= 0:
            return memo[i][j][k]

        if i == 0 and j == 0 and k == 0:
            return 0.0

        res = 0.0
        denom = i + j + k
        if i > 0:
            res += 1.0 * rec(i-1, j, k) * i / denom
        if j > 0:
            res += 1.0 * rec(i+1, j-1, k) * j / denom
        if k > 0:
            res += 1.0 * rec(i, j+1, k-1) * k / denom
        res += 1.0 * N / denom
        memo[i][j][k] = res
        return res
    print(rec(xs.count(1), xs.count(2), xs.count(3)))
    #dump(memo, M)

main()

Submission status: Time Limit Exceeded

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

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

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


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

    for k in range(three + 1):
        for j in range(two + three + 1 - k):
            for i in range(one + two + three + 1 -k -j):
                if i == 0 and j == 0 and k == 0:
                    continue
                ret = N
                if i > 0:
                    ret += dp[i - 1][j][k] * i
                if j > 0:
                    ret += dp[i + 1][j - 1][k] * j 
                if k > 0:
                    ret += dp[i][j + 1][k -1] * k

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

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

main()

Submission status: Time Limit Exceeded

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

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

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


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

    for k in range(three + 1):
        for j in range(two + three + 1 - k):
            for i in range(one + two + three + 1 -k -j):
                if i == 0 and j == 0 and k == 0:
                    continue
                ret = 0
                if i > 0:
                    ret += dp[i - 1][j][k] * i
                if j > 0:
                    ret += dp[i + 1][j - 1][k] * j 
                if k > 0:
                    ret += dp[i][j + 1][k -1] * k

                ret += N
                ret *= 1.0 / (i + j + k)
                dp[i][j][k] = ret

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

main()

Submission status: Time Limit Exceeded

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

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

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


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

for k in range(three + 1):
    for j in range(two + three + 1 - k):
        for i in range(one + two + three + 1 -k -j):
            if i == 0 and j == 0 and k == 0:
                continue
            ret = 0
            if i > 0:
                ret += dp[i - 1][j][k] * i
            if j > 0:
                ret += dp[i + 1][j - 1][k] * j 
            if k > 0:
                ret += dp[i][j + 1][k -1] * k

            ret += N
            ret *= 1.0 / (i + j + k)
            dp[i][j][k] = ret

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

Submission status: Time Limit Exceeded

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

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


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

for k in range(three + 1):
    for j in range(two + three + 1 - k):
        for i in range(one + two + three + 1 -k -j):
            if i == 0 and j == 0 and k == 0:
                continue
            ret = 0
            if i > 0:
                ret += dp[i - 1][j][k] * i
            if j > 0:
                ret += dp[i + 1][j - 1][k] * j 
            if k > 0:
                ret += dp[i][j + 1][k -1] * k

            ret += N
            ret *= 1.0 / (i + j + k)
            dp[i][j][k] = ret

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(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 calc(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
    ret = 0
    if i > 0:
        ret += calc(i - 1, j, k) * i
    if j > 0:
        ret += calc(i + 1, j - 1, k) * j 
    if k > 0:
        ret += calc(i, j + 1, k -1) * k

    ret += N
    ret *= 1.0 / (i + j + k)
    dp[i][j][k] = ret
    return ret


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

ret = calc(one,two,three)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache

N = int(input())

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

dish = N*4
dp = [[[-1 for k in range(dish )]for j in range(dish)]for i in range(dish)]

@lru_cache(maxsize=N*4)
def calc(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
    ret = 0
    if i > 0:
        ret += calc(i - 1, j, k) * i
    if j > 0:
        ret += calc(i + 1, j - 1, k) * j 
    if k > 0:
        ret += calc(i, j + 1, k -1) * k

    ret += N
    ret *= 1.0 / (i + j + k)
    dp[i][j][k] = ret
    return ret


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

ret = calc(one,two,three)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

def tami(i,j,k):
  if (dp[i][j][k] >= 0):
    return dp[i][j][k]
  if (i == j == k == 0):
    return 0
  else:
    res = 0
    if (i > 0):
      res += tami(i-1, j, k) * i
    if (j > 0):
      res += tami(i+1, j-1, k) * j
    if (k > 0):
      res += tami(i, j+1, k-1) * k
    res += N
    res *= (1 / (i + j + k))
    dp[i][j][k] = res
    return res
  
N = int(input())
a = list(map(float,input().split()))

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

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

print(tami(one,two,three))

Submission status: Time Limit Exceeded

Solution code:
def tami(i,j,k):
  if (dp[i][j][k] >= 0):
    return dp[i][j][k]
  if (i == j == k == 0):
    return 0.0
  else:
    res = 0.0
    if (i > 0):
      res += tami(i-1, j, k) * i
    if (j > 0):
      res += tami(i+1, j-1, k) * j
    if (k > 0):
      res += tami(i, j+1, k-1) * k
    res += N
    res *= (1.0 / (i + j + k))
    dp[i][j][k] = res
    return res
  
N = int(input())
a = list(map(float,input().split()))

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

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

print(tami(one,two,three))

Submission status: Time Limit Exceeded

Solution code:
def ev(d0, d1, d2, d3):
    if d0 == n:
        return 0
    tp = (d1, d2, d3)
    if tp in memo:
        return memo[tp]
    re = n
    if d1 > 0:
        re += ev(d0 + 1, d1 - 1, d2, d3) * d1
    if d2 > 0:
        re += ev(d0, d1 + 1, d2 - 1, d3) * d2
    if d3 > 0:
        re += ev(d0, d1, d2 + 1, d3 - 1) * d3
    re /=n - d0
    memo[tp] = re
    return re


memo = {}
n = int(input())
a = list(map(int, input().split()))
ds = [0] + [a.count(i) for i in range(1, 4)]
print(ev(0, ds[1], ds[2], ds[3]))

Submission status: Time Limit Exceeded

Solution code:
def ev(d0, d1, d2, d3):
    if d0 == n:
        return 0
    if memo[d1][d2][d3] != -1:
        return memo[d1][d2][d3]
    re = 1
    if d1 > 0:
        re += ev(d0 + 1, d1 - 1, d2, d3) * d1 / n
    if d2 > 0:
        re += ev(d0, d1 + 1, d2 - 1, d3) * d2 / n
    if d3 > 0:
        re += ev(d0, d1, d2 + 1, d3 - 1) * d3 / n
    re *= n / (n - d0)
    memo[d1][d2][d3] = re
    return re


n = int(input())
a = list(map(int, input().split()))
ds = [0] + [a.count(i) for i in range(1, 4)]
memo = [[[-1] * (ds[3] + 1) for i in range(ds[2] + ds[3] + 1)] for ii in range(n + 1)]
print(ev(0, ds[1], ds[2], ds[3]))

Submission status: Time Limit Exceeded

Solution code:
def ev(d0, d1, d2, d3):
    if d0 == n:
        return 0
    tp = (d1, d2, d3)
    if tp in memo:
        return memo[tp]
    re = 1
    if d1 > 0:
        re += ev(d0 + 1, d1 - 1, d2, d3) * d1 / n
    if d2 > 0:
        re += ev(d0, d1 + 1, d2 - 1, d3) * d2 / n
    if d3 > 0:
        re += ev(d0, d1, d2 + 1, d3 - 1) * d3 / n
    re *=n / (n - d0)
    memo[tp] = re
    return re


memo = {}
n = int(input())
a = list(map(int, input().split()))
ds = [0, 0, 0, 0]
for i in range(1, 4):
    ic = a.count(i)
    ds[i] = ic
print(ev(0, ds[1], ds[2], ds[3]))

Submission status: Time Limit Exceeded

Solution code:
def ev(d0, d1, d2, d3):
    if d0 == n:
        return 0
    tp = (d1, d2, d3)
    if tp in memo:
        return memo[tp]
    re = [1]
    if d1 > 0:
        re += [ev(d0 + 1, d1 - 1, d2, d3) * d1 / n]
    if d2 > 0:
        re += [ev(d0, d1 + 1, d2 - 1, d3) * d2 / n]
    if d3 > 0:
        re += [ev(d0, d1, d2 + 1, d3 - 1) * d3 / n]
    re = sum(re) * n / (n - d0)
    memo[tp] = re
    return re


memo = {}
n = int(input())
a = list(map(int, input().split()))
ds = [0, 0, 0, 0]
for i in range(1, 4):
    ic = a.count(i)
    ds[i] = ic
print(ev(0, ds[1], ds[2], ds[3]))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
x,y,z=0,0,0
for i in input().split():
    if i=="1":
        x+=1
    elif i=="2":
        y+=1
    else:
        z+=1
dp=[[[0 for k in range(N+1)] for j in range(N+1)] for i in range(N+1)]
fl=[[[0 for k in range(N+1)] for j in range(N+1)] for i in range(N+1)]
def f(a,b,c):
    if fl[a][b][c]==1:
        return dp[a][b][c]
    fl[a][b][c]=1
    if a+b+c==0:
        dp[a][b][c]=0
        return 0
    fans=1/(1-(N-a-b-c)/N)
    if a>0:
        fans+=f(a-1,b,c)*a/N/(1-(N-a-b-c)/N)
    if b>0:
        fans+=f(a+1,b-1,c)*b/N/(1-(N-a-b-c)/N)
    if c>0:
        fans+=f(a,b+1,c-1)*c/N/(1-(N-a-b-c)/N)
    dp[a][b][c]=fans
    return fans
print(f(x,y,z))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
def solve():
  n = int(input())
  a = list(int(i) for i in input().split())
  one,two,three  = a.count(1),a.count(2),a.count(3)
  
  
  dp = [[[0,0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
  
  for k in range(three +1):
    for j in range(two +three +1 -k):
      for i in range(one + two + three + 1 - k - j):
        if i == 0 and j == 0 and k == 0:
          continue
        tmp = n*1.0
        if i > 0:
          tmp += i*dp[i-1][j][k]
        if j > 0:
          tmp += j*dp[i+1][j-1][k]
        if k > 0:
          tmp += k*dp[i][j+1][k-1]
        dp[i][j][k] = tmp/(i+j+k)
  print(dp[one][two][three])
solve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
cnt = [0] * 4
for i in range(N):
    cnt[a[i]] += 1
q = sum(cnt)
dp = [[[-1] * (q + 1) for i in range(q + 1)] for j in range(q + 1)]


def dfs(x, y, z):
    if dp[x][y][z] != -1:
        return dp[x][y][z]
    if x == 0 and y == 0 and z == 0:
        return 0

    s = x + y + z
    d = N / s
    if x > 0:
        d += (x / s) * dfs(x - 1, y, z)
    if y > 0:
        d += (y / s) * dfs(x + 1, y - 1, z)
    if z > 0:
        d += (z / s) * dfs(x, y + 1, z - 1)

    dp[x][y][z] = d
    return d


print(dfs(cnt[1], cnt[2], cnt[3]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
cnt = [0] * 4
for i in range(N):
    cnt[a[i]] += 1

dp = {}


def dfs(x, y, z):
    if (x, y, z) in dp:
        return dp[(x, y, z)]
    if x == 0 and y == 0 and z == 0:
        return 0

    s = x + y + z
    d = N / s
    if x > 0:
        d += (x / s) * dfs(x - 1, y, z)
    if y > 0:
        d += (y / s) * dfs(x + 1, y - 1, z)
    if z > 0:
        d += (z / s) * dfs(x, y + 1, z - 1)

    dp[(x, y, z)] = d
    return d


print(dfs(cnt[1], cnt[2], cnt[3]))

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    lst_a = list(map(int, input().split()))
    one, two, three = lst_a.count(1), lst_a.count(2), lst_a.count(3)
    # dp[i][j][k] := 残り1個の皿がi枚、2個の皿がj枚、3個の皿がk枚の状態から、
    # 寿司をすべてなくすのに必要な操作回数の期待値
    # dpは-1で初期化
    dp = [[[-1.0] * (N+1) for _ in range(N+1)] for _ 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 *= 1.0 / (i + j + k)
        dp[i][j][k] = res
        return dp[i][j][k]
    print(rec(one, two, three))

main()

Submission status: Time Limit Exceeded

Solution code:
import sys

# input処理を高速化する
input = sys.stdin.readline

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

    # dp[i][j][k] := 残り1個の皿がi枚、2個の皿がj枚、3個の皿がk枚の状態から、
    # 寿司をすべてなくすのに必要な操作回数の期待値
    dp = [[[0] * (N+1) for _ in range(N+1)] for _ in range(N+1)]

    for k in range(three + 1):
        for j in range(two + three + 1  - k):
            for i in range(one + two + three + 1 - k -j):
                if i == 0 and j == 0 and k == 0:
                    continue
                tmp = N * 1.0
                if i > 0:
                    tmp += i * dp[i -1][j][k]
                if j > 0:
                    tmp += j * dp[i + 1][j - 1][k]
                if k > 0:
                    tmp += k * dp[i][j + 1][k -1]
                dp[i][j][k] = tmp / (i + j + k)

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


main()

Submission status: Time Limit Exceeded

Solution code:
import sys

# input処理を高速化する
input = sys.stdin.readline

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

    # dp[i][j][k] := 残り1個の皿がi枚、2個の皿がj枚、3個の皿がk枚の状態から、
    # 寿司をすべてなくすのに必要な操作回数の期待値
    dp = [[[0] * (N+1) for _ in range(N+1)] for _ in range(N+1)]

    for k in range(three + 1):
        for j in range(two + three + 1  - k):
            for i in range(one + two + three + 1 - k -j):
                if i == 0 and j == 0 and k == 0:
                    continue
                tmp = N
                if i > 0:
                    tmp += i * dp[i -1][j][k]
                if j > 0:
                    tmp += j * dp[i + 1][j - 1][k]
                if k > 0:
                    tmp += k * dp[i][j + 1][k -1]
                dp[i][j][k] = tmp / (i + j + k)

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


main()

Submission status: Time Limit Exceeded

Solution code:
import sys

# input処理を高速化する
input = sys.stdin.readline
# 許容する再帰処理の回数を変更
sys.setrecursionlimit(300*300*300+10)

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

    # dp[i][j][k] := 残り1個の皿がi枚、2個の皿がj枚、3個の皿がk枚の状態から、
    # 寿司をすべてなくすのに必要な操作回数の期待値
    # dpは-1で初期化
    dp = [[[-1] * (N+1) for _ in range(N+1)] for _ 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 *= 1.0 / (i + j + k)

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

    print(rec(one, two, three))


main()

Submission status: Time Limit Exceeded

Solution code:
import sys

# input処理を高速化する
input = sys.stdin.readline
# 許容する再帰処理の回数を変更
sys.setrecursionlimit(300*300*300+10)

def main():
    N = int(input())
    lst_a = list(map(int, input().split()))

    # dp[i][j][k] := 残り1個の皿がi枚、2個の皿がj枚、3個の皿がk枚の状態から、
    # 寿司をすべてなくすのに必要な操作回数の期待値
    # dpは-1で初期化
    dp = [[[-1] * (N+1) for _ in range(N+1)] for _ 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 *= 1.0 / (i + j + k)

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

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

    print(rec(one, two, three))

main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3

import sys

MEMO = dict()

def counter(N, i, j, k):
    global MEMO

    if (i, j, k) in MEMO:
        return MEMO[(i, j, k)]
    else:
        count = 0
        if i:
            count += i * counter(N, i-1, j, k)

        if j:
            count += j * counter(N, i+1, j-1, k)

        if k:
            count += k * counter(N, i, j+1, k-1)

        res = (count + N) / (i + j + k)
        MEMO[(i, j, k)] = res
        return res

def main():
    global MEMO

    sys.setrecursionlimit(10**9)

    N = int(input())
    sushis = tuple(map(int, input().split()))

    one = sushis.count(1)
    two = sushis.count(2)
    three = N - one - two
    MEMO[(0, 0, 0)] = 0

    print(counter(N, one, two, three))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3

import sys

MEMO = dict()

def counter(N, i, j, k):
    global MEMO

    if (i, j, k) in MEMO:
        return MEMO[(i, j, k)]
    else:
        count = 0
        if i > 0:
            count += i * counter(N, i-1, j, k)

        if j > 0:
            count += j * counter(N, i+1, j-1, k)

        if k > 0:
            count += k * counter(N, i, j+1, k-1)

        res = (count + N) / (i + j + k)
        MEMO[(i, j, k)] = res
        return res

def main():
    global MEMO

    sys.setrecursionlimit(10**9)

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

    one = sushis.count(1)
    two = sushis.count(2)
    three = N - one - two
    MEMO[(0, 0, 0)] = 0

    print(counter(N, one, two, three))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

c = Counter(a)

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

for z in range(c[3] + 1):
    for y in range(c[2]+1 + c[3] - z):
        for x in range(n+1 - z - y):
            if x + y + z == 0:
                continue
            ret = 0.0
            if x > 0:
                ret += dp[x-1][y][z] * x
            if y > 0:
                ret += dp[x+1][y-1][z] * y
            if z > 0:
                ret += dp[x][y+1][z-1] * z
            dp[x][y][z] = (ret + n) / (x + y + z)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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)]

def sushi(x, y, z):
    if dp[x][y][z] >= 0:
        return dp[x][y][z]
    if x == 0 and y == 0 and z == 0:
        return 0.0

    ret = 0.0
    if x > 0:
        ret += sushi(x-1, y, z) * x
    if y > 0:
        ret += sushi(x+1, y-1, z) * y
    if z > 0:
        ret += sushi(x, y+1, z-1) * z
    dp[x][y][z] = (ret + n) / (x + y + z)
    return dp[x][y][z]

print(sushi(c[1], c[2], c[3]))

Submission status: Time Limit Exceeded

Solution code:
#import sys
#input=sys.stdin.readline
from collections import Counter
n=int(input())
A=tuple(input().split())
C=Counter(A)
c1,c2,c3=[C[s] for s in ('1','2','3')]
DP=[[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
for i in range(c3+1):
    for j in range(c3+c2-i+1):
        for k in range(n-i-j+1):
            if i==j==k==0:
                continue
            tmp=n
            if i>0:
                tmp+=DP[k][j+1][i-1]*i
            if j>0:
                tmp+=DP[k+1][j-1][i]*j
            if k>0:
                tmp+=DP[k-1][j][i]*k
            DP[k][j][i]=tmp/(i+j+k)
print(DP[c1][c2][c3])

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
from collections import Counter
n=int(input())
A=tuple(input().split())
C=Counter(A)
c1,c2,c3=[C[s] for s in ('1','2','3')]
DP=[[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)] 
DP[0][0][0]=0
for i in range(c3+1):
    for j in range(c3+c2-i+1):
        for k in range(n-i-j+1):
            if i==j==k==0:
                continue
            tmp=n
            if i>0:
                tmp+=DP[k][j+1][i-1]*i
            if j>0:
                tmp+=DP[k+1][j-1][i]*j
            if k>0:
                tmp+=DP[k-1][j][i]*k
            DP[k][j][i]=tmp/(i+j+k)
print(DP[c1][c2][c3])