Problem:
Problem C: Minimal Backgammon Here is a very simple variation of the game backgammon, named “Minimal Backgammon”. The game is played by only one player, using only one of the dice and only one checker (the token used by the player). The game board is a line of ( N + 1) squares labeled as 0 (the start) to N (the goal). At the beginning, the checker is placed on the start (square 0). The aim of the game is to bring the checker to the goal (square N ). The checker proceeds as many squares as the roll of the dice. The dice generates six integers from 1 to 6 with equal probability. The checker should not go beyond the goal. If the roll of the dice would bring the checker beyond the goal, the checker retreats from the goal as many squares as the excess. For example, if the checker is placed at the square ( N - 3), the roll "5" brings the checker to the square ( N - 2), because the excess beyond the goal is 2. At the next turn, the checker proceeds toward the goal
as usual. Each square, except the start and the goal, may be given one of the following two special instructions.  Lose one turn (labeled " L " in Figure 2) If the checker stops here, you cannot move the checker in the next turn.  Go back to the start (labeled " B " in Figure 2) If the checker stops here, the checker is brought back to the start.    Figure 2: An example game  Given a game board configuration (the size N , and the placement of the special instructions), you are requested to compute the probability with which the game succeeds within a given number of turns. Input The input consists of multiple datasets, each containing integers in the following format.  N T L B  Lose 1 ... Lose L  Back 1 ... Back B   N is the index of the goal, which satisfies 5 ≤ N ≤ 100. T is the number of turns. You are requested to compute the probability of success within T turns. T satisfies 1 ≤ T ≤ 100. L is the number of squares marked “Lose one turn”, which satisfies 0 ≤ L ≤ N - 1. B is the number of squares marked “Go back to the start”, which satisfies 0 ≤ B ≤ N - 1. They are separated by a space.  Lose i 's are the indexes of the squares marked “Lose one turn”, which satisfy 1 ≤ Lose i ≤ N - 1. All Lose i 's are distinct, and sorted in ascending order. Back i 's are the indexes of the squares marked “Go back to the start”, which satisfy 1 ≤ Back i ≤ N - 1. All Back i 's are distinct, and sorted in ascending order. No numbers occur both in Lose i 's and Back i 's. The end of the input is indicated by a line containing four zeros separated by a space. Output For each dataset, you should answer the probability with which the game succeeds within the
given number of turns. The output should not contain an error greater than 0.00001. Sample Input 6 1 0 0
7 1 0 0
7 2 0 0
6 6 1 1
2
5
7 10 0 6
1
2
3
4
5
6
0 0 0 0 Output for the Sample Input 0.166667
0.000000
0.166667
0.619642
0.000000

Solution (Python):
while True:
    n, t, L, b = (int(s) for s in input().split())
    if (n, t, L, b) == (0, 0, 0, 0):
        break

    loses = [int(input()) for i in range(L)]
    backs = [int(input()) for i in range(b)]
    dp = [[0.] * n + [1.] for i in range(3)]
    stops = list(range(n + 1)) + list(reversed(range(n - 5, n)))

    for i in reversed(range(t)):
        for j in range(n):
            dp[i % 3][j] = sum(
                dp[(i + 2) % 3][d] if d in loses
                else dp[(i + 1) % 3][0] if d in backs
                else dp[(i + 1) % 3][d]
                for d in stops[j + 1:j + 7]) / 6

    print('{:.6f}'.format(dp[0][0]))

Solution (Python):
while 1:
	N, T, L, B = map(int,raw_input().split(" "))
	if N == T == L == B == 0: break
	check = 0
	dp = [[0 for _ in range(N+1)] for _ in range(T+1)]
	Lose = set([int(raw_input()) for _ in range(L)])
	Back = set([int(raw_input()) for _ in range(B)])
	dp[0][0] = 1
	for i in range(T):
		for j in range(N):
			rank = i-1 if j in Lose else i
			for d in range(1,7):
				next = j + d
				if next > N :
					next = N - (next - N)
				if next in Back:
					dp[i+1][0] += dp[rank][j] / 6.
				else :
					dp[i+1][next] += dp[rank][j] / 6.
	
	print "%6f" % sum([dp[i][N] for i in range(1,T+1)])

