#P9229. 简单九连环
简单九连环
Background
Hint: This problem has a large sample.
Hint: The “nine linked rings” in this problem are different from the traditional nine linked rings.
Nine linked rings are a traditional Chinese puzzle game. As shown in the figure, nine rings are placed on a “sword” and are linked with each other.

In the traditional nine linked rings, the -th ring can be put onto the “sword” (denoted as ) or taken off the “sword” (denoted as ) if and only if the -th ring is on the sword, and all rings before it are not on the sword. In particular, the -st ring can be put on or taken off freely.
In this problem, we discuss a more general situation, although this simple nine linked rings may not necessarily be physically constructible.
Problem Description
A simple nine linked rings can be viewed as two 01 strings: the rule string and the state string , satisfying . Here, means the -th ring is on, and means the -th ring is off.
Within the same game, never changes, while in each step changes the value at exactly one position (from 0 to 1 or from 1 to 0). The simple nine linked rings is fully taken off if and only if all are 0. The simple nine linked rings is fully put on if and only if all are 1.
The rule of the simple nine linked rings is: is allowed to change if and only if is a suffix of . It can be seen that the traditional nine linked rings is a special case where is 00...01.
Given , find the minimum number of steps needed to go from the fully taken-off state to the fully put-on state. Output the answer modulo .
Input Format
The first line contains an integer , the length of . Note that this is not the number of rings.
The second line contains a 01 string .
Output Format
One line containing an integer, the answer modulo .
3
011
6
8
00000001
341
见附件中的 samples/rings3.in
见附件中的 samples/rings3.ans
见附件中的 samples/rings4.in
见附件中的 samples/rings4.ans
Hint
Explanation for Sample 1
At the initial moment, all rings are not on the sword, and the state string is 0000.
In step 1, put on the -st ring, and becomes 1000.
In step 2, put on the -nd ring, and becomes 1100.
In step 3, put on the -rd ring, and becomes 1110.
Next, you cannot directly put on the -th ring, because 111 is not a suffix of the rule string 011. Therefore, in step 4 you should take off the -st ring, and becomes 0110.
Then in step 5, put on the -th ring, and becomes 0111.
In the last step, put on the -st ring, and becomes 1111, completing the goal.
Explanation for Sample 2
This is the traditional nine linked rings, and there are exactly rings.
Explanation for Sample 3
Sample 3 satisfies the constraints of test point .
Explanation for Sample 4
Sample 4 satisfies the constraints of test point .
Constraints
For of the testdata, , .
| Test point ID | Special property | |
|---|---|---|
All are 0. |
||
The last character of is 1, and all other positions are 0. |
||
Translated by ChatGPT 5