We need to parse multiple datasets: each dataset: line with n and r. Then r lines each containing a c (one integer per line, presumably). We need to simulate riffle shuffle r times, each with given c. Then output the top card after all shuffles. Cards are numbered 0..n-1 from bottom to top. Initially deck order: bottom is 0, top is n-1. So we can treat list where index 0 is bottom, index n-1 is top. After each operation, we produce new deck order. Then after all, output the top card i.e., last element if we maintain bottom->top list.
But careful: The description says "top" and "bottom". In the example: deck [0 1 2 3 4 5 6 7 8] (right is top). They wrote that as list with rightmost top. So we can use list where last element is top. In step 0: deck A [4 5 6 7 8] (top half? Actually they said deck A is top half, deck B bottom half. So they split: deck A (top half) has top half of cards: for n=9, top half = top half: cards from index n/2 upwards? Actually they said: "First, we divide it into two decks; deck A which consists of the top half of it and deck B of the bottom half. Deck A will have one more card when n is odd." So deck A contains the top half, meaning the cards originally near the top. In initial deck [0 1 2 3 4 5 6 7 8] (right top). So top half: the higher-indexed cards: {4,5,6,7,8}? Actually indices: if we list bottom to top: 0,1,2,3,4,5,6,7,8 (position 0 bottom, position 8 top). Top half (top half of deck) would be the cards closer to the top: with indices from ceil(n/2) to n-1? For n=9, top half size = 5 (one more), bottom half size = 4. So deck A (top half) should contain cards {4,5,6,7,8} (the ones at the top positions). Indeed in step 0 they list deck A [4 5 6 7 8] and deck B [0 1 2 3]. In their representation they list deck A as [4 5 6 7 8] and deck B [0 1 2 3] presumably with right as top. But note: In step 1 they pull c cards from bottom of deck A and bottom of deck B. The "bottom" of a deck is the side opposite to its top. For deck A [4 5 6 7 8] (right top), bottom is leftmost: 4. They pull c cards from bottom: means take from the bottom of the deck (the side opposite to the top). So they take the leftmost elements. In step 1: c=3. Deck A bottom has three cards: 4,5,6? Actually deck A initially: [4,5,6,7,8] (if we treat as list with index 0 bottom, index 4 top). Taking bottom 3 cards means taking the first 3 elements: [4,5,6]. They put them on deck C (empty). After pulling, deck A becomes [7,8] (the remaining top cards). Then from deck B bottom: deck B [0,1,2,3] bottom 3 cards: [0,1,2] taken, deck B becomes [3]. Add to deck C, but note they "stacked on deck C". They seem to be adding to deck C presumably by placing them on top? Let's see: after step 2: deck C [4 5 6 0 1 2]. That suggests after first pull from A, deck C has [4,5,6]. Then after pull from B, they add [0,1,2] on top of deck C? Actually if deck C is built such that we start with empty, then we take cards from bottom of A and "stack" them on deck C. What does "stack" mean? Typically when you take a pile of cards and place them onto another deck, you might be placing them on top (so the order of those cards relative to each other may be preserved or reversed? Need to check.
In step 1: after pulling bottom 3 from A, we have A [7,8], and we put those 3 onto deck C. If we place them onto deck C as a stack, we need to decide orientation. Then step 2: after pulling bottom 3 from B, deck B becomes [3], and we stack those onto deck C. The final deck C after step 4 is [4 5 6 0 1 2 7 8 3]. Let's trace the process more carefully to deduce stacking order.
Initial deck: [0 1 2 3 4 5 6 7 8] (right top). So bottom is 0, top is 8.
Step 0: Split: Deck A (top half) = [4 5 6 7 8] (still bottom? Actually if we maintain representation as list with index 0 bottom, then deck A's bottom is the card that was closest to the middle? In original deck, after splitting, we take the top half as a separate pile. How is that pile oriented? The deck A contains cards that were the top of original deck, but the pile itself is still a deck: its bottom should be the card that was originally just above the split? Actually if you take the top half of a deck as a separate pile, you might pick up that half and keep its orientation: the top of that half remains the top of the original deck, and the bottom of that half is the card that was just above the split point. So orientation remains consistent: the ordering from bottom to top remains same as originally. So if original deck bottom to top: [0,1,2,3,4,5,6,7,8], and we split at index 4? Top half: indices 4..8 inclusive (size 5). So deck A bottom = index 4 (value 4), top = index 8 (value 8). So deck A as list bottom->top: [4,5,6,7,8]. Similarly deck B bottom->top: indices 0..3: [0,1,2,3].
Now, the riffle operation: "c cards are pulled from bottom of deck A and are stacked on deck C, which is empty initially. Then c cards are pulled from bottom of deck B and stacked on deck C, likewise. This operation is repeated until deck A and B become empty. When the number of cards of deck A(B) is less than c, all cards are pulled."
Interpret: At each iteration, we take exactly c cards from bottom of A (unless less, then take all) and place them onto deck C. Then similarly from bottom of B. And we repeat until A and B are empty. The term "stacked on deck C" likely means we add these cards onto the top of deck C, preserving the order relative to each other? But careful: If we take a group of cards from the bottom of a deck, we are taking them in order from bottom upward? Actually if we pull from the bottom, we remove the bottommost card first? Or do we pull them as a block? The description: "c cards are pulled from bottom of deck A and are stacked on deck C". It might mean we take the bottom c cards as a chunk and place them onto deck C, but the orientation might matter: if we take from bottom and then place onto deck C, do we place them as a block such that the former bottom card becomes the bottom of the new addition? Or we might be flipping? Let's look at example:
Deck A: [4,5,6,7,8] (bottom->top)
Deck B: [0,1,2,3]
c=3.
Step 1: pull 3 cards from bottom of A. That would be cards 4,5,6 (the bottom three). Remove them from A, leaving A as [7,8] (bottom 7, top 8). Now stack these pulled cards onto deck C. Deck C initially empty. After stacking, deck C should have these cards. In the final deck C after all steps, we have [4,5,6,0,1,2,7,8,3]. Notice that the first three cards in the final deck (from left to right in their representation, presumably bottom->top?) Their representation: [4 5 6 0 1 2 7 8 3] with right as top? They state "shuffled deck [4 5 6 0 1 2 7 8 3]". In that representation, rightmost is top. So bottom is leftmost. So final deck bottom->top order is 4,5,6,0,1,2,7,8,3. So the bottommost card after shuffle is 4, then 5,6, then 0,1,2, then 7,8, then 3 on top.
Now, let's see how that order emerges from stacking. After step 1, if we place the three cards from A onto deck C, what would deck C be? Since deck C is initially empty, after stacking, it must become something. Then step 2: pull 3 from bottom of B: B bottom cards are 0,1,2. After removal, B becomes [3]. Then stack these onto deck C. After step 2, we have deck C [4 5 6 0 1 2] according to step 2 in the example. So deck C after step 2 has bottom->top: 4,5,6,0,1,2? Actually they wrote: "deck C [4 5 6 0 1 2]". In representation, likely right is top? They didn't specify for deck C orientation, but presumably it's consistent: right is top. So deck C bottom->top = [4,5,6,0,1,2] with top=2. Then step 3: pull c from bottom of A? But A has [7,8] and c=3, but since A has less than c, we pull all cards: all of A: 7 and 8. Then stack onto deck C. After step 3, deck C becomes [4 5 6 0 1 2 7 8] (they show: "deck C [4 5 6 0 1 2 7 8]"). So that is the bottom->top order: 4,5,6,0,1,2,7,8 with top=8. Then step 4: pull all from B (which has [3]) and stack onto deck C, resulting [4 5 6 0 1 2 7 8 3].
Now, how are the cards stacked? In step 1, we placed [4,5,6] onto empty deck C. Then in step 2, we placed [0,1,2] onto deck C, and they appear after the previous ones, i.e., to the right? Actually if deck C's representation right is top, then initially after step1, deck C would have something. Then after step2, we add [0,1,2] onto deck C, and the new deck C becomes [4 5 6 0 1 2] - meaning that the newly added cards appear to the right (top) of existing ones. So stacking onto deck C means placing the pulled chunk onto the top of deck C. However, we need to check the order of cards within the chunk: Are they placed in the same order as they were taken from the source deck? From step1: we took bottom of A: 4,5,6. When we placed them onto deck C (which was empty), the resulting deck C had these three cards: the order in the final deck from bottom to top is 4 then 5 then 6. That means that the bottommost card among those three in deck C is 4, which was the first card we would have taken if we pulled from bottom one by one? If we pull from bottom of deck A, the bottommost card (4) is the first removed. If we then place it onto deck C, how do we place it? Typically if you take a card from the bottom of a deck and put it on top of another deck, that card would become the new top. But here we are taking a block of c cards. The description: "c cards are pulled from bottom of deck A and are stacked on deck C." The phrase "stacked on" suggests that we are placing them onto deck C, possibly preserving the relative order. In many shuffles, when you pull a packet from the bottom and put it onto the top, the order of the packet may be reversed? Let's think.
If we have deck A with bottom->top: [4,5,6,7,8]. The bottom of deck A is 4. If we take one card from bottom and place it onto deck C (which initially empty), and then place it, if we put it on top, then deck C would have that card on top. If we continue pulling bottom cards one by one and stacking each on top of deck C, the order would be reversed: first pull 4, place on empty => deck C bottom->top = [4]; then pull 5, place on top => deck C = [4,5]? Actually if we put on top, then after placing 5, the top becomes 5, bottom stays 4 => deck C = [4,5] (bottom->top) where top is 5. That would preserve the order 4 then 5? Actually initial order in A bottom to top: 4,5,6. If we take them one by one from bottom and push onto top of C, the first card (4) goes onto empty => top=4. Then second card (5) goes on top => now order: 4 at bottom, 5 on top. So bottom->top = [4,5]. Then third (6) on top => [4,5,6]. So the order is the same as original bottom->top. So taking from bottom and placing onto top one by one yields same sequence. But if we take the whole block as a unit and place it onto top, we have to decide how to orient the block. If we take the block of three cards as a group from bottom (i.e., we cut the deck A at a point c cards from bottom, producing a packet consisting of those bottom c cards, with their original order relative to each other: the bottommost card is at one end of the packet and the topmost of the packet is the c-th card from bottom). If we then place this packet onto the top of deck C, we need to specify which side of the packet becomes the new top. Usually, if you take a packet from the bottom of a deck and put it onto the top of another deck without flipping, the cards that were at the bottom of the packet (the ones that were originally lowest) would end up at the bottom of the packet on top, so the relative order within the packet remains the same as it was in the original deck. But when you place the packet onto the top, the top of the packet becomes the new top of the combined deck. The packet originally: bottommost card (4) is at one end, and the topmost of the packet (6) is at the other end (since they were taken from the bottom, the packet orientation: if you lift the bottom c cards as a stack, you might flip them? Actually think of physical cards: A deck with bottom on table, top facing up. If you take a packet from the bottom, you slide them out. The orientation: the bottommost card of the deck becomes the bottom of the packet? Or does it become the top? This depends on how you grip the packet. Usually, if you take a packet from the bottom by sliding them out and then placing them on top, you might not flip them; the bottommost card ends up being on the bottom of the packet, and when placed on top, that bottommost card becomes just above the previous top? Actually, imagine a deck of cards in your hand, with top card facing you. Bottom card is at the far end. To take a packet from the bottom, you might grasp the packet from the bottom and move it to the top. In that process, the packet might be turned over? Not necessarily. There's a known shuffle called "riffle shuffle" where you split into two halves and interleave. But this operation is different: it's a type of shuffle described. According to the example, the order of the packet after being placed onto C is such that the sequence within the packet appears in the same order as they were taken from the bottom (i.e., 4,5,6). In step 2, when pulling from B bottom: B originally bottom->top = [0,1,2,3]; pulling bottom 3 yields [0,1,2]. After placing onto C, they appear after the previous cards (i.e., to the right, meaning on top), and the order is 0,1,2. So indeed the order within each pulled block remains as they were in the source deck from bottom to top. So that suggests that when pulling from the bottom, we take the block of c cards in their order (bottommost first) and then we place them onto the top of deck C, preserving the order. So the resulting deck C will have the blocks appended to the top in the order they are taken.
Thus the algorithm for one riffle operation given c:
Input: a deck list L (bottom->top). We need to split into two decks A (top half) and B (bottom half). Specifically, split such that A gets the top half (cards originally nearer the top) and B gets the bottom half. The split point: let sizeA = ceil(n/2), sizeB = n - sizeA. L[0..sizeB-1] is bottom half? Actually if L is bottom->top (index0 bottom, index n-1 top), then the bottom half (original lower half) are the first sizeB elements? Wait: The bottom half of original deck are the cards near the bottom, i.e., indices 0..sizeB-1 if sizeB = floor(n/2). The top half are indices sizeB..n-1 (sizeA = ceil(n/2)). So deck B = bottom half = L[0:sizeB] (if we use Python slicing inclusive of start exclusive of end: L[0:sizeB]), and deck A = top half = L[sizeB:]. But careful: In the example, L = [0,1,2,3,4,5,6,7,8], sizeB = floor(9/2)=4, sizeA=5. So deck B should be [0,1,2,3] (bottom half), deck A = [4,5,6,7,8] (top half). Yes.
Now for the riffle operation:
We have deck A (top half) and deck B (bottom half). Both are lists bottom->top. The operation: repeatedly do:
- Take min(c, len(A)) cards from the bottom of A. That is, take the first (bottom) elements: A[0:k] (where k = min(c, len(A))). Remove them from A. This block is blockA. Then "stack" blockA onto deck C: that means we append blockA to the top of deck C? But careful: deck C is built from scratch each operation. Initially C empty (list). At the end, C becomes the new deck. The stacking operation: we need to place blockA onto deck C. In the example, after taking blockA (size 3) from A, and placing onto empty C, C became [4,5,6] (bottom->top). So it seems that we are adding blockA to the bottom of C? Wait: if we think of building C by successive stacking: initially C empty. Then we take blockA from A and stack onto C. The phrase "stacked on deck C" might mean that we place the block onto the top of C. But if C is empty, then any placement yields that block as C. However, after step1, we need to know the orientation: C = [4,5,6] (presumably bottom 4, top 6). In step2, we take blockB from B (size 3) and stack onto C, and result becomes [4,5,6,0,1,2]. If we are stacking onto the top of C, then the existing C's top was 6, and we place blockB on top of it, so the new top becomes the last card of blockB. That would result in order: bottom: 4,5,6, then on top of those we put 0,1,2? But does that produce the sequence [4,5,6,0,1,2] as bottom->top? If we represent deck as list with index 0 bottom, index end top, then if we start with C = [4,5,6] (bottom to top). Then we want to add blockB = [0,1,2] on top. That yields new list: [4,5,6,0,1,2] indeed. So stacking onto the top means appending blockB to the end of the list (since top is end). That matches: after step1, C = [4,5,6] (i.e., we appended blockA). After step2, we appended blockB. After step3, we appended blockA (remaining [7,8]), after step4 appended blockB ([3]) resulting in [4,5,6,0,1,2,7,8,3]. So indeed stacking onto C means appending the block to the top, i.e., adding to the end of list (since top is last). But what about the orientation of block? The block taken from bottom of A: we take A[0:k] (the bottommost cards). In our list representation, A[0] is bottom, A[-1] top. Taking from bottom means taking the first elements of list A. In step1, A = [4,5,6,7,8]; we take k=3 from bottom: that yields [4,5,6] (the first three). That's exactly blockA. Then we append that list to C (which initially empty). So C becomes [4,5,6] (bottom->top order: 4 bottom, 6 top). Then we take blockB: B = [0,1,2,3]; take bottom 3: [0,1,2]; append to C yields [4,5,6,0,1,2]. Then A left: [7,8]; take all: [7,8]; append -> [4,5,6,0,1,2,7,8]; B left: [3]; take all: [3]; append -> [4,5,6,0,1,2,7,8,3]. That matches the example exactly.
Thus algorithm: For each riffle operation with parameter c:
Given deck L (list bottom->top) of length n:
-
Split L into A and B:
sizeB = n // 2 (floor(n/2))
sizeA = n - sizeB
A = L[sizeB:] (top half)
B = L[:sizeB] (bottom half)
But careful: In example, n=9: sizeB = 4, sizeA = 5. A = L[4:] = [4,5,6,7,8]; B = L[:4] = [0,1,2,3] correct.
-
Initialize C = [] (empty list).
-
While A or B not empty:
- Take k = min(c, len(A)) cards from bottom of A: this is the first k elements of list A (since bottom is start). Remove them from A (pop from front). But more efficiently, we can use list indexing and slicing; but n <= 50, so performance fine.
- Append these cards to C (preserving order, i.e., extend C with that block)
- Then similarly for B: take k2 = min(c, len(B)) from bottom (front), remove from B, extend C with that block.
-
After loop, deck becomes C (new L for next operation).
After all operations, the top card is the last element of L (since index -1). Output that number.
Edge cases: When c may be larger than n? Possibly but c is positive integer? The problem statement says "r more positive integers follow, each of which represents a riffle operation." It doesn't specify a bound, but likely c can be up to maybe n? But the operation is defined: pull c cards from bottom; if less than c, pull all. So c can be any positive integer, but in practice the effect when c>= max(len(A), len(B)): then we just take all A first then all B, leading to a specific order. That's fine. We'll implement as described.
Multiple data sets until EOF. Input sample:
9 1
3
9 4
1 2 3 4
Note: In second dataset, after "9 4" there are four integers, each on separate lines: 1, 2, 3, 4. So we need to read r lines.
Implementation plan:
Read all lines from stdin, parse integers. But careful: input may be separated by whitespace, not necessarily one per line? The problem description: "r more positive integers follow, each of which represents a riffle operation." They show each on separate line, but could be whitespace separated? Usually in programming contest problems, they are given each on its own line but we should handle generic whitespace separation. Safer to read token by token. We'll read all ints from stdin, then process sequentially? But datasets are separated by lines with n and r, then r integers. But if we read token by token, we can parse: while there are tokens: read n, r; if EOF break. Then read r integers. That works.
We'll implement simulation using Python lists.
Check: Are there any subtle aspects? The split: A is top half, B bottom half. In the description: "First, we divide it into two decks; deck A which consists of the top half of it and deck B of the bottom half. Deck A will have one more card when n is odd." So indeed A gets the larger half if odd. So sizeA = (n+1)//2, sizeB = n//2.
Now, note: In the riffle operation, after splitting, we pull c cards from bottom of A and then c from bottom of B, and repeat. Important: The order of pulling: first from A, then from B. That's consistent.
Now, test with the example: n=9, r=1, c=3. Simulation should produce top=3, as output.
Second dataset: n=9, r=4, c values: 1,2,3,4. Output: 0. Let's test quickly mentally maybe confirm.
We'll implement and test with given example.
But let's also think about potential pitfalls: The operation of pulling from bottom: we need to take from front of list if we maintain bottom at index 0. When we remove from A, we can do: block = A[:k]; then A = A[k:]; similarly for B. Since n is small, it's fine.
Alternatively, we can simulate using collections.deque for efficient popleft, but list slicing is okay.
We need to be careful: After each operation, the deck becomes C, which is a list with bottom at index 0, top at end.
Now, answer is the top card after all operations: L[-1].
Now, implement.
Pseudo:
import sys
def riffle(deck, c):
n = len(deck)
sizeB = n // 2
sizeA = n - sizeB
A = deck[sizeB:] # top half
B = deck[:sizeB] # bottom half
C = []
while A or B:
k = min(c, len(A))
if k:
block = A[:k]
A = A[k:]
C.extend(block)
k = min(c, len(B))
if k:
block = B[:k]
B = B[k:]
C.extend(block)
return C
def solve():
data = list(map(int, sys.stdin.read().split()))
i = 0
out_lines = []
while i < len(data):
n = data[i]; r = data[i+1]; i += 2
if n == 0 and r == 0? Not mentioned. Just EOF.
deck = list(range(n)) # bottom 0, top n-1
for _ in range(r):
c = data[i]; i += 1
deck = riffle(deck, c)
top = deck[-1]
out_lines.append(str(top))
sys.stdout.write("\n".join(out_lines))
But need to ensure we don't run out of data: When reading, we must have exactly r integers after each dataset. The total tokens should be even number? Actually each dataset contributes 2 + r tokens. We'll guard by while i + r <= len(data) maybe? But it's safe if input is well-formed; we'll just trust.
Potential problem: There might be blank lines; reading all ints handles.
Edge case: n=1: then split: sizeB = 0, sizeA = 1. A = deck[0:] = [0]; B = [] empty. Then while loop: take min(c,1) from A, append to C, then from B (none). Then A becomes empty, B empty. So C = [0]. So deck unchanged. top = 0.
Test with n=1, r=any, output 0.
Now, check the second example: n=9, r=4, c=1,2,3,4.
We'll simulate quickly to see if top becomes 0.
We'll write a small test in mind or using code later, but trust.
Now, complexity: n<=50, r<=50, number of datasets <100, trivial.
But ensure the split is correct: In the example, they said deck A is top half. That is A = top half, B = bottom half. So we used A = deck[sizeB:] (top half), B = deck[:sizeB] (bottom half). In the operation, we first take from bottom of A (which corresponds to the cards that were originally just above the split? Actually bottom of A is the card that was at the split point? That's fine.
Now, confirm with example: A = [4,5,6,7,8] bottom=4, top=8. First pull from A bottom: we took [4,5,6] correctly.
So algorithm is correct.
Now, double-check the step "c cards are pulled from bottom of deck A and are stacked on deck C". The phrase "stacked on" could be interpreted as placing them on top of deck C, which we implemented by extend (append to end). But could it be placing them on the bottom of deck C? Let's test alternative: If we placed them onto the bottom of C (i.e., prepend), then after step1, C would be [4,5,6] if we prepend? Actually prepending the block would put them at the front of list, which would be bottom of C. In step2, we would prepend block from B? But the order in final deck would be different. Let's see: If we always prepend (i.e., place at bottom) then after step1, C = [4,5,6] (prepending to empty is same as append). But after step2, if we prepend [0,1,2] to C, then C becomes [0,1,2,4,5,6] (since prepend block at front). That is not the given intermediate step (they had [4,5,6,0,1,2]). So stacking onto top (append) is correct.
Now, about the order of pulling: we alternate A then B, but after one of them becomes empty, we only pull from the other. That's what we do.
Now, potential nuance: What about when after pulling from A, if A becomes empty, we still pull from B if B not empty? Yes, because loop continues. In our while loop condition: while A or B: we always attempt to pull from A (if A non-empty) then from B (if B non-empty). In case A empty but B not, then first min(c,len(A)) = 0, so we do nothing from A, then we pull from B. That's correct.
Similarly if B empty but A not: we pull from A and then from B (which will be zero). So it's fine.
Now, check for scenario when c is large: e.g., c=100, n=9. A has 5, B 4. First iteration: kA = min(100,5)=5, we take all A, then kB = min(100,4)=4, take all B. So C = A_all + B_all = top half + bottom half = [4,5,6,7,8,0,1,2,3]. That would be a cut (moving top half to bottom? Actually order: bottom half? Let's see: original deck bottom->top: [0..8]. After this operation, C = [4,5,6,7,8,0,1,2,3]; top is 3. That seems like a "cut" that moves the top half to the bottom? Actually it's like taking top half and moving it to bottom? Wait: original: [0,1,2,3,4,5,6,7,8]. After operation: [4,5,6,7,8,0,1,2,3]. That is effectively a cut at the half: the top half becomes bottom? Not exactly: It's like taking the top half (A) and putting it on the bottom, then putting the bottom half on top? Actually the resulting order: bottom is 4 (which was originally just above the split). The top is 3 (originally just below top? Actually original top 8 is now near the bottom? Let's compute: A (top half) = [4,5,6,7,8]; B (bottom half) = [0,1,2,3]. After appending A block then B block, we get A then B. So the deck becomes: top half first (original indices 4..8) then bottom half (0..3). So it's like cutting the deck and putting the bottom half on top? Actually original deck: bottom (0) to top (8). After transformation, we have [4,5,6,7,8,0,1,2,3] so the new bottom is 4, new top is 3. That's a specific rearrangement. This is plausible.
Now, confirm with sample second dataset: n=9, r=4, c=1,2,3,4. I'll simulate to see if top becomes 0.
Let's quickly simulate with code in head or manually.
Initialize deck: [0,1,2,3,4,5,6,7,8]
Operation 1: c=1
Split: A = top half: sizeA=5 => indices 4-8: [4,5,6,7,8]; B = bottom: [0,1,2,3].
Now loop with c=1:
- Pull 1 from A bottom: take [4]; A becomes [5,6,7,8]; C = [4]
- Pull 1 from B bottom: take [0]; B becomes [1,2,3]; C = [4,0]
- Next iteration: A non-empty, pull 1: [5]; A->[6,7,8]; C->[4,0,5]
- Pull 1 from B: [1]; B->[2,3]; C->[4,0,5,1]
- Next: A: [6]; A->[7,8]; C->[4,0,5,1,6]
- B: [2]; B->[3]; C->[4,0,5,1,6,2]
- Next: A: [7]; A->[8]; C->[4,0,5,1,6,2,7]
- B: [3]; B->[]; C->[4,0,5,1,6,2,7,3]
- Next: A: [8]; A->[]; C->[4,0,5,1,6,2,7,3,8]
- B empty: skip.
Result after first op: [4,0,5,1,6,2,7,3,8] (bottom->top). Top = 8.
Operation 2: c=2
Now deck: [4,0,5,1,6,2,7,3,8]
Split: n=9, sizeB=4, sizeA=5. So A = top half = last 5 elements: indices 4..8: deck[4:] = [6,2,7,3,8]? Wait careful: deck list is [4,0,5,1,6,2,7,3,8]. Indices: 0:4,1:0,2:5,3:1,4:6,5:2,6:7,7:3,8:8. sizeB=4, so B = first 4: [4,0,5,1]; A = rest: [6,2,7,3,8].
Now c=2:
Initialize C = []
A = [6,2,7,3,8] (bottom 6, top 8)
B = [4,0,5,1] (bottom 4, top 1)
Iteration:
- Pull 2 from A bottom: block = [6,2]; A -> [7,3,8]; C = [6,2]
- Pull 2 from B bottom: block = [4,0]; B -> [5,1]; C = [6,2,4,0]
- Next: A len=3 >=2: pull [7,3]; A -> [8]; C = [6,2,4,0,7,3]
- B len=2 >=2: pull [5,1]; B -> []; C = [6,2,4,0,7,3,5,1]
- Next: A len=1 (<2): pull all [8]; A -> []; C = [6,2,4,0,7,3,5,1,8]
- B empty: skip
Result: [6,2,4,0,7,3,5,1,8]; top = 8.
Operation 3: c=3
Deck: [6,2,4,0,7,3,5,1,8]
Split: sizeB=4, sizeA=5.
B = first 4: [6,2,4,0]
A = rest: [7,3,5,1,8]
Now c=3:
C = []
A = [7,3,5,1,8] (bottom 7, top 8)
B = [6,2,4,0] (bottom 6, top 0)
Iteration:
- Pull 3 from A bottom: [7,3,5]; A -> [1,8]; C = [7,3,5]
- Pull 3 from B bottom: [6,2,4]; B -> [0]; C = [7,3,5,6,2,4]
- Next: A len=2 (<3): pull all [1,8]; A -> []; C = [7,3,5,6,2,4,1,8]
- B len=1 (<3): pull all [0]; B -> []; C = [7,3,5,6,2,4,1,8,0]
Result: [7,3,5,6,2,4,1,8,0]; top = 0.
Operation 4: c=4
Deck: [7,3,5,6,2,4,1,8,0]
Split: sizeB=4, sizeA=5.
B = first 4: [7,3,5,6]
A = rest: [2,4,1,8,0]
c=4:
C = []
A = [2,4,1,8,0]
B = [7,3,5,6]
Iteration:
- Pull 4 from A bottom: [2,4,1,8]; A -> [0]; C = [2,4,1,8]
- Pull 4 from B bottom: [7,3,5,6]; B -> []; C = [2,4,1,8,7,3,5,6]
- Next: A len=1 (<4): pull all [0]; A -> []; C = [2,4,1,8,7,3,5,6,0]
- B empty skip.
Result: [2,4,1,8,7,3,5,6,0]; top = 0.
Thus final top card = 0, matches sample output.
Great.
So algorithm is correct.
Now, implement reading. We'll parse all ints. However, need to be careful: The input might have each dataset on separate lines but we can just read ints.
We'll define solve() as described.
Complexities: fine.
Now, consider potential off-by-one: In split, we used floor(n/2) for B (bottom half). That matches example: n=9, B size = 4, A size=5. For even n, both halves equal.
Edge case: n=0 not possible.
Now, also note that c may be 0? Problem says positive integer, so c>=1.
Now, we output each top card on its own line.
Implementation:
def riffle(deck, c):
n = len(deck)
mid = n // 2 # size of bottom half
A = deck[mid:] # top half
B = deck[:mid] # bottom half
C = []
while A or B:
k = min(c, len(A))
if k:
C.extend(A[:k])
A = A[k:]
k = min(c, len(B))
if k:
C.extend(B[:k])
B = B[k:]
return C
Alternatively, we can use deque for efficient pops from left, but not necessary.
Now, we need to loop over data sets until no more tokens. But we must ensure we don't try to read beyond available tokens; that is, after reading n and r, we need to check that there are at least r integers left. But input is guaranteed to be valid.
We'll use index pointer.
One nuance: The input might have trailing whitespace, but that's fine.
Now, we can write code.
But also note: the problem may be from an old contest and might have multiple test cases where n and r are on one line, and then the r integers may be on one line or multiple lines. Our token parsing handles that.
Now, final code.
Potential pitfalls: For languages like C, they may need to parse line by line, but Python fine.
Now, ensure that we handle large number of datasets (less than 100). n up to 50, r up to 50. So simulation is trivial.
Now, output as integers.
We'll write the solve function.
I'll add a main guard.
Testing with given input:
Input:
9 1
3
9 4
1 2 3 4
Our code reads ints: [9,1,3,9,4,1,2,3,4]
Processing first dataset: n=9,r=1, then reads 1 int: 3, deck after riffle top=3 -> output 3.
Second dataset: n=9,r=4, reads 4 ints:1,2,3,4, top=0 -> output 0.
Matches.
Now, edge case: if there are extra blank lines, int parsing will ignore.
Now, final answer.
But there is also possibility that c may be larger than n. In the operation, after splitting, we may take more cards than available; we handle by min.
Now, I think that's it.
But before finalizing, I want to double-check the orientation: In the split, A is top half, B is bottom half. We set A = deck[mid:], B = deck[:mid]. That's correct because deck is bottom->top, so indices 0 to mid-1 are bottom half, indices mid to n-1 are top half. Yes.
Now, the operation: we pull from bottom of A and B. Since we represent A as list with index 0 bottom, pulling from bottom means taking from start. So we take A[:k] and B[:k] and remove those elements. That's consistent.
Now, stacking onto deck C: we extend C (append). That places the block on top.
Now, after loop, C becomes new deck.
Thus correct.
Now, note: In the problem description they mention "c cards are pulled from bottom of deck A and are stacked on deck C". The term "stacked on" might mean that they are placed on top of C, which we did.
Now, I'll produce final solution.
But let's also consider if the input format may have each dataset separated by newline, but r integers may be on the same line as n r? Typically, after n r line, the next r lines each contain one integer. But they may also appear on the same line. Our token parsing works.
Now, code.
We need to be careful about reading EOF: The while loop condition: i < len(data) is fine if we have exactly the needed tokens. But if there is extra whitespace, it's fine. However, there might be trailing newline after last dataset; that doesn't add extra ints.
Now, we must ensure that we don't try to read beyond data if the input is incomplete; but trust.
Now, final code.
Potential micro-optimizations: None needed.
We'll output each result on separate line.
Now, let's write final answer.
I will include explanation in comments? Not needed.
But as final output, provide code.
Now, also note: Some problems may require that we stop when n=0 and r=0. But here not mentioned; it says EOF. So we just read until EOF.
Thus final solution.
We'll produce Python code.
Now, I'll write it. ```python
import sys
def riffle(deck, c):
"""
Perform one riffle operation with parameter c on the deck.
deck is a list with bottom at index 0, top at index -1.
Returns the new deck after the shuffle.
"""
n = len(deck)
mid = n // 2 # size of the bottom half
A = deck[mid:] # top half (larger if odd)
B = deck[:mid] # bottom half
C = []
while A or B:
# take from A
k = min(c, len(A))
if k:
C.extend(A[:k])
A = A[k:]
# take from B
k = min(c, len(B))
if k:
C.extend(B[:k])
B = B[k:]
return C
def solve() -> None:
data = list(map(int, sys.stdin.read().split()))
i = 0
out_lines = []
while i < len(data):
n = data[i]
r = data[i + 1]
i += 2
deck = list(range(n)) # initial order: 0 bottom .. n-1 top
for _ in range(r):
c = data[i]
i += 1
deck = riffle(deck, c)
top_card = deck[-1] # last element is the top
out_lines.append(str(top_card))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()