#P3098. [USACO13DEC] The Bessie Shuffle G
[USACO13DEC] The Bessie Shuffle G
Description
Bessie has a unique shuffling method, called the A-shuffle (also called the Bessie-shuffle).
A-shuffle: Take a stack of () cards labeled from top to bottom . Move the -th card from the top to position from the top.
For example, , after performing one A-shuffle, from top to bottom it becomes , i.e., card goes to position , card goes to position , card goes to position .
Now Bessie practices another method, called the B-shuffle.
B-shuffle process:
There is a stack of () cards labeled , initially stacked in order from top to bottom to . There is also an auxiliary stack used during shuffling, called the temporary pile, which starts empty.
- Apply one A-shuffle to the top cards.
- Move the top card to the top of the temporary pile, face down.
- Repeat the previous two operations until the original pile is empty.
During this process, when the original pile has fewer than cards remaining, do not perform the A-shuffle; instead, keep moving the top card to the temporary pile one by one.
Given , and the permutation , there are () queries. After performing one B-shuffle, for each query, find the label of the card at position in the temporary pile.
Constraints:
- .
- .
- .
- .
- .
- of test cases have .
Input Format
- Line 1: A single line containing , and separated by a space.
- Lines 2..1+M: Line indicates the position from the top, , of the -th card in the Bessie-shuffle ().
- Lines 2+M..1+M+Q: Line contains a single integer describing the -th query. You are to compute the label on the card located in position from the top ().
Output Format
- Lines 1..Q: On line , print a single integer indicating the card at position from the top.
5 3 5
3
1
2
1
2
3
4
5
4
5
3
1
2
Hint
Bessie has a deck of 5 cards initially ordered as [1, 2, 3, 4, 5]. Her shuffle is on 3 cards and has the effect of moving the top card to the bottom. There are 5 queries, one for each position in the deck.
The shuffle proceeds as:
[1, 2, 3, 4, 5] -> [2, 3, 1, 4, 5] (put 2 face down)
[3, 1, 4, 5] -> [1, 4, 3, 5] (put 1 face down)
[4, 3, 5] -> [3, 5, 4] (put 3 face down)
[5, 4] (put 5 face down)
[4] (put 4 face down)
This produces the final order of [4, 5, 3, 1, 2].
Translated by ChatGPT 5
京公网安备 11011102002149号