Solution (Python):
while 1:
	N, T, L, B = map(int,raw_input().split(" "))
	if N == T == L == B == 0: break
	dp = [[0 for _ in range(N+1)] for _ in range(T+1)]
	Lose = set([int(raw_input()) for _ in range(L)])
	Back = set([int(raw_input()) for _ in range(B)])
	dp[0][0] = 1
	for i in range(T):
		for j in range(N):
			rank = i-1 if j in Lose else i
			for d in range(1,7):
				next = j + d
				if next > N :
					next = N - (next - N)
				if next in Back:
					dp[i+1][0] += dp[rank][j] / 6.
				else :
					dp[i+1][next] += dp[rank][j] / 6.
	
	print "%6f" % sum([dp[i][N] for i in range(1,T+1)])

Solution (Python):
while 1:
    N,T,L,B = map(int,raw_input().split())
    if N == 0: break
    lose = [int(raw_input()) for i in xrange(L)]
    back = [int(raw_input()) for i in xrange(B)]

    dp = [[0.0]*(N+1) for i in xrange(T+2)]
    dp[0][0] = 1.0
    ans = 0.0
    p = 1.0/6.0
    for t in xrange(T):
        for i in xrange(N):
            tt = t-1 if i in lose else t
            for j in xrange(i+1,i+7):
                if j > N: j = N-j%N
                if j in back:
                    dp[t+1][0] += p*dp[tt][i]
                else:
                    dp[t+1][j] += p*dp[tt][i]
        dp[t+1][N] += dp[t][N]
    print "%.6f"%dp[T][N]

Solution (Python):
while 1:
    N,T,L,B = map(int,raw_input().split())
    if N == 0: break
    lose = set([int(raw_input()) for i in xrange(L)])
    back = set([int(raw_input()) for i in xrange(B)])
    dp = [[0.]*(N+1) for i in xrange(T+1)]
    dp[0][0] = 1.
    p = 1./6.
    for t in xrange(T):
        for i in xrange(N):
            tt = t-1 if i in lose else t
            for j in xrange(i+1,i+7):
                if j > N: j = N-j%N
                if j in back: j = 0
                dp[t+1][j] += p*dp[tt][i]
        dp[t+1][N] += dp[t][N]
    print "%.6f"%dp[T][N]

Solution (Python):
def cross(vT,AT):
   return [sum([x*y for x,y in zip(vT,a)]) for a in AT]

me=1.0/6
while True:
    [n,t,l,b] = [int(x) for x in raw_input().split()]
    if n|t|l|b == 0:break
    ls={}
    for i in xrange(l):
        ls[int(raw_input())]=n+i+1
    bs=[]
    for i in xrange(b):
        bs.append(int(raw_input()))
    AT=[]
    for i in xrange(n+l+1):
        AT.append([0.0]*(n+l+1))
    for i in xrange(n):
        for j in xrange(1,7):
            x=i+j
            if x>n:x=n*2-x
            if x in ls:
                AT[ls[x]][i]+=me
            elif x in bs:
                AT[0][i]+=me
            else:
                AT[x][i]+=me
    AT[n][n]=1.0
    for k,v in ls.iteritems():
        AT[k][v]+=1.0
    bT=[0.0]*(n+l+1)
    bT[0]=1.0
    for i in xrange(t):
        bT=cross(bT,AT)
    print '%.6f' % bT[n]

