Third Month Insanity (Codeforces 859D)
Tags: DP, probabilities

The annual college sports-ball tournament is approaching, which for trademark reasons we'll refer to as Third Month Insanity. There are a total of 2N teams participating in the tournament, numbered from 1 to 2N. The tournament lasts N rounds, with each round eliminating half the teams. The first round consists of 2N - 1 games, numbered starting from 1. In game i, team 2·i - 1 will play against team 2·i. The loser is eliminated and the winner advances to the next round (there are no ties). Each subsequent round has half as many games as the previous round, and in game i the winner of the previous round's game 2·i - 1 will play against the winner of the previous round's game 2·i.Every year the office has a pool to see who can create the best bracket. A bracket is a set of winner predictions for every game. For games in the first round you may predict either team to win, but for games in later rounds the winner you predict must also be predicted as a winner in the previous round. Note that the bracket is fully constructed before any games are actually played. Correct predictions in the first round are worth 1 point, and correct predictions in each subsequent round are worth twice as many points as the previous, so correct predictions in the final game are worth 2N - 1 points.For every pair of teams in the league, you have estimated the probability of each team winning if they play against each other. Now you want to construct a bracket with the maximum possible expected score.

Input: Input will begin with a line containing N (2 ≤ N ≤ 6).2N lines follow, each with 2N integers. The j-th column of the i-th row indicates the percentage chance that team i will defeat team j, unless i = j, in which case the value will be 0. It is guaranteed that the i-th column of the j-th row plus the j-th column of the i-th row will add to exactly 100.

Output: Print the maximum possible expected score over all possible brackets. Your answer must be correct to within an absolute or relative error of 10 - 9.Formally, let your answer be a, and the jury's answer be b. Your answer will be considered correct, if .

Note: In the first example, you should predict teams 1 and 4 to win in round 1, and team 1 to win in round 2. Recall that the winner you predict in round 2 must also be predicted as a winner in round 1.

Sample Inputs:
2
0 40 100 100
60 0 40 40
0 60 0 45
0 60 55 0

3
0 0 100 0 100 0 0 0
100 0 100 0 0 0 100 100
0 0 0 100 100 0 0 0
100 100 0 0 0 0 100 100
0 100 0 100 0 0 100 0
100 100 100 100 100 0 0 0
100 0 100 0 0 100 0 0
100 0 100 0 100 100 100 0

2
0 21 41 26
79 0 97 33
59 3 0 91
74 67 9 0

Sample Outputs:
1.75

12

3.141592

Solution (Python):
n = int(input())
m = 1 << n
p = [list(map(int, input().split())) for i in range(m)]
(u, x) = ([1] * m, [0] * m)
(v, y) = (u[:], x[:])
for i in range(n):
    d = 1 << i
    for j in range(m):
        s = d * (j // d ^ 1)
        v[j] = u[j] * sum((u[k] * p[j][k] for k in range(s, s + d))) / 100
        y[j] = max(x[s:s + d]) + x[j] + v[j] * d
    (u, v, x, y) = (v, u, y, x)
print(max(x))

Solution (Python):
n = int(input())
m = 1 << n
p = [list(map(int, input().split())) for i in range(m)]
(u, x) = ([1] * m, [0] * m)
(v, y) = (u[:], x[:])
for i in range(n):
    d = 1 << i
    for j in range(m):
        s = d * (j // d ^ 1)
        v[j] = u[j] * sum((u[k] * p[j][k] for k in range(s, s + d))) / 100
        y[j] = max(x[s:s + d]) + x[j] + v[j] * d
    (u, v, x, y) = (v, u, y, x)
print(max(x))

Solution (Python):
import functools
n = int(input())
props = []

def preproc(a):
    return float(a) / 100.0
for i in range(pow(2, n)):
    props.append(list(map(preproc, input().split())))
wining_props = []
wining_props_first_round = []
for i in range(0, 2 ** n, 2):
    wining_prop_for_i = props[i][i + 1]
    wining_props_first_round.append(wining_prop_for_i)
    wining_props_first_round.append(1.0 - wining_prop_for_i)
wining_props.append(wining_props_first_round)
assert len(wining_props_first_round) == len(props)
for round_num in range(2, n + 1):
    this_round_wining_props = []
    for team_num in range(2 ** n):
        t = team_num // 2 ** round_num * 2 ** round_num
        teams_we_meet_this_round = [t + x for x in range(2 ** round_num)]
        t = team_num // 2 ** (round_num - 1) * 2 ** (round_num - 1)
        teams_we_meet_prev_round = [t + x for x in range(2 ** (round_num - 1))]
        for tt in teams_we_meet_prev_round:
            teams_we_meet_this_round.remove(tt)
        this_team_wining_props = wining_props[round_num - 2][team_num]
        chances_win_i_team = []
        for tm in teams_we_meet_this_round:
            chances_win_i_team.append(wining_props[round_num - 2][tm] * props[team_num][tm])
        mo_win_this_round = sum(chances_win_i_team)
        this_team_wining_props *= mo_win_this_round
        this_round_wining_props.append(this_team_wining_props)
    wining_props.append(this_round_wining_props)

@functools.lru_cache(maxsize=None)
def revenue(round_num, teams_left, teams_right, winner=-1):
    split = (teams_left + teams_right) // 2
    if round_num == 1:
        return wining_props[0][winner] if winner != -1 else max(wining_props[0][teams_left:teams_right])
    if winner == -1:
        results = []
        for winner in range(teams_left, teams_right):
            winner_prop = wining_props[round_num - 1][winner]
            if winner >= split:
                res = sum([revenue(round_num - 1, teams_left, split), revenue(round_num - 1, split, teams_right, winner), winner_prop * 2 ** (round_num - 1)])
            else:
                res = sum([revenue(round_num - 1, teams_left, split, winner), revenue(round_num - 1, split, teams_right), winner_prop * 2 ** (round_num - 1)])
            results.append(res)
        return max(results)
    else:
        winner_prop = wining_props[round_num - 1][winner]
        if winner >= split:
            res = sum([revenue(round_num - 1, teams_left, split), revenue(round_num - 1, split, teams_right, winner), winner_prop * 2 ** (round_num - 1)])
        else:
            res = sum([revenue(round_num - 1, teams_left, split, winner), revenue(round_num - 1, split, teams_right), winner_prop * 2 ** (round_num - 1)])
        return res
print(revenue(n, 0, 2 ** n))