1507.Strange clocks

Time Limit: 1s Memory Limit: 256MB

Welcome to the Time Management Bureau !

We need to calibrate some clocks that are affected by time floods. There is a row of $n$ clocks to be calibrated here, with the scale on each clock starting from $0$ and placed clockwise at $1,2$...... Each clock provides two numbers $x$ and $y$ to indicate that it needs to rotate $x$ times to complete one revolution and has already completed $y$ rotations from the $0$ scale.

Ypr9f78PQVdDXlfl7jPD2PZJCfS2hNZLgqIiDN52.jpg

We have the three calibration operations:

  • For each $i\in[1,n]$, let $y_{i} = [y_{i} \& (y_{i} + 1)] \mod x_{i} $.
  • For each $i\in[1,n]$, let $y_{i} = [y_{i} \oplus (y_{i} + 1)] \mod x_{i}$.
  • For each $i\in[1,n]$, let $y_{i} = (y_{i} + 1) \mod x_{i}$.

Here & denotes the bitwise AND operation, and $\oplus$ denotes the bitwise XOR operation.

523KKFvjrIVA1yUn1FhjfH3RmptS2mEWI772N6tv.jpg

The Time Management Bureau needs to calibrate these n clocks to the correct scale through several operations. But to save time, please calculate the minimum number of operations to calibrate these n clocks to the correct scale.

Output $-1$ when there is no solution.

Input Format(From the terminal/stdin)

The first line contains one positive integer $n (1 \le n \le 40)$.

The second line contains $n$ positive integers separated by spaces to represent the correct scale sequence, ensuring that the sequence is legal.

The next n lines, each contains two positive integers $x (2 \le x \le 4)$ and $y (0\le y\le x-1)$ separated by spaces.

Output Format(To the terminal/stdout)

The output contains an integer representing the minimum number of operations to calibrate all clocks.

Sample Input

Copy
5
1 1 1 1 1
2 0
2 1
4 3
3 2
3 2
 \n
 · · · · \n
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output

Copy
2
 \n

Hints

In the first test case, let's first perform operation $1$ and then operation $2$:

Scale sequence after completing operation $1$: $0,0,0,2,2$

Scale sequence after completing operation $2$: $1,1,1,1,1$

Source: The 2024 Hunan University Programming Contest

Submit

请先 登录

© 2025 FAQs