#P4461. [CQOI2018] 九连环

    ID: 3390 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>搜索2018重庆各省省选快速傅里叶变换 FFT

[CQOI2018] 九连环

Description

Attaching and removing the rings must follow two rules:

  1. The first (rightmost) ring can be attached or removed at any time.

  2. If the kk-th ring is not removed and all rings to the right of the kk-th ring have been removed, then the (k+1)(k+1)-th ring (the ring immediately to the left of the kk-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:

  1. 1101 (according to rule 2, remove the 2nd ring).
  2. 1100 (according to rule 1, remove the 1st ring).
  3. 0100 (according to rule 2, remove the 4th ring).
  4. 0101 (according to rule 1, attach the 1st ring).
  5. 0111 (according to rule 2, attach the 2nd ring).
  6. 0110 (according to rule 1, remove the 1st ring).
  7. 0010 (according to rule 2, remove the 3rd ring).
  8. 0011 (according to rule 1, attach the 1st ring).
  9. 0001 (according to rule 2, remove the 2nd ring).
  10. 0000 (according to rule 1, remove the 1st ring).

Thus, removing the four-ring set requires at least 1010 steps. As the number of rings increases, the required number of steps also increases. For example, removing nine rings requires at least 341341 steps.

Please compute, for nn rings, the minimum number of steps required to remove all rings under the rules.

Input Format

The first line contains an integer mm, the number of test cases.

The next mm lines each contain an integer nn.

Output Format

Output mm lines, each containing the result for the corresponding test case.

3
3
5
9
5
21
341

Hint

For 10% of the testdata, 1n101 \le n \le 10.

For 30% of the testdata, 1n301 \le n \le 30.

For 100% of the testdata, 1n1051 \le n \le 10^5, 1m101 \le m \le 10.

Translated by ChatGPT 5