Rubik's Cube Coloring (hard version) (Codeforces 1594E2)
Tags: Math, trees

It is the hard version of the problem. The difference is that in this version, there are nodes with already chosen colors.Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem?You have a perfect binary tree of $$$2^k - 1$$$ nodes — a binary tree where all vertices $$$i$$$ from $$$1$$$ to $$$2^{k - 1} - 1$$$ have exactly two children: vertices $$$2i$$$ and $$$2i + 1$$$. Vertices from $$$2^{k - 1}$$$ to $$$2^k - 1$$$ don't have any children. You want to color its vertices with the $$$6$$$ Rubik's cube colors (White, Green, Red, Blue, Orange and Yellow).Let's call a coloring good when all edges connect nodes with colors that are neighboring sides in the Rubik's cube.  A picture of Rubik's cube and its 2D map. More formally:   a white node can not be neighboring with white and yellow nodes;  a yellow node can not be neighboring with white and yellow nodes;  a green node can not be neighboring with green and blue nodes;  a blue node can not be neighboring with green and blue nodes;  a red node can not be neighboring with red and orange nodes;  an orange node can not be neighboring with red and orange nodes; However, there are $$$n$$$ special nodes in the tree, colors of which are already chosen.You want to calculate the number of the good colorings of the binary tree. Two colorings are considered different if at least one node is colored with a different color.The answer may be too large, so output the answer modulo $$$10^9+7$$$.

Input: The first line contains the integers $$$k$$$ ($$$1 \le k \le 60$$$) — the number of levels in the perfect binary tree you need to color.The second line contains the integer $$$n$$$ ($$$1 \le n \le \min(2^k - 1, 2000)$$$) — the number of nodes, colors of which are already chosen.The next $$$n$$$ lines contains integer $$$v$$$ ($$$1 \le v \le 2^k - 1$$$) and string $$$s$$$ — the index of the node and the color of the node ($$$s$$$ is one of the white, yellow, green, blue, red and orange).It is guaranteed that each node $$$v$$$ appears in the input at most once.

Output: Print one integer — the number of the different colorings modulo $$$10^9+7$$$.

Note: In the picture below, you can see one of the correct colorings of the first test example.  

Sample Inputs:
3
2
5 orange
2 white

2
2
1 white
2 white

10
3
1 blue
4 red
5 orange

Sample Outputs:
1024

0

328925088

Solution (Python):
import sys
MOD = int(1000000000.0 + 7)
colors_matrix = [[2, 4, 3, 5], [2, 4, 3, 5], [0, 1, 4, 5], [0, 1, 4, 5], [0, 1, 2, 3], [0, 1, 2, 3]]

def rec(tree, vertex, level):
    global dp
    if vertex not in tree:
        return [dp[level]] * 6
    vertex_color_restriction = tree[vertex]
    if level == 0:
        if vertex_color_restriction >= 0:
            res = [0] * 6
            res[vertex_color_restriction] = 1
            return res
        else:
            return [1] * 6
    left = rec(tree, vertex * 2, level - 1)
    right = rec(tree, vertex * 2 + 1, level - 1)
    res = [0] * 6
    for vertex_color in range(6):
        if vertex_color_restriction >= 0 and vertex_color_restriction != vertex_color:
            continue
        left_options = 0
        for color_left in colors_matrix[vertex_color]:
            left_options += left[color_left]
        right_options = 0
        for color_right in colors_matrix[vertex_color]:
            right_options += right[color_right]
        res[vertex_color] = left_options * right_options % MOD
    return res

def solve():
    global dp
    inp = sys.stdin.readline
    k = int(inp())
    n = int(inp())
    dp = [0] * k
    dp[0] = 1
    for i in range(1, k):
        dp[i] = dp[i - 1] * 4 * dp[i - 1] * 4 % MOD
    tree = {}
    color_to_id = {'white': 0, 'yellow': 1, 'green': 2, 'blue': 3, 'red': 4, 'orange': 5}
    for i in range(n):
        (v, s) = inp().split()
        v = int(v)
        tree[v] = color_to_id[s]
        v //= 2
        while v > 0:
            if v not in tree:
                tree[v] = -1
            v //= 2
    print(sum(rec(tree, 1, k - 1)) % MOD)

def main():
    solve()
if __name__ == '__main__':
    main()

Solution (Python):
MOD = 10 ** 9 + 7
color = {'white': 0, 'yellow': 5, 'green': 2, 'blue': 3, 'red': 4, 'orange': 1}
special = {}
marked = set()
k = int(input())
n = int(input())
dp = {}

def solve(i, j):
    if special.get(i, -1) != -1 and special[i] != j:
        return 0
    if dp[i][j] != -1:
        return dp[i][j]
    nodel = 2 * i
    noder = 2 * i + 1
    suml = 0
    sumr = 0
    for col in range(6):
        if col != 5 - j and col != j:
            if nodel in marked:
                suml += solve(nodel, col)
            else:
                suml = 1
            if noder in marked:
                sumr += solve(noder, col)
            else:
                sumr = 1
    dp[i][j] = suml * sumr % MOD
    return dp[i][j]
for _ in range(n):
    (v, s) = input().split(' ')
    v = int(v)
    special[v] = color[s]
    while v not in marked and v >= 1:
        marked.add(v)
        dp[v] = [-1] * 6
        v //= 2
res = 0
for col in range(6):
    res += solve(1, col)
unmarked_node = 2 ** k - 1 - len(marked)
ability_unmarked = pow(4, unmarked_node, MOD)
res %= MOD
res = res * ability_unmarked % MOD
print(res)

Solution (Python):
k = int(input())
n = int(input())
l = [1]
m = 10 ** 9 + 7
for i in range(k - 1):
    l.append(16 * l[-1] ** 2 % m)
counts = {}
ind = {'white': 0, 'green': 1, 'red': 2, 'yellow': 3, 'blue': 4, 'orange': 5}
given = {}
for i in range(n):
    (v, c) = input().split()
    v = int(v)
    c = ind[c]
    given[v] = c

def count(k, i, counts, color=None):
    if i in counts:
        return None
    m = 10 ** 9 + 7
    if 2 * i not in counts and 2 * i + 1 not in counts:
        layer = len(bin(i)) - 2
        if color is None:
            counts[i] = [l[k - layer]] * 6
        else:
            counts[i] = [0] * 6
            counts[i][color] = l[k - layer]
    elif 2 * i in counts and 2 * i + 1 in counts:
        left = counts[2 * i]
        right = counts[2 * i + 1]
        new = [0] * 6
        for _ in range(6):
            totleft = sum(left) - left[_] - left[(_ + 3) % 6]
            totright = sum(right) - right[_] - right[(_ + 3) % 6]
            if color is None or _ == color:
                new[_] = totleft * totright % m
        counts[i] = new
    else:
        layer = len(bin(i)) - 2
        if 2 * i in counts:
            left = counts[2 * i]
            right = [l[k - layer - 1]] * 6
        else:
            left = counts[2 * i + 1]
            right = [l[k - layer - 1]] * 6
        new = [0] * 6
        for _ in range(6):
            totleft = sum(left) - left[_] - left[(_ + 3) % 6]
            totright = sum(right) - right[_] - right[(_ + 3) % 6]
            if color is None or _ == color:
                new[_] = totleft * totright % m
        counts[i] = new
bits = k
while bits > 0:
    for guy in list(counts.keys()):
        if len(bin(guy)) == bits + 3:
            if guy // 2 in given:
                color = given[guy // 2]
            else:
                color = None
            count(k, guy // 2, counts, color)
    for guy in given:
        if len(bin(guy)) == bits + 2:
            count(k, guy, counts, given[guy])
    bits -= 1
print(sum(counts[1]) % m)