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: Accepted

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

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

input:
3
1 1 1

output:
    5.5

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

期待値DP。
同じ寿司の量の皿の数にさえ注目すればいいところが鍵。
※PythonもPyPyも遅くて通らない...
再帰をfor文に書き直せばPyPyで通るはず
'''

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


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

for k in range(three+1):
    for j in range(two+three+1):
        for i in range(one+two+three+1):
            if i + j + k == 0 or i + j + k > n:
                continue
            res = 0.0
            if i != 0:
                res += dp[i - 1][j][k] * i
            if j != 0:
                res += dp[i + 1][j - 1][k] * j
            if k != 0:
                res += dp[i][j + 1][k - 1] * k
            res += n
            res *= 1.0 / (i + j + k)
            dp[i][j][k] = res
print(dp[one][two][three])

# 再帰。こっちの方が簡単だがPyPyでは通らない。
# @lru_cache(maxsize=None)
# def rec(i, j, k):
#     if i == 0 and j == 0 and k == 0:
#         return 0.0
    
#     res = 0.0
#     if i != 0:
#         res += rec(i - 1, j, k) * i
#     if j != 0:
#         res += rec(i + 1, j - 1, k) * j
#     if k != 0:
#         res += rec(i, j + 1, k - 1) * k
#     res += n
#     res *= 1.0 / (i + j + k)
#     return res

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


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

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

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

Submission status: Accepted

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

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

input:
3
1 1 1

output:
    5.5

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

期待値DP。
同じ寿司の量の皿の数にさえ注目すればいいところが鍵。
※PythonもPyPyも遅くて通らない...
再帰をfor文に書き直せばPyPyで通るはず
'''

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


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

dp[0][0][0] = 1.0
for k in range(three+1):
    for j in range(two+three+1):
        for i in range(one+two+three+1):
            if i + j + k == 0 or i + j + k > n:
                continue
            res = 0.0
            if i != 0:
                res += dp[i - 1][j][k] * i
            if j != 0:
                res += dp[i + 1][j - 1][k] * j
            if k != 0:
                res += dp[i][j + 1][k - 1] * k
            res += n
            res *= 1.0 / (i + j + k)
            dp[i][j][k] = res
print(dp[one][two][three]-1.0)
# print(dp[one - 1][two - 1][three - 1])
# print(dp)
# @lru_cache(maxsize=None)
# def rec(i, j, k):
#     if i == 0 and j == 0 and k == 0:
#         return 0.0
    
#     res = 0.0
#     if i != 0:
#         res += rec(i - 1, j, k) * i
#     if j != 0:
#         res += rec(i + 1, j - 1, k) * j
#     if k != 0:
#         res += rec(i, j + 1, k - 1) * k
#     res += n
#     res *= 1.0 / (i + j + k)
#     return res

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


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

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

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

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

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

Solution code:
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def NI(): return int(sys.stdin.readline())


N = NI()
a = [0, 0, 0]
for x in LI():
    a[x-1] += 1
dp = [[[0.0] * (N+1) for _ in range(N+1)] for _ in range(N+1)]

for k in range(a[2]+1):
    for j in range(a[1]+a[2]+1):
        for i in range(N+1):
            if not (0 < i+j+k <= N):
                continue
            x = 0.0
            if i > 0:
                x += dp[i-1][j][k] * (i/N)
            if j > 0:
                x += dp[i+1][j-1][k] * (j/N)
            if k > 0:
                x += dp[i][j+1][k-1] * (k/N)
            x += 1.0
            x *= N / (i+j+k)
            dp[i][j][k] = x
print(dp[a[0]][a[1]][a[2]])

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())

N = NI()
a = [0,0,0]
for x in LI():
    a[x-1] += 1
dp = [[[0.0] * (N+1) for _ in range(N+1)] for _ in range(N+1)]

for k in range(a[2]+1):
    for j in range(a[1]+a[2]+1):
        for i in range(N+1):
            if not (0 < i+j+k <= N): continue
            x = 0.0
            if i > 0: x += dp[i-1][j][k] * (i/N)
            if j > 0: x += dp[i+1][j-1][k] * (j/N)
            if k > 0: x += dp[i][j+1][k-1] * (k/N)
            x += 1.0
            x *= N / (i+j+k)
            dp[i][j][k] = x
print(dp[a[0]][a[1]][a[2]])

Submission status: Accepted

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

    # (i, j, k) -> (i - 1, j, k), (i + 1, j - 1, k), (i, j + 1, k - 1)
    # s = i + j + k の小さい方から遷移
    dp = [[[None] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]
    dp[0][0][0] = 0
    for s in range(1, n + 1):
        for k in range(n + 1):
            for j in range(n + 1):
                i = s - j - k
                if i < 0:
                    break
                res = n
                if i: res += i * dp[i - 1][j][k]
                if j: res += j * dp[i + 1][j - 1][k]
                if k: res += k * dp[i][j + 1][k - 1]

                res /= s
                dp[i][j][k] = res

    i, j, k = counter
    print(dp[i][j][k])
resolve()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
d={1:0,2:0,3:0}
for a in A:
    d[a]+=1
dp=[[[0]*(N+1) for j in range(d[2]+d[3]+1)] for i in range(d[3]+1)]
for i in range(d[3]+1):
    for j in range(d[2]+d[3]-i+1):
        for k in range(N-i-j+1):
            if i+j+k==0:continue
            dp[i][j][k]=N
            if i>0:
                dp[i][j][k]+=dp[i-1][j+1][k]*i
            if j>0:
                dp[i][j][k]+=dp[i][j-1][k+1]*j
            if k>0:
                dp[i][j][k]+=dp[i][j][k-1]*k
            dp[i][j][k]/=i+j+k
print(dp[d[3]][d[2]][d[1]])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
ns = list(map(int, input().split()))
one = ns.count(1)
two = ns.count(2)
three = n-one-two
dp = [[[0 for _ in range(n+1)] for _ in range(two+three+1)] for _ in range(three+1)]
for k in range(three+1):
    for j in range(two+three-k+1):
        for i in range(n-j-k+1):
            v = k+i+j
            if v==0:
                continue
            res = n
            if i:
                res += i * dp[k][j][i-1]
            if j:
                res += j * dp[k][j-1][i+1]
            if k:
                res += k * dp[k-1][j+1][i]
            dp[k][j][i] = res / v
print(dp[three][two][one])

Submission status: Accepted

Solution code:
n=int(input())
a=tuple(map(int,input().split()))
c1 = a.count(1)
c2 = a.count(2)
c3 = a.count(3)

dp = [[[0.0]*(n+1) for i in range(n+1)] for j in range(n+1)] 
dp[0][0][0] = 0
for i in range(c3+1):
    for j in range(c2+c3-i+1):
        for k in range(n-i-j+1):
            if i == j == k == 0:
                continue
            tmp = n
            if k != 0:
                tmp += k*dp[k-1][j][i]
            if j != 0:
                tmp += j*dp[k+1][j-1][i]
            if i != 0:
                tmp += i*dp[k][j+1][i-1]
            dp[k][j][i] = tmp/(i+j+k)

print(dp[c1][c2][c3])