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: Runtime Error

Solution code:
import numpy as np


def main():
    N = int(input())
    a = map(int, input().split())
    single = double = triple = 0
    for sushi in a:
        if sushi == 1:
            single += 1
        elif sushi == 2:
            double += 1
        elif sushi == 3:
            triple += 1
    dp = np.zeros((single + 3, double + 3, triple + 3))
    for k in range(triple+2):
        for j in range(double+2):
            for i in range(single + 2):
                if i == 0 and j == 0 and k == 0:
                    continue
                print(i, j, k)
                first = second = third = 0
                if i-1 >= 0 and j >= 0 and k >= 0:
                    first = dp[i - 1][j][k]
                if i+1 >= 0 and j-1 >= 0 and k >= 0:
                    second = dp[i + 1][j - 1][k]
                if i >= 0 and j+1 >= 0 and k-1 >= 0:
                    third = dp[i][j + 1][k - 1]
                dp[i][j][k] = (first*i + second * j +
                               third * k + N) / (i + j + k)
    print(dp[single][double][triple])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#http://compro.tsutajiro.com/archive/180220_probability_dp.pdf
import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split( )))
cnt =[0,0,0]
for i in range(n):
    cnt[3-a[i]] += 1

divT = [1/i for i in range(1,n+1)]

from functools import lru_cache### 
@lru_cache(100100100)

def f(x,y,z):
    if x==y==z==n:
        return 0
    res = 0
    delta = n*divT[n-z-1]
    if x != n:
        res += (f(x+1,y,z) + delta) * (n-x)
    if y != x:
        res += (f(x,y+1,z) + delta) * (x-y)#x+1,y+1ではない
    if z != y:
        res += (f(x,y,z+1) + delta)* (y-z)#x+1,y+1,z+1ではない
    res *=divT[n-z-1]
    return res
    

print(f(cnt[1]+cnt[2],cnt[2],0))

Submission status: Runtime Error

Solution code:
#http://compro.tsutajiro.com/archive/180220_probability_dp.pdf
import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split( )))
cnt =[0,0,0]
for i in range(n):
    cnt[3-a[i]] += 1

divT = [1/i for i in range(1,n+1)]

from functools import lru_cache### これの練習
@lru_cache(100000)

def f(x,y,z):
    if x==y==z==n:
        return 0
    res = 0
    delta = n*divT[n-z-1]
    if x != n:
        res += (f(x+1,y,z) + delta) * (n-x)
    if y != x:
        res += (f(x,y+1,z) + delta) * (x-y)#x+1,y+1ではない
    if z != y:
        res += (f(x,y,z+1) + delta)* (y-z)#x+1,y+1,z+1ではない
    res *=divT[n-z-1]
    return res
    

print(f(cnt[1]+cnt[2],cnt[2],0))

Submission status: Runtime Error

Solution code:
import random


def main():
    N = int(input())
    a = list(map(int, input().split()))
    ans = 0
    for _ in range(10**4):
        tmp = a.copy()
        all = list(range(N))
        not_aten = set(all)
        while not_aten:
            x = random.choice(all)
            tmp[x] -= 1
            if tmp[x] == 0:
                not_aten.remove(x)
            ans += 1
    print(ans/10**4)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
# from math import gcd
from functools import lru_cache
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

lst=[0]*3
for i in range(n):
    lst[a[i]-1]+=1

@lru_cache(maxsize=None)
def bfs(one, two, thr):
    if one==two==thr==0:
        return 0
    tmp=1
    if one>0:
        tmp+=bfs(one-1,two,thr)*one/n
    if two>0:
        tmp+=bfs(one+1,two-1,thr)*two/n
    if thr>0:
        tmp+=bfs(one,two+1,thr-1)*thr/n
    
    return tmp*n/(one+two+thr)

print(bfs(lst[0],lst[1],lst[2]))

Submission status: Runtime Error

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

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


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

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

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

Submission status: Runtime Error

Solution code:
W = int(input())
N, K = map(int, input().split(' '))
A = []
B = []
dp = [[[0 for _ in range(N + 1)] for _ in range(W + 1)] for _ in range(K + 1)]
for i in range(N):
  a, b = map(int, input().split(' '))
  A.append(a)
  B.append(b)
for i in range(K):
  for j in range(W + 1):
    for k in range(N):
      if j - A[k] >= 0:
        dp[i+1][j][k] = max(dp[i][j-A[k]][k-1] + B[k], dp[i+1][j][k])
      dp[i+1][j][k] = max(dp[i][j][k-1], dp[i+1][j][k])
print(max(dp[K][W][1:k+1]))

Submission status: Runtime Error

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

print(calc(one,two,three))

Submission status: Runtime Error

Solution code:
#input
import numpy as np
from numba import njit
from numba_modules import solve
N = int(input())
n1 = 0
n2 = 0
n3 = 0
for s in input().split():
    if s == "1":
        n1 += 1
    elif s == "2":
        n2 += 1
    else:
        n3 += 1
#output

#experiment

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

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

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

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

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

print(dp[n1, n2, n3])