#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 switches that control lights in the room. The switches are numbered , and the lights are numbered .
The rules for how switches affect lights are as follows:
- Pressing switch () toggles light and light .
- If these two indices are the same (that is, ), 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 .
Input Format
The first line contains an integer , the number of testdata groups.
Next come groups of testdata. Each group consists of one line containing two integers and , 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 .
2
4 4
5 5
3
3
Hint
Sample Explanation
In the first group of testdata, there are switches (numbered - ) and lights (numbered - ).
Switch-to-light relationships:
- Switch : controls light (the same light).
- Switch : controls lights and .
- Switch : controls lights and .
- Switch : controls lights and .
One optimal plan is to press switch , switch , and switch , for a total of presses. The state changes are as follows (off , on ):
| State | Controlled Lights | Light | Light | Light | Light |
|---|---|---|---|---|---|
| Initial State | off | off | off | ||
| Press switch | Lights | on | on | ||
| Press switch | Lights | on | off | ||
| Press switch | Lights | on | |||
In the second group of testdata, there are switches (numbered - ) and lights (numbered - ).
Switch-to-light relationships:
- Switch : controls light (the same light).
- Switch : controls lights and .
- Switch : controls lights and .
- Switch : controls lights and .
- Switch : controls lights and .
One optimal plan is to press switch , switch , and switch , for a total of presses. The state changes are as follows:
| State | Controlled Lights | Light | Light | Light | Light | Light |
|---|---|---|---|---|---|---|
| Initial State | off | off | off | |||
| Press switch | Light | on | ||||
| Press switch | Lights | on | ||||
| Press switch | Lights | on | ||||
Constraints
For of the data: , .
For of the data: , .
For of the data: , .
For of the data: , .
Translated by ChatGPT 5