#P16263. [蓝桥杯 2026 省 Python B 组] 密室逃脱开关谜题

[蓝桥杯 2026 省 Python B 组] 密室逃脱开关谜题

Problem Description

You are trapped in an escape room. In front of you is a control panel with nn switches that control mm lights in the room. The switches are numbered 0,1,…,n−10, 1, \dots, n - 1, and the lights are numbered 0,1,…,m−10, 1, \dots, m - 1.

The rules for how switches affect lights are as follows:

  • Pressing switch ii (0≤i<n0 \leq i < n) toggles light (i mod m)(i \bmod m) and light (2×i mod m)(2 \times i \bmod m).
  • If these two indices are the same (that is, i mod m=2×i mod mi \bmod m = 2 \times i \bmod m), then only this one light is toggled.
  • Toggling means: if a light is off, it becomes on; if it is on, it becomes off.

Initially, all lights are off. You may press switches any number of times. Each switch may be pressed multiple times (or not pressed at all).

Your goal is to make all lights end up on. Now, compute the minimum number of switch presses needed to achieve this. If it is impossible to make all lights on at the same time no matter what you do, output −1-1.

Input Format

The first line contains an integer tt, the number of testdata groups.

Next come tt groups of testdata. Each group consists of one line containing two integers nn and mm, representing the number of switches and the number of lights.

Output Format

For each group of testdata, output one line containing an integer, the minimum number of switch presses. If it is impossible to turn on all lights, output −1-1.

2
4 4
5 5
3
3

Hint

Sample Explanation

In the first group of testdata, there are 44 switches (numbered 00 - 33) and 44 lights (numbered 00 - 33).

Switch-to-light relationships:

  • Switch 00: controls light 00 (the same light).
  • Switch 11: controls lights 11 and 22.
  • Switch 22: controls lights 22 and 00.
  • Switch 33: controls lights 33 and 22.

One optimal plan is to press switch 11, switch 22, and switch 33, for a total of 33 presses. The state changes are as follows (off =0= 0, on =1= 1):

State Controlled Lights Light 00 Light 11 Light 22 Light 33
Initial State off off off
Press switch 11 Lights 1,21,2 on on
Press switch 22 Lights 2,02,0 on off
Press switch 33 Lights 3,23,2 on

In the second group of testdata, there are 55 switches (numbered 00 - 44) and 55 lights (numbered 00 - 44).

Switch-to-light relationships:

  • Switch 00: controls light 00 (the same light).
  • Switch 11: controls lights 11 and 22.
  • Switch 22: controls lights 22 and 44.
  • Switch 33: controls lights 33 and 11.
  • Switch 44: controls lights 44 and 33.

One optimal plan is to press switch 00, switch 11, and switch 44, for a total of 33 presses. The state changes are as follows:

State Controlled Lights Light 00 Light 11 Light 22 Light 33 Light 44
Initial State off off off
Press switch 00 Light 00 on
Press switch 11 Lights 1,21,2 on
Press switch 44 Lights 4,34,3 on

Constraints

For 20%20\% of the data: 1≤t≤31 \leq t \leq 3, 1≤n,m≤81 \leq n, m \leq 8.

For 50%50\% of the data: 1≤t≤31 \leq t \leq 3, 1≤n,m≤201 \leq n, m \leq 20.

For 80%80\% of the data: 1≤t≤51 \leq t \leq 5, 1≤n,m≤1001 \leq n, m \leq 100.

For 100%100\% of the data: 1≤t≤51 \leq t \leq 5, 1≤n,m≤10001 \leq n, m \leq 1000.

Translated by ChatGPT 5