Solution (Python):
while True:
    N, T, L, B = [int(_) for _ in raw_input().split()]
    if N == T == L == B == 0:
        break
    loses = {int(raw_input()) for _ in xrange(L)}
    backs = {int(raw_input()) for _ in xrange(B)}

    probs = [[0.0] * (N + 1) for _ in xrange(T + 1)]
    for t in xrange(T + 1):
        probs[t][N] = 1.0

    for t in xrange(1, T + 1):
        for x in xrange(N):
            if x in loses:
                dt = 2
                if t == 1:
                    continue
            else:
                dt = 1
            for dx in xrange(1, 7):
                if x + dx <= N:
                    probs[t][x] += probs[t - dt][x + dx] / 6.0
                else:  
                    probs[t][x] += probs[t - dt][2 * N - (x + dx)] / 6.0
        for x in backs:
            probs[t][x] = probs[t][0]

    print("{:.6f}".format(probs[T][0]))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        n,t,l,b = LI()
        if n == 0:
            break
        ls = set([I() for _ in range(l)])
        bs = set([I() for _ in range(b)])
        r = [[0] * (n+1) for _ in range(t+2)]
        r[0][0] = 1
        for i in range(t):
            for j in range(n):
                if r[i][j] == 0:
                    continue
                for k in range(1,7):
                    nj = j+k
                    if nj > n:
                        nj = n - (nj-n)
                    ni = i + 1
                    if nj in ls:
                        ni += 1
                    if nj in bs:
                        nj = 0
                    if nj == n:
                        ni = t
                    r[ni][nj] += r[i][j] / 6.0
        rr.append('{:0.9f}'.format(r[t][n]))

    return '\n'.join(map(str, rr))


print(main())

Solution (Python):
import sys
import bisect


while True:
    N, T, L, B = map(int, sys.stdin.readline().split())
    
    if N == 0:
        break
    
    state = [[0 for i in xrange(T + 2)] for j in xrange(N + 1)]
    
    lose = set()
    for i in xrange(L):
        lose.add(int(raw_input()))
    back = set()
    for i in xrange(B):
        back.add(int(raw_input()))
    
    state[0][0] = 1.0
    
    for i in xrange(T):
        for j in xrange(N):
            for k in xrange(1, 7):
                next = j + k
                if next > N:
                    next = N - (next - N)
                if next in lose:
                    state[next][i + 2] += state[j][i] / 6.0
                elif next in back:
                    state[0][i + 1] += state[j][i] / 6.0
                else:
                    state[next][i + 1] += state[j][i] / 6.0
        state[N][i + 1] += state[N][i]
    print '{:6f}'.format(state[N][T])

Solution (Python):
while True:
    N, T, L, B = map(int, input().split())
    if not (N | T | L | B):
        break

    Lose = [False for i in range(N + 1)]
    Back = [False for i in range(N + 1)]
    for i in range(L):
        Lose[int(input())] = True

    for j in range(B):
        Back[int(input())] = True

    dp = [[0.0 for j in range(N + 1)] for i in range(T + 2)]
    dp[0][0] = 1.0

    
    for t in range(T):
        
        for n in range(N):
            
            for k in range(1, 7):
                tmp = n + k
                if n + k > N:
                    tmp = N - n - k - 1
                if Lose[tmp]:
                    dp[t + 2][tmp] += dp[t][n] / 6
                elif Back[tmp]:
                    dp[t + 1][0] += dp[t][n] / 6
                else:
                    dp[t + 1][tmp] += dp[t][n] / 6

    ans = 0.0
    for i in range(T + 2):
        ans += dp[i][N]
    print("{0:.6f}".format(ans))

Solution (Python):
while True:
    N, T, L, B = map(int, input().split())
    if N | T | L | B == 0:
        break

    lose = set([int(input()) for i in range(L)])
    back = set([int(input()) for i in range(B)])
    dp = [[0] * (T + 1) for j in range(N + 1)]
    dp[0][0] = 1

    for t in range(T):
        for n in range(N):
            for d in range(1, 7):
                step = 0
                if n + d <= N:
                    step = n + d
                else:
                    step = 2 * N - (n + d)

                if step in lose:
                    if t + 2 <= T:
                        dp[step][t + 2] += dp[n][t] / 6
                elif step in back:
                    dp[0][t + 1] += dp[n][t] / 6
                else:
                    dp[step][t + 1] += dp[n][t] / 6

    print(format(sum(dp[-1]), "0.6f"))

Solution (Python):
while True:
    n,turn,y,b = map(int,input().split())
    if [n,turn,y,b]==[0,0,0,0]:
        break
        
    yasumi=[]
    back=[]
    for _ in range(y):
        yasumi.append(int(input()))
    for _ in range(b):
        back.append(int(input()))
    
    taiki=[0]*(turn+10)
    dp=[[0.0]*(n+1) for _ in range(turn+10)]
    dp[0][0]=1
    for i in range(0,turn):
        for j in range(0,n):
            for dice in range(1,7):
                cur= j+dice
                if cur > n:
                    cur= n*2-cur
                if cur in yasumi:
                    dp[i+2][cur]+=dp[i][j]/6
                        
                elif cur in back:
                    dp[i+1][0]+=dp[i][j]/6
                else:
                    dp[i+1][cur]+=dp[i][j]/6

    print('{:.6f}'.format(sum([dp[i][n] for i in range(turn+1)])))

