#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 k(k2)k(k\ge 2)-th ring can be put onto the “sword” (denoted as 11) or taken off the “sword” (denoted as 00) if and only if the (k1)(k-1)-th ring is on the sword, and all rings before it are not on the sword. In particular, the 11-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 ss and the state string tt, satisfying s=t1|s|=|t|-1. Here, ti=1t_i=\texttt 1 means the ii-th ring is on, and ti=0t_i=\texttt 0 means the ii-th ring is off.

Within the same game, ss never changes, while in each step tt 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 tit_i are 0. The simple nine linked rings is fully put on if and only if all tit_i are 1.

The rule of the simple nine linked rings is: tit_i is allowed to change if and only if t1i1t_{1\sim i-1} is a suffix of ss. It can be seen that the traditional nine linked rings is a special case where ss is 00...01.

Given ss, 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 109+710^9+7.

Input Format

The first line contains an integer nn, the length of ss. Note that this is not the number of rings.

The second line contains a 01 string ss.

Output Format

One line containing an integer, the answer modulo 109+710^9+7.

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 tt is 0000.

In step 1, put on the 11-st ring, and tt becomes 1000.

In step 2, put on the 22-nd ring, and tt becomes 1100.

In step 3, put on the 33-rd ring, and tt becomes 1110.

Next, you cannot directly put on the 44-th ring, because 111 is not a suffix of the rule string ss 011. Therefore, in step 4 you should take off the 11-st ring, and tt becomes 0110.

Then in step 5, put on the 44-th ring, and tt becomes 0111.

In the last step, put on the 11-st ring, and tt becomes 1111, completing the goal.

Explanation for Sample 2

This is the traditional nine linked rings, and there are exactly 99 rings.

Explanation for Sample 3

Sample 3 satisfies the constraints of test point 77.

Explanation for Sample 4

Sample 4 satisfies the constraints of test point 1515.

Constraints

For 100%100\% of the testdata, 1n20001\le n\le 2000, si{0,1}s_i\in\{\texttt 0,\texttt 1\}.

Test point ID s\vert s\vert\le Special property
131\sim 3 33
464\sim 6 1515
7117\sim 11 300300
121312\sim 13 10001000
1414 20002000 All sis_i are 0.
151715\sim 17 The last character of ss is 1, and all other positions are 0.
182518\sim 25

Translated by ChatGPT 5