Black and White
Time limit5sMemory limit1024 MB
Count the spanning trees of a wheel-shaped graph for each k, where k is the number of trees' roads controlled by the White gang.
- Level
Hard9 of 10
- Topics
- Graph, Dynamic programming, Combinatorics, Divide and conquer
- Solved
- No attempts yet
Problem
In a new region, Sam found cities connected by two-way roads. Exactly of these cities lie on a circle, and one city is the capital located at the center. Number the cities on the circle from to in traversal order, and give the capital number . Each road either connects the capital with a city on the circle, or connects two adjacent cities on the circle. That is, a road connects cities and , or cities and , where .
Each road is controlled by one of two rival gangs: the White gang or the Black gang. Sam wants to choose and secure the minimum number of roads so that every city can reach every other city. In other words, Sam wants to choose a spanning tree of the city graph. Any spanning tree contains exactly roads.
To secure a road, Sam has to make a deal with the leader of the gang that controls it. Sam does not yet know which gang will be more willing to negotiate, so for each integer he asks you to count the number of spanning trees that contain exactly roads controlled by the White gang, with the remaining roads controlled by the Black gang. Since the answers can be large, output them modulo .
Input
The first line contains one integer ().
The second line contains a string of length describing the roads between cities on the circle. If is -, there is no road between city and city . If is W, the road is controlled by the White gang. If is B, the road is controlled by the Black gang.
The third line contains a string of length describing the roads between the capital and the cities on the circle. If is -, there is no road between the capital and city . If is W, the road is controlled by the White gang. If is B, the road is controlled by the Black gang.
Output
Print integers . The -th of them is the number of spanning trees in which exactly roads are controlled by the White gang.