#P4461. [CQOI2018] 九连环
[CQOI2018] 九连环
Description
Attaching and removing the rings must follow two rules:
-
The first (rightmost) ring can be attached or removed at any time.
-
If the -th ring is not removed and all rings to the right of the -th ring have been removed, then the -th ring (the ring immediately to the left of the -th ring) can be attached or removed arbitrarily.
Unlike the Rubik's Cube with its myriad variations, the optimal strategy for the Nine Linked Rings is unique. For simplicity, we take the four-ring case as an example to demonstrate the process. Here, 1 means the ring is on the "sword", and 0 means the ring has been removed.
The initial state is 1111, and the operations at each step are as follows:
- 1101 (according to rule 2, remove the 2nd ring).
- 1100 (according to rule 1, remove the 1st ring).
- 0100 (according to rule 2, remove the 4th ring).
- 0101 (according to rule 1, attach the 1st ring).
- 0111 (according to rule 2, attach the 2nd ring).
- 0110 (according to rule 1, remove the 1st ring).
- 0010 (according to rule 2, remove the 3rd ring).
- 0011 (according to rule 1, attach the 1st ring).
- 0001 (according to rule 2, remove the 2nd ring).
- 0000 (according to rule 1, remove the 1st ring).
Thus, removing the four-ring set requires at least steps. As the number of rings increases, the required number of steps also increases. For example, removing nine rings requires at least steps.
Please compute, for rings, the minimum number of steps required to remove all rings under the rules.
Input Format
The first line contains an integer , the number of test cases.
The next lines each contain an integer .
Output Format
Output lines, each containing the result for the corresponding test case.
3
3
5
9
5
21
341
Hint
For 10% of the testdata, .
For 30% of the testdata, .
For 100% of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号