luogu#P10765. 「CROI · R2」在相思树下 I

    ID: 10177 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>数学贪心洛谷原创O2优化洛谷月赛

「CROI · R2」在相思树下 I

Background

Year after year under the peach tree,
Watching one become two.
Little wishes often gestured,
"Come true soon, please do!"
Yet unaware of his presence beneath the same tree.
Though "meeting" seems separated by the ends of the earth,
Around the corner, fate brings us together—
Oh, destiny, destiny!

Problem Description

This problem uses multiple test cases.

Xinxin has a sequence from 11 to nn and wants to perform the following two operations:

Operation 1: Delete all elements at odd positions.
Operation 2: Delete all elements at even positions.

After performing kk operations, exactly one number remains. Given the sequence of operations, determine the final remaining number.

Input Format

  • The first line contains a positive integer TT, the number of test cases.
  • For each test case:
    • The first line contains two integers nn and kk.
    • The second line contains kk integers representing the operations (1 for Operation 1, 2 for Operation 2).

Output Format

Output TT lines, each containing the answer for the corresponding test case.

4
5 2
1 1
8 3
2 2 2 
8 3 
1 1 1
8 3
1 2 1
4
1
8
6

Hint

Explanation

Sample Explanation:
For the first test case, the sequence evolves as:
{1,2,3,4,5}{2,4}{4}\{1,2,3,4,5\} \to \{2,4\} \to \{4\}.

Constraints

  • For 30%30\% of the data: n5×105n \leq 5 \times 10^5.
  • For 100%100\% of the data:
    • 1T101 \leq T \leq 10,
    • 1n10181 \leq n \leq 10^{18}.
  • It is guaranteed that exactly one number remains after kk operations for all test cases.