#P6616. 环 Rings

环 Rings

Description

You have an nn-ring puzzle, which has the same structure as the well-known nine-ring puzzle.

This is a nine-ring puzzle

The figure above shows a nine-ring puzzle.

As the name suggests, an nn-ring puzzle has a long bar, with nn rings on it that affect each other.

We number these nn rings: the ring closest to the head of the bar is ring 11, then ring 22, and so on; the ring closest to the tail of the bar is ring nn.

Define fif_i as the state of ring ii, where fi=1f_i = 1 means the ring is on the bar, and fi=0f_i = 0 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 11 to 00 or from 00 to 11.

The legal rules for removing/installing rings are:

  1. Ring 11 can always be removed/installed alone.
  2. Ring k+1k+1 can be removed/installed alone if and only if fk=1f_k = 1 and for all i<ki < k, we have fi=0f_i = 0.
  3. If f1=f2f_1 = f_2, then rings 11 and 22 can be removed/installed together.

Now you need to solve the following problem: given the initial state of an nn-ring puzzle, find the minimum number of legal remove/install operations needed to completely remove the nn-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 nn.

The second line contains a positive integer mm.

The next mm lines describe the initial state:

Line i+2i + 2 contains two integers leni,stilen_{i}, st_{i}, meaning that we continuously append lenilen_{i} rings of state stist_{i} to the tail of the initial nn-ring puzzle.

Output Format

Output one integer, the minimum number of remove/install operations needed to remove the nn-ring puzzle starting from the given state.

Since the answer may be very large, you only need to output the result modulo 12012012011201201201.

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.

Subtask 1 (10 pts):\text{Subtask 1 (10 pts)}: guarantee 1n201 \le n \le 20.

Subtask 2 (15 pts):\text{Subtask 2 (15 pts)}: guarantee 1n10001 \le n \le 1000.

Subtask 3 (15 pts):\text{Subtask 3 (15 pts)}: guarantee that there is only one 11 in the initial state.

Subtask 4 (30 pts):\text{Subtask 4 (30 pts)}: guarantee 1n1071 \le n \le 10^7.

Subtask 5 (30 pts):\text{Subtask 5 (30 pts)}: no special constraints.

For all testdata, 1n10181 \le n \le 10^{18}, 1m1051 \le m \le 10^5, sti{0,1}st_{i} \in \{0, 1\}, leni1len_{i} \ge 1.

It is guaranteed that i=1mleni=n\sum\limits_{i=1}^m len_{i} = n.


Sample #1 Explanation

The sample describes a 44-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 77 steps.


Extra Notes

In this problem, the 3rd remove/install rule for the nn-ring puzzle is not mentioned in most nine-ring puzzle instructions, but it is indeed a real and feasible operation.

Translated by ChatGPT 5