#P16535. [THUPC 2026 决赛] 供电网络
[THUPC 2026 决赛] 供电网络
Background
From the final of the 2026 Tsinghua University Student Programming Contest and Intercollegiate Invitational (THUPC2026).
Resources such as the editorial can be found at https://github.com/dapingguo8/THUPC2026-final.
After finishing the yearbook, the venue was preparing to start the nighttime lighting effects. However, since the backstage power supply network had not been configured yet, the long-awaited holographic projector still could not be turned on.
The entire power grid consists of many relay nodes, and these nodes must be connected to two parallel main power modules. According to the electrical safety protocol, adjacent nodes within the same module are very likely to cause phase resonance. Therefore, the system has a strict parity restriction on the number of neighbors that each node has within its own module.
Facing the complicated transmission lines, Xiao S pulled out a stack of old test records. Xiao T pointed out that, since the tests back then were carried out level by level along the hierarchy of the grid, the node sets involved in the records have a regular nested structure: for any two records, the node sets they involve are either completely disjoint, or one strictly contains the other.
Blind trial and error is not only time-consuming but also highly risky. In order to configure the power supply network as soon as possible, Xiao T and Xiao S need to restore in advance, for each test, the number of ways to connect all nodes to the main modules.
Problem Description
The power supply network contains nodes. Nodes are connected by several bidirectional transmission lines, forming an undirected graph.
When configuring the network, all nodes will be assigned to two independent main power modules. For node , define its same-module neighbor count as: within the power module that node is connected to, the number of nodes that have a direct line connected to node .
Xiao S found records of tests. Each test record is represented by a string of length . For node :
- If , then in this configuration, node must have an even same-module neighbor count ;
- If , then in this configuration, node must have an odd same-module neighbor count ;
- If , then node is not involved in the record, i.e. there is no requirement on the parity of .
Xiao T pointed out that the node sets involved in the records have a regular nested structure. Specifically, let the node set involved in the -th test be (i.e. the set of positions in the string that are not ?). Then for any two different test records , and must satisfy exactly one of the following three relations: , , or .
To configure the power supply network as soon as possible, you need to help Xiao T and Xiao S compute, for each test, the number of essentially different ways to connect all nodes to the two main modules. Two assignments are considered different if and only if there exists at least one node that is connected to different main modules in the two assignments. Since the answer may be large, you only need to output it modulo .
Input Format
The first line contains two positive integers .
The next lines each contain a string of length . The -th character of the -th line indicates whether there is a transmission line between node and node : 1 if there is, otherwise 0.
The next lines each contain a string of length , representing one test record.
Output Format
Output lines. Each line contains a non-negative integer, representing the number of essentially different ways to connect all nodes to the two main modules in that test, modulo .
3 2
010
100
000
1?0
010
4
0
6 5
000010
000001
000000
000001
100000
010100
?11?0?
??????
?10?1?
??0?0?
?01?01
0
64
16
32
0
Hint
For the first test in Sample 1, there are four possible connection assignments:
- Connect all nodes to the first main module.
- Connect all nodes to the second main module.
- Connect nodes to the first main module, and connect node to the second main module.
- Connect nodes to the second main module, and connect node to the first main module.
Translated by ChatGPT 5