Solution (Python):
def solve(N,T,L,B):
    route = [-1] * (N + 1)
    for _ in range(L):
        x = int(input())
        route[x] = 0
    for _ in range(B):
        y = int(input())
        route[y] = 1
    
    ans = 0
    before = [0] * (N + 1)
    before[0] = 1
    beforebefore = [0] * (N + 1)
    for _ in range(T):
        now = [0] * (N + 1)
        future = [0] *(N + 1)
        for i in range(N):
            for j in range(1,7):
                X = i + j
                if X >= N + 1:
                    X = N - (X - N)
                if route[X] == -1:
                    now[X] += before[i]/6
                elif route[X] == 0:
                    future[X] += before[i]/6
                else:
                    now[0] += before[i]/6
        ans += now[-1]
        before = [0] * (N + 1)
        for i in range(N + 1):
            before[i] = now[i] + beforebefore[i]
        beforebefore = future
    print("{:9f}".format(ans))

def main():
    while True:
        N,T,L,B = map(int,input().split())
        if N == 0:
            return
        solve(N,T,L,B)
if __name__ == '__main__':
    main()

Solution (Python):
def solve():
    from sys import stdin
    f_i = stdin
    
    while True:
        N, T, L, B = map(int, f_i.readline().split())
        if N == 0:
            break
        
        skip = {}
        sq_attr = [None] * N
        for i in range(L):
            sq = int(f_i.readline())
            sq_attr[sq] = 'Lose'
            skip[sq] = 0
        for i in range(B):
            sq_attr[int(f_i.readline())] = 'Back'
        
        dp1 = [0] * N
        dp2 = [0] * N
        dp1[0] = 1
        ans = 0
        
        for i in range(T):
            for sq, pr in skip.items():
                tmp = dp1[sq]
                dp1[sq] = pr
                skip[sq] = tmp
            for sq, pr in enumerate(dp1):
                if pr == 0:
                    continue
                
                new_pr = pr / 6
                for next_sq in range(sq + 1, sq + 7):
                    if next_sq > N:
                        next_sq = 2 * N - next_sq
                    elif next_sq == N:
                        ans += new_pr
                        continue
                    
                    if sq_attr[next_sq] == 'Back':
                        next_sq = 0
                    dp2[next_sq] += new_pr
                    
            dp1 = dp2
            dp2 = [0] * N
        
        print('{:.6f}'.format(ans))

solve()

Solution (Python):
while True:
	N, T, L, B = map(int, input().split(' '))
	if N == 0:
		break
	l = set([int(input()) for _ in range(L)])
	b = set([int(input()) for _ in range(B)])

	memo = [[0.0] * (N+1) for _ in range(T+2)]
	memo[0][0] = 1
	for t in range(T):
		for n in range(N):
			for i in [1,2,3,4,5,6]:
				next_pos = n + i
				if next_pos > N:
					next_pos = 2*N - next_pos

				if next_pos in l:
					memo[t+2][next_pos] += memo[t][n]/6
				elif next_pos in b:
					memo[t+1][0] += memo[t][n]/6
				else:
					memo[t+1][next_pos] += memo[t][n]/6

	print('{:.6f}'.format(sum([memo[t][N] for t in range(T+1)])))

Solution (Python):
def main():
    while True:
        N, T, L, B = map(int, input().split())
        if N == T == L == B == 0:
            break
        Lose = []
        for i in range(L):
            Lose.append(int(input()))
        isLose = {i:False for i in range(N)}
        for i in Lose:
            isLose[i] = True
        Back = []
        for i in range(B):
            Back.append(int(input()))
        

        Board = [0.0 for x in range(N+1)]
        Board[0] = 1.0

        Lose_Board = [0.0 for x in range(N+1)]
        for turn_i in range(T):
            new_Board = [0.0 for x in range(N+1)]
            for board_i in range(N):
                if isLose[board_i]:
                    Board[board_i], Lose_Board[board_i] = Lose_Board[board_i], Board[board_i]
                if Board[board_i] == 0.0:
                    continue
                for i in range(1, 7):
                    new_board_i = board_i + i
                    if new_board_i > N:
                        new_board_i = N - abs(new_board_i - N)
                    new_Board[new_board_i] += Board[board_i] * (1/6)
            for i in Back:
                new_Board[0] += new_Board[i]
                new_Board[i] = 0.0
            new_Board[-1] += Board[-1]
            Board = new_Board
        print(f'{Board[N]:.6f}')


main()

Solution (Python):


import sys


input = sys.stdin.readline


def inpl(): return list(map(int, input().split()))


while True:
    N, T, L, B = inpl()
    if N == 0: break
    lose = [int(input()) for _ in range(L)]
    back = [int(input()) for _ in range(B)]
    mass = [0] * (N + 1)
    for l in lose:
        mass[l] = 1
    for b in back:
        mass[b] = 2

    dp = [[0] * (N + 1) for _ in range(T + 3)]
    dp[0][0] = 1.0

    for i in range(T + 1):
        for j in range(N):
            if dp[i][j] == 0.0:
                continue
            for k in range(1, 7):
                curr = j + k
                if curr > N: curr = N - curr % N
                if mass[curr] == 0:  
                    dp[i + 1][curr] += dp[i][j] / 6.0
                elif mass[curr] == 1:  
                    dp[i + 2][curr] += dp[i][j] / 6.0
                else:  
                    dp[i + 1][0] += dp[i][j] / 6.0

    ans = 0
    for i in range(T + 1):
        ans += dp[i][N]
    print('{:.6f}'.format(ans))

Solution (Python):
N, T, L, B = map(int, input().split())
while (N != 0 and T != 0):
    Llist = []
    Blist = []
    for i in range(L):
        Llist.append(int(input()))
    for i in range(B):
        Blist.append(int(input()))
    prob = [0for i in range(N+1)]
    prob[0] = 1 
    copy1 = [0for i in range(N+1)]  
    copy2 = prob[:] 
    cntp = 0
    for t in range(1,T+1):
        for i in range(N, -1, -1):
            prob_i = 0
            if (i != 0):
                prob_i = 0
                for k in range(i-6,i):
                    if (k in Blist):
                        prob_i += 0
                    elif (k in Llist):
                        prob_i += (1/6)*copy1[k]
                    elif (k >= 0):
                        prob_i += (1/6)*copy2[k]
            if(N-5<= i <N):
                if ((N-1) in Blist):
                    prob_i += 0
                elif ((N-1) in Llist):
                    prob_i += (1/6)*copy1[N-1]
                else:
                    prob_i += (1/6)*copy2[N-1]
            if(N-4<= i <N):
                if ((N-2) in Blist):
                    prob_i += 0
                elif ((N-2) in Llist):
                    prob_i += (1/6)*copy1[N-2]
                else:
                    prob_i += (1/6)*copy2[N-2]
            if(N-3<= i <N):
                if ((N-3) in Blist):
                    prob_i += 0
                elif ((N-3) in Llist):
                    prob_i += (1/6)*copy1[N-3]
                else:
                    prob_i += (1/6)*copy2[N-3]
            if(N-2<= i <N):
                if ((N-4) in Blist):
                    prob_i += 0
                elif ((N-4) in Llist):
                    prob_i += (1/6)*copy1[N-4]
                else:
                    prob_i += (1/6)*copy2[N-4]
            if(N-1<= i <N):
                if ((N-5) in Blist):
                    prob_i += 0
                elif ((N-5) in Llist):
                    prob_i += (1/6)*copy1[N-5]
                else:
                    prob_i += (1/6)*copy2[N-5]
            
            if(i == 0):
                for b in Blist:
                    prob_i += prob[b]
            prob[i] = prob_i
            
        copy1 = copy2[:]
        copy2 = prob[:]
        cntp += prob[-1]
    print('{0:.6f}'.format(cntp))
    N, T, L, B = map(int, input().split())