#P6616. 环 Rings
环 Rings
Description
You have an -ring puzzle, which has the same structure as the well-known nine-ring puzzle.

The figure above shows a nine-ring puzzle.
As the name suggests, an -ring puzzle has a long bar, with rings on it that affect each other.
We number these rings: the ring closest to the head of the bar is ring , then ring , and so on; the ring closest to the tail of the bar is ring .
Define as the state of ring , where means the ring is on the bar, and means the ring is not on the bar.
Define removing/installing a ring as flipping the ring’s state, i.e. changing the state from to or from to .
The legal rules for removing/installing rings are:
- Ring can always be removed/installed alone.
- Ring can be removed/installed alone if and only if and for all , we have .
- If , then rings and can be removed/installed together.
Now you need to solve the following problem: given the initial state of an -ring puzzle, find the minimum number of legal remove/install operations needed to completely remove the -ring puzzle.
Input Format
The amount of data in this problem is large, so a special input format is used.
The first line contains a positive integer .
The second line contains a positive integer .
The next lines describe the initial state:
Line contains two integers , meaning that we continuously append rings of state to the tail of the initial -ring puzzle.
Output Format
Output one integer, the minimum number of remove/install operations needed to remove the -ring puzzle starting from the given state.
Since the answer may be very large, you only need to output the result modulo .
4
3
1 1
1 0
2 1
7
15
4
5 1
2 1
4 0
4 1
15424
3
3
1 1
1 0
1 1
5
Hint
This problem uses bundled tests, with O2 optimization enabled.
guarantee .
guarantee .
guarantee that there is only one in the initial state.
guarantee .
no special constraints.
For all testdata, , , , .
It is guaranteed that .
Sample #1 Explanation
The sample describes a -ring puzzle with initial state 1101.
One way to finish with the minimum number of legal remove/install operations is:
1. 1101 -> 1100
2. 1100 -> 0100
3. 0100 -> 0111
4. 0111 -> 0110
5. 0110 -> 0010
6. 0010 -> 0011
7. 0011 -> 0000
A total of steps.
Extra Notes
In this problem, the 3rd remove/install rule for the -ring puzzle is not mentioned in most nine-ring puzzle instructions, but it is indeed a real and feasible operation.
Translated by ChatGPT 5
京公网安备 11011102002149号