Mousetrap (Large Input)
Time limit5sMemory limit512 MB
In the perfect Mousetrap deck of K cards, report the card value at each queried position.
- Level
Medium7 of 10
- Topics
- Simulation, Implementation, Math
- Solved
- No attempts yet
Problem
Mousetrap is a card game for one player. The deck holds cards numbered 1 through , stacked face down. You reveal the top card of the deck, put that card on the bottom, and keep count of how many cards you have revealed. The first card you reveal is counted as 1. If the number on the revealed card equals the current count, you remove that card from the deck and reset the count, so the next card you reveal is counted as 1 again. If the count reaches , you lose. If the deck runs out of cards, you win.
Play a deck of 5 cards that reads 2, 5, 3, 1, 4 from the top. You reveal the 2 on count 1, the 5 on count 2, and the 3 on count 3. The number matches the count, so you remove the 3 and reset the count. The four remaining cards read 1, 4, 2, 5 from the top. You reveal the 1 on count 1 and remove it as well. Going on the same way you remove the 2, then the 4, then the 5, and you win.
A deck is perfect if playing the game with it wins and the removed cards come out in increasing order 1, 2, , . With 4 cards the deck 1, 4, 2, 3 is perfect, because the cards come out in the order 1, 2, 3, 4. For every there is exactly one perfect deck of size , so the card at each position is determined.
Report which card sits at each of several positions of the perfect deck of size .
Input
The first line contains the number of test cases . Each test case is two lines. The first line contains , the number of cards in the deck. The second line contains an integer , followed by integers , the positions you are asked about. Position 1 is the top card of the deck and position is the bottom card.
The positions inside one test case need not be distinct and need not be sorted.
Output
Print one line for each test case. Print Case #x: first, then integers separated by single spaces. Here is the test case number starting from 1, and is the number of the card at position of the perfect deck of size . Put exactly one space after the colon as well.