結果

問題 No.463 魔法使いのすごろく🎲
ユーザー lam6er
提出日時 2025-03-31 17:28:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,236 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 82,252 KB
実行使用メモリ 74,192 KB
最終ジャッジ日時 2025-03-31 17:30:14
合計ジャッジ時間 3,539 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    n, m = int(input[idx]), int(input[idx+1])
    idx +=2
    if n > 2:
        c = list(map(int, input[idx:idx+n-2]))
    else:
        c = []
    
    cost = [0.0]*(n+1)
    for i in range(2, n):
        cost[i] = c[i-2]
    
    # Precompute next_pos[i][j]
    next_pos = [[0]*(m+1) for _ in range(n+1)]
    for i in range(1, n+1):
        for j in range(1, m+1):
            current = i
            for _ in range(j):
                if current < n:
                    current +=1
                else:
                    current -=1
            next_pos[i][j] = current
    
    # Initialize E0 and E1
    E0 = [0.0]*(n+1)
    E1 = [0.0]*(n+1)
    epsilon = 1e-12
    max_iter = 100000  # Prevent infinite loop
    
    for _ in range(max_iter):
        new_E0 = [0.0]*(n+1)
        new_E1 = [0.0]*(n+1)
        max_error = 0.0
        
        for i in range(1, n+1):
            if i == n:
                new_E0[i] = 0.0
                new_E1[i] = 0.0
                continue
            
            # Compute E1[i]
            total1 = 0.0
            for j in range(1, m+1):
                next_i = next_pos[i][j]
                total1 += E1[next_i]
            new_E1_i = cost[i] + total1 / m
            new_E1[i] = new_E1_i
            
            # Compute E0[i]
            min_e1 = float('inf')
            for j in range(1, m+1):
                next_i = next_pos[i][j]
                if E1[next_i] < min_e1:
                    min_e1 = E1[next_i]
            option1 = cost[i] + min_e1
            
            total2 = 0.0
            for j in range(1, m+1):
                next_i = next_pos[i][j]
                total2 += E0[next_i]
            option2 = cost[i] + (total2 / m)
            
            new_E0_i = min(option1, option2)
            new_E0[i] = new_E0_i
            
            # Update max_error
            max_error = max(max_error, abs(new_E0_i - E0[i]), abs(new_E1_i - E1[i]))
        
        # Check convergence
        if max_error < epsilon:
            break
        E0, new_E0 = new_E0, E0
        E1, new_E1 = new_E1, E1
    
    print("{0:.10f}".format(E0[1]))

if __name__ == '__main__':
    main()
0