CF1594E2.Rubik's Cube Coloring (hard version)

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 2k−12^k - 1 nodes — a binary tree where all vertices ii from 11 to 2k−1−12^{k - 1} - 1 have exactly two children: vertices 2i2i and 2i+12i + 1. Vertices from 2k−12^{k - 1} to 2k−12^k - 1 don't have any children. You want to color its vertices with the 66 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 nn 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 109+710^9+7.

这是该问题的困难版本。区别在于,在此版本中,部分节点的颜色已经预先选定。

西奥法尼斯饿了,他想吃自己最爱的食物——希夫塔利亚香肠(sheftalia)。然而,他必须先完成家庭作业。你能帮他解决这个问题吗?

你有一棵包含 2k−12^k - 1 个节点的完美二叉树——即一棵二叉树,其中所有编号从 11 到 2k−1−12^{k - 1} - 1 的顶点 ii 均恰好有两个子节点:顶点 2i2i 和 2i+12i + 1;而编号从 2k−12^{k - 1} 到 2k−12^k - 1 的顶点均无子节点。你需要用魔方的六种颜色(白色、绿色、红色、蓝色、橙色和黄色)对这棵树的所有顶点进行染色。

若一棵树的所有边所连接的两个顶点的颜色,在魔方中恰好为相邻面,则称该染色方案为合法染色。

魔方实物图及其二维展开图。

更形式化地,约束如下:

  • 白色顶点不能与白色或黄色顶点相邻;
  • 黄色顶点不能与白色或黄色顶点相邻;
  • 绿色顶点不能与绿色或蓝色顶点相邻;
  • 蓝色顶点不能与绿色或蓝色顶点相邻;
  • 红色顶点不能与红色或橙色顶点相邻;
  • 橙色顶点不能与红色或橙色顶点相邻;

然而,树中有 nn 个特殊节点,其颜色已预先固定。

你需要计算该二叉树的合法染色方案总数。若至少有一个节点的颜色不同,则认为两种染色方案不同。

答案可能非常大,请输出结果对 109+710^9+7 取模后的值。

输入格式

The first line contains the integers kk (1≤k≤601 \le k \le 60) — the number of levels in the perfect binary tree you need to color.

The second line contains the integer nn (1≤n≤min⁡(2k−1,2000)1 \le n \le \min(2^k - 1, 2000)) — the number of nodes, colors of which are already chosen.

The next nn lines contains integer vv (1≤v≤2k−11 \le v \le 2^k - 1) and string ss — the index of the node and the color of the node (ss is one of the white, yellow, green, blue, red and orange).

It is guaranteed that each node vv appears in the input at most once.

第一行包含整数 kk(1≤k≤601 \le k \le 60)—— 表示你需要染色的完全二叉树的层数。

第二行包含整数 nn(1≤n≤min⁡(2k−1,2000)1 \le n \le \min(2^k - 1, 2000))—— 表示已预先指定颜色的节点个数。

接下来的 nn 行,每行包含一个整数 vv(1≤v≤2k−11 \le v \le 2^k - 1)和一个字符串 ss —— 分别表示节点的编号及其颜色(ss 是以下六种颜色之一:white、yellow、green、blue、red 和 orange)。

保证每个节点 vv 在输入中至多出现一次。

输出格式

Print one integer — the number of the different colorings modulo 109+710^9+7.

输出一个整数——不同染色方案的数量对 109+710^9+7 取模的结果。

输入输出样例

  • 输入#1

    3
    2
    5 orange
    2 white

    输出#1

    1024
  • 输入#2

    2
    2
    1 white
    2 white

    输出#2

    0
  • 输入#3

    10
    3
    1 blue
    4 red
    5 orange

    输出#3

    328925088

说明/提示

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

在下面的图片中,您可以看到第一个测试样例的一种正确染色方案。

输入解题思路,AI测评打分。不知道怎么写?

首页