Coloring Cards 2
Time limit1sMemory limit1024 MB
Given the colors of the first row of an N by N grid, sum the number of white patterns over all valid colorings where diagonals match and every row has the same white count.
- Level
Hard8 of 10
- Topics
- Combinatorics, Dynamic programming, Math
- Solved
- No attempts yet
Problem
Yeongwoo made greeting cards for the participants of the year-end contest. The image of Donghyeon, summoned from the entrance greeting card, will greet the participants. There are cards in total, arranged in a square with rows and columns. Leehwan is in charge of coloring the cards. Leehwan colors every card either black or white. Some cards in the first row are already colored. The coloring must follow these rules.
- To keep the color pattern from getting complicated, a card and the card diagonally below and to its right must have the same color. That is, the card in row , column and the card in row , column must have the same color. ()
- In every row, the number of white cards must be the same.
It can be proven that, no matter how the first-row cards are colored, there is at least one way to color the cards that follows these rules.
After all cards are colored, the score of the card array is defined as follows.
- Each white card belongs to a pattern. Two white cards belong to the same pattern only if one can move from one to the other through white cards only, moving up, down, left, or right.
- The score of the card array is the number of distinct patterns. The shape of a pattern does not matter.
Leehwan wants to know the scores of the card arrays that can result from coloring. Given the coloring of the first row, write a program that prints the sum of the scores of all distinct card arrays that can be obtained by coloring the cards under the rules. Since the answer can be very large, print it modulo .
Input
The first line contains an integer .
The second line contains a string of length consisting of 0, 1, and ?. If the -th character is ?, the card in row 1, column is not colored. If it is 0 or 1, the card in row 1, column is colored black or white, respectively.
Output
Print the sum of the scores of all distinct card arrays that can be obtained by coloring the cards under the rules, modulo .
Constraints
- All given numbers are integers.