One of the oddest traditions of the town of Gameston may be that even the town mayor of the next term is chosen according to the result of a game. When the expiration of the term of the mayor approaches, at least three candidates, including the mayor of the time, play a game of pebbles, and the winner will be the next mayor.
The rule of the game of pebbles is as follows. In what follows, n is the number of participating candidates.
Requisites A round table, a bowl, and plenty of pebbles. Start of the Game A number of pebbles are put into the bowl; the number is decided by the Administration Commission using some secret stochastic process. All the candidates, numbered from 0 to n-1 sit around the round table, in a counterclockwise order. Initially, the bowl is handed to the serving mayor at the time, who is numbered 0. Game Steps When a candidate is handed the bowl and if any pebbles are in it, one pebble is taken out of the bowl and is kept, together with those already at hand, if any. If no pebbles are left in the bowl, the candidate puts all the kept pebbles, if any, into the bowl. Then, in either case, the bowl is handed to the next candidate to the right. This step is repeated until the winner is decided. End of the Game When a candidate takes the last pebble in the bowl, and no other candidates keep any pebbles, the game ends and that candidate with all the pebbles is the winner.
A math teacher of Gameston High, through his analysis, concluded that this game will always end within a finite number of steps, although the number of required steps can be very large.
Input
The input is a sequence of datasets. Each dataset is a line containing two integers n and p separated by a single space. The integer n is the number of the candidates including the current mayor, and the integer p is the total number of the pebbles initially put in the bowl. You may assume 3 ≤ n ≤ 50 and 2 ≤ p ≤ 50.
With the settings given in the input datasets, the game will end within 1000000 (one million) steps.
The end of the input is indicated by a line containing two zeros separated by a single space.
Output
The output should be composed of lines corresponding to input datasets in the same order, each line of which containing the candidate number of the winner. No other characters should appear in the output.
Sample Input
3 2
3 3
3 50
10 29
31 32
50 2
50 50
0 0
Output for the Sample Input
1
0
1
5
30
1
13
Example
Input
3 2
3 3
3 50
10 29
31 32
50 2
50 50
0 0
Output
1
0
1
5
30
1
13
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to simulate a game of pebbles with n candidates (0..n-1) sitting around a round table counterclockwise. Bowl is passed to the right (so next candidate index? They say "next candidate to the right". Since they sit counterclockwise, "right" presumably means clockwise? Actually careful: They said "candidates, numbered from 0 to n-1 sit around the round table, in a counterclockwise order." So the order around the table is 0,1,2,...,n-1 in counterclockwise direction. "Next candidate to the right" – typically if you sit around a table, the person to your right is the one clockwise. Since numbers increase counterclockwise, moving clockwise would decrease index? Actually standard: If you are sitting around a table, facing center, "right" is clockwise. If numbers are in counterclockwise order, then going counterclockwise you go to higher numbers mod n. So "right" is opposite direction, i.e., clockwise, which would be decreasing numbers mod n? But we need to interpret correctly. Let's analyze sample inputs to deduce.
Sample input: 3 2 -> output winner 1. Let's simulate to see direction.
We have n=3, p=2. Candidates 0,1,2 counterclockwise. Initially bowl handed to candidate 0.
Game rules: When a candidate is handed the bowl:
If any pebbles are in bowl, they take one pebble out and keep it (add to their personal kept pile). Then they hand bowl to next candidate to the right.
If no pebbles in bowl, they put all their kept pebbles (if any) into the bowl. Then hand bowl to next candidate to the right.
Game ends when a candidate takes the last pebble in the bowl, and no other candidates keep any pebbles. So that candidate wins.
We need to see which candidate wins for n=3,p=2.
Let's simulate both possibilities for direction:
Case 1: "right" means moving to higher index (counterclockwise) i.e., next candidate (i+1 mod n). Because that could also be considered "right"? Actually if you are sitting around table, the order is counterclockwise; the person to your right would be the one clockwise, which is opposite direction of numbering. But maybe they consider the "next candidate to the right" as in the order of seating: since they sit in counterclockwise order, going to the right along the table (like clockwise) would be decreasing indices. But sometimes they say "right" meaning the person seated to your immediate right, which is in clockwise direction. However in many round table problems, they often say "to the right" meaning clockwise. But let's test.
Simulate with n=3, p=2. Starting with bowl at 0, bowl has p=2 pebbles.
If direction = (i+1) mod n: 0 -> 1 -> 2 -> 0 ...
Sequence steps:
Initial: Bowl at 0, bowl pebbles=2, each candidate's kept count: [0,0,0].
Step for candidate 0: bowl has pebbles, so take one, keep. Now bowl pebbles=1, kept[0]=1. Then pass to right (1).
Now bowl at 1: bowl has 1 pebble, take one, keep. Bowl pebbles=0, kept[1]=1. Pass to right (2).
Bowl at 2: bowl has 0 pebbles. So candidate 2 puts all kept pebbles (0) into bowl (nothing). So bowl pebbles still 0. Pass to right (0).
Bowl at 0: bowl has 0 pebbles. Candidate 0 has kept=1, so puts all kept (1) into bowl. So bowl pebbles=1, kept[0]=0. Pass to right (1).
Bowl at 1: bowl has 1 pebble, take one. Bowl pebbles=0, kept[1]=2? Actually kept[1] was 1, now plus 1 =>2. Pass to right (2).
Bowl at 2: bowl has 0, kept[2]=0, puts 0, pass to right (0).
Bowl at 0: bowl 0, kept[0]=0, puts 0, pass to right (1).
Bowl at 1: bowl 0, kept[1]=2 -> puts 2 into bowl, kept[1]=0, bowl=2, pass to right (2).
Bowl at 2: bowl 2, take one, kept[2]=1, bowl=1, pass to right (0).
Bowl at 0: bowl 1, take one, kept[0]=1, bowl=0, pass to right (1).
Bowl at 1: bowl 0, kept[1]=0? Actually kept[1] was 0 after putting, so kept[1]=0. So no pebbles to put? Actually candidate 1 checks: bowl empty; they put all kept (0) -> bowl remains 0. Pass to right (2).
Bowl at 2: bowl 0, kept[2]=1 -> puts 1 into bowl, kept[2]=0, bowl=1, pass to right (0).
Bowl at 0: bowl 1, take one, kept[0]=2 (since had 1, now add 1 ->2), bowl=0, pass to right (1).
Bowl at 1: bowl 0, kept[1]=0, pass to right (2).
Bowl at 2: bowl 0, kept[2]=0, pass to right (0).
Bowl at 0: bowl 0, kept[0]=2 -> puts 2 into bowl, kept[0]=0, bowl=2, pass to right (1).
This seems like it will continue indefinitely? But we need to see ending condition: "When a candidate takes the last pebble in the bowl, and no other candidates keep any pebbles". So at the moment a candidate takes a pebble that makes bowl empty, and all other kept counts are zero, game ends. So we need to detect such moment.
Let's continue simulation a bit more, tracking after each "take" action whether other kept are zero.
Our simulation with direction +1 gave many steps. But we could compute winner based on known results? Let's compute using sample output: expected winner 1. Could our simulation eventually yield winner 1? Possibly. But we can also test direction = (i-1 mod n) i.e., moving clockwise (right). Let's simulate that.
Define next = (i - 1) mod n (since clockwise). For n=3, order: 0 -> 2 -> 1 -> 0 (since 0-1 mod 3 = 2, 2-1=1, 1-1=0). Let's simulate with p=2.
Initial: bowl at 0, bowl=2, kept=[0,0,0].
0: take one: bowl=1, kept[0]=1; pass to right (next=2? Actually 0 -> 2 because to the right clockwise). So bowl at 2.
2: bowl has 1, take: bowl=0, kept[2]=1; pass to right -> next = 1 (2-1=1). Bowl at 1.
1: bowl=0, puts kept? kept[1]=0, bowl stays 0; pass to right -> next = 0. Bowl at 0.
0: bowl=0, puts kept[0]=1 -> bowl=1, kept[0]=0; pass to right -> 2.
2: bowl=1, take: bowl=0, kept[2]=2; pass to right ->1.
Now at step after candidate 1 took last pebble? Actually candidate 1 took from bowl with 1 making bowl 0. But we need to check other kept counts: After candidate 1 took, kept[1] becomes 1, other kept: kept[0]=0, kept[2]=1 (since had 1 from earlier?). Wait careful: Let's track properly.
We need to keep a full state. I'll do systematic simulation for direction = -1 (clockwise) and see if we get winner 1 earlier. But the sample says winner is 1 for (3,2). Could be either direction.
But we should test the sample more thoroughly: Another sample: 3 3 -> output 0. Let's simulate for direction = +1 and -1 to see which matches.
I'll write a small mental simulation for both directions. But perhaps it's known as "bowl game" or "pebble game". The description resembles a variant of the Josephus problem or a game of passing around tokens. Might be easier to simulate programmatically mentally? But we can attempt to reason.
Alternate approach: The game can be modeled as a deterministic process; we can compute winner by simulation since constraints: n up to 50, p up to 50, and max steps <= 1e6. So simulation is feasible. For each dataset, we need to simulate until termination. The input is multiple datasets, and termination within 1e6 steps, so even naive simulation in C++ is fine.
Thus we need to code a simulation, but first clarify direction.
Let's examine the phrase: "All the candidates, numbered from 0 to n-1 sit around the round table, in a counterclockwise order. Initially, the bowl is handed to the serving mayor at the time, who is numbered 0." Then: "the bowl is handed to the next candidate to the right." Since they are seated counterclockwise, if you stand at a candidate and look toward the center of the table, your right side is clockwise. That is the direction opposite to the numbering. So the "next candidate to the right" would be the one with the next lower number modulo n (since numbers increase counterclockwise). For example, if n=5, order counterclockwise: 0,1,2,3,4. The person to the right of 0 is 4 (clockwise). To the right of 1 is 0, etc. So next index = (i-1+n) % n. This is typical.
But some problems consider "right" as the direction of increasing numbers if they specify seating order. However, they explicitly said "counterclockwise order", so "right" likely means clockwise. Let's test with sample to confirm.
We have to check both possibilities: For (3,2) the winner should be 1; for (3,3) winner 0; for (3,50) winner 1; for (10,29) winner 5; for (31,32) winner 30; for (50,2) winner 1; for (50,50) winner 13.
We can attempt quick simulations for small n,p to see which direction yields these outputs.
Because we are writing solution, we need to implement correctly. Let's simulate manually for (3,2) and (3,3) for both directions to see which matches.
We already started direction -1 (clockwise) and we haven't reached termination yet. Let's continue from where we left for (3,2) with direction -1.
I'll re-simulate systematically for (3,2) with direction = (i-1) mod n.
State: we have bowl at candidate i, bowl pebbles b, kept array k[0..2].
We need to simulate step by step, and after each "take" action (when a candidate takes a pebble from bowl), check if after taking, bowl becomes 0 AND all other k's (including the candidate's own? The rule: "When a candidate takes the last pebble in the bowl, and no other candidates keep any pebbles". The candidate who just took the last pebble may have kept pebbles (including the one just taken) but the condition says "no other candidates keep any pebbles". The candidate themselves has just taken the last pebble and now holds that pebble (maybe plus others they had kept). But the condition requires that no other candidates (i.e., besides the winner) have any kept pebbles. So after taking, we check if b==0 and sum of kept for all candidates except the current one is 0. Or equivalently, total kept pebbles all belong to that candidate. But note: the candidate's own kept includes the one just taken; they may have had some previously. That is allowed because they say "that candidate with all the pebbles is the winner." So indeed, after the move, all pebbles are in the winner's possession (their kept pile) and the bowl is empty. So condition: after the "take" action, b == 0 and for all j != i, k[j] == 0.
Now simulate carefully.
Let's start fresh: n=3, p=2. Direction: next = (i-1) mod 3.
Initialize:
b = 2
k = [0,0,0]
i = 0 (current holder)
We'll track steps.
Step 1: i=0, b>0 -> take 1: b=1, k[0]=1. Check termination? b=1 not 0, continue. Pass to next: i = (0-1)%3 = 2.
Step 2: i=2, b=1>0 -> take: b=0, k[2]=1. Check: b==0, but other kept? k[0]=1, k[1]=0. So other candidates have pebbles (k[0]=1) -> not win. Pass to next: i = (2-1)%3 = 1.
Step 3: i=1, b=0 -> so puts all kept into bowl: k[1]=0 -> nothing. b remains 0. Pass: i = (1-1)%3 = 0.
Step 4: i=0, b=0 -> puts all kept into bowl: k[0]=1 -> b=1, k[0]=0. Pass: i = (0-1)%3 = 2.
Step 5: i=2, b=1>0 -> take: b=0, k[2]=2. Check: b==0, other kept: k[0]=0, k[1]=0? Actually k[0]=0 after step 4, k[1]=0, so all others have 0. But wait, k[2]=2 includes the one just taken plus previous one. So after this take, bowl empty, and only candidate 2 has pebbles. So candidate 2 wins. But sample output says winner is 1. So this direction gives winner 2, not 1. So direction = clockwise yields winner 2 for (3,2). That does not match.
Now check direction = +1 (counterclockwise, i.e., next = (i+1)%n). We earlier started simulation but didn't finish. Let's simulate that thoroughly for (3,2) to see who wins.
Step5: i=1, b=1 -> take: b=0, k[1]=2. Check: b=0, other kept: k[0]=0, k[2]=0 -> all others 0. So candidate 1 wins! That matches sample (winner 1). So direction is (i+1) mod n, i.e., moving to the next candidate in counterclockwise order? But they said "next candidate to the right". However we have found that for n=3, p=2, the winner 1 emerges if we pass to index+1. But we need to double-check: In our step order, we passed from 0 to 1 (increase index). That is "right"? If they are seated counterclockwise, moving to the right (clockwise) would decrease index. But here increasing index gave correct winner. So perhaps "right" is misinterpreted: Maybe they mean "to the right" along the seating order (since they sit counterclockwise, the person to your right is actually the one with a higher number if you consider the direction of seating? Let's visualize: If people are seated around a round table, and they are numbered in counterclockwise order, then if you stand behind the table and look at them, the numbers increase as you go counterclockwise. Your right hand side if you are facing the center is clockwise, which is opposite to the numbering. But if you are sitting at the table, your personal right is clockwise, indeed. However sometimes in these puzzles, they define "right" as the direction when looking from above the table, not from the perspective of the candidates. But they said "next candidate to the right." Usually in round table problems, "to the right" means clockwise. But our simulation contradicted that. Let's double-check the phrasing: "All the candidates, numbered from 0 to n-1 sit around the round table, in a counterclockwise order. Initially, the bowl is handed to the serving mayor at the time, who is numbered 0. Game Steps When a candidate is handed the bowl and if any pebbles are in it, one pebble is taken out of the bowl and is kept, together with those already at hand, if any. If no pebbles are left in the bowl, the candidate puts all the kept pebbles, if any, into the bowl. Then, in either case, the bowl is handed to the next candidate to the right. This step is repeated until the winner is decided."
It might be that "to the right" means the next candidate in the order of seating, i.e., counterclockwise. Because in many cultures, when people sit around a table, the person to your right is the one sitting next to you in a clockwise direction if you're facing the center, but here they may be facing outward? Or they might be using "right" to mean "in the direction of increasing numbers", which they set as counterclockwise. They explicitly said "in a counterclockwise order", so the order is counterclockwise. Then "to the right" could be synonymous with "following that order"? Actually no, the phrase "to the right" typically refers to the immediate neighbor on the right side. However, they might be using a convention: The candidates are seated around the table, and the bowl is passed to the next candidate in the seating order (which is counterclockwise). They might have used "to the right" loosely to mean "to the next person on the right" if you are looking at the center, but that would be clockwise. So it's ambiguous.
Given our simulation of (3,2) and (3,3) (we need to check (3,3) as well) suggests direction = +1 gives correct results for (3,2). Let's test (3,3) with direction +1.
n=3, p=3. Simulate to see winner.
Initialize: b=3, k=[0,0,0], i=0.
Step1: i=0, b>0 take -> b=2, k0=1; pass i=1.
Step2: i=1, b=2 take -> b=1, k1=1; pass i=2.
Step3: i=2, b=1 take -> b=0, k2=1; Check: b=0, other kept: k0=1, k1=1 -> not win. pass i=0.
Step4: i=0, b=0 -> put kept: k0=1 -> b=1, k0=0; pass i=1.
Step5: i=1, b=1 take -> b=0, k1=2; Check: others? k0=0, k2=1 -> k2=1, so not win. pass i=2.
Step6: i=2, b=0 -> put kept: k2=1 -> b=1, k2=0; pass i=0.
Step7: i=0, b=1 take -> b=0, k0=1; Check: others? k1=2, k2=0 -> k1=2, not win. pass i=1.
Step8: i=1, b=0 -> put kept: k1=2 -> b=2, k1=0; pass i=2.
Step9: i=2, b=2 take -> b=1, k2=1; pass i=0.
Step10: i=0, b=1 take -> b=0, k0=2 (since had 1, add 1 =>2); Check: others? k1=0, k2=1 -> k2=1, not win. pass i=1.
Step11: i=1, b=0 -> put kept: k1=0; b=0; pass i=2.
Step12: i=2, b=0 -> put kept: k2=1 -> b=1, k2=0; pass i=0.
Step13: i=0, b=1 take -> b=0, k0=3; Check: others? k1=0, k2=0 -> all others zero. So candidate 0 wins. That matches sample output 0. Good.
So direction +1 yields correct for (3,2) and (3,3). For (3,50) sample says 1. We could test later, but likely correct.
Thus direction is: pass to next candidate with index (i+1) mod n. That is, "to the right" means moving counterclockwise (increasing index). Probably they consider that the seating order is counterclockwise, and "right" means following that order (like a circle, going to the right). Actually if you imagine a circle with numbers increasing counterclockwise, then moving to the right (clockwise) would be decreasing numbers. But they might have defined seating order as "counterclockwise" but then said "to the right" meaning the direction of the seating order? Hmm ambiguous but simulation confirms.
Thus we implement simulation: Bowl starts at candidate 0. Keep an array kept[n] initialized to 0. Bowl pebbles = p. Current player index i = 0.
Loop:
If bowl > 0:
bowl--;
kept[i]++;
After this action, if bowl == 0 and sum of kept for all j != i is 0 -> winner i, break.
else: i = (i+1) % n; continue.
Else (bowl == 0):
if kept[i] > 0:
bowl += kept[i];
kept[i] = 0;
(if kept[i]==0, nothing changes)
Then i = (i+1) % n; continue.
Note: The problem statement: "If no pebbles are left in the bowl, the candidate puts all the kept pebbles, if any, into the bowl." So they put all of their kept pebbles into the bowl, not just one. That's what we did.
Now termination condition: "When a candidate takes the last pebble in the bowl, and no other candidates keep any pebbles, the game ends and that candidate with all the pebbles is the winner." So we check after the take action, not after a put action.
The simulation must run within 1e6 steps, which is fine.
Edge Cases: p>=2, n>=3. We need to handle correctly.
Now we should verify more sample cases to be confident.
Check (10,29) expected output 5. We can simulate quickly with code mentally? Not necessary; we trust that direction +1 yields correct outputs. But let's test maybe (50,2) and (50,50) quickly by reasoning? Not needed.
Thus solution: For each dataset, simulate until win condition.
However, we must ensure that the simulation terminates within given bound. Since p <= 50 and n <= 50, the state space might be large, but maximum steps 1e6 is given. So we can just simulate.
But is there any trick about the condition "no other candidates keep any pebbles"? After a candidate takes the last pebble, if other candidates have zero kept, then game ends. In our simulation we check after each take action if bowl == 0 and sum(kept) for others zero. However, note that sum(kept) for others is zero if and only if all pebbles are with the current candidate. Because total pebbles p is constant. So we could also check if kept[i] == p and bowl == 0. But after a take, bowl becomes 0 and kept[i] becomes previous_kept[i] + 1. So condition is kept[i] == p. Because total pebbles = bowl + sum(kept) always = p. Initially bowl = p, kept sum 0. When we take, bowl reduces by 1, kept[i] increases by 1, total constant. When we put, bowl increases by kept[i], kept[i] becomes 0, total constant. So invariant: bowl + sum(kept) = p. So at any time, if bowl == 0, then sum(kept) = p. Then the condition "no other candidates keep any pebbles" is equivalent to kept[i] = p (i.e., all pebbles with this candidate). So we can check: after a take, if bowl == 0 and kept[i] == p. That is simpler: after take, if bowl == 0 and kept[i] == p (since bowl==0 and total p, sum kept = p, so if kept[i] == p, others are zero). Indeed.
Thus check: after take, if bowl == 0 and kept[i] == p => winner.
Similarly, after a put action, there's no win condition.
Now simulation steps: while true:
if bowl > 0:
bowl--, kept[i]++
if bowl == 0 and kept[i] == p: winner i, break
else:
if kept[i] > 0:
bowl += kept[i]
kept[i] = 0
// no win check
i = (i+1)%n
That's it.
But we need to be careful: after a take, it's possible that bowl becomes 0 and kept[i] == p, but if p is total pebbles, kept[i] can become p only if all other kept are zero. But if after take, bowl==0, then total sum kept = p. So kept[i] == p implies others zero. So yes.
Thus we don't need to sum others.
Now, one more nuance: The game ends when a candidate takes the last pebble in the bowl, and no other candidates keep any pebbles. That is exactly the condition we check immediately after taking the last pebble, before passing the bowl. So we check right after the take, not after passing.
Implementation straightforward.
But we must also consider that there could be a case where the candidate takes the last pebble, but others have pebbles, so game continues.
Now, is there any chance of infinite loop? According to problem, it always ends within finite steps, and for given constraints within 1e6 steps.
We'll implement simulation accordingly.
Now I'll write a solution in C++.
Pseudo:
while (cin >> n >> p, n||p) {
vector kept(n,0);
int bowl = p;
int cur = 0;
int winner = -1;
while (true) {
if (bowl > 0) {
// take one
bowl--;
kept[cur]++;
if (bowl == 0 && kept[cur] == p) {
winner = cur;
break;
}
} else {
// bowl empty
if (kept[cur] > 0) {
bowl += kept[cur];
kept[cur] = 0;
}
}
cur = (cur + 1) % n;
}
cout << winner << endl;
}
Let's test with sample.
We already manually tested (3,2) and (3,3). For (3,50) sample says 1; we can run mental? Not necessary.
But we should test (10,29) maybe by writing a small script? Since we can't run code now, but we trust.
But I want to double-check edge cases: What if a candidate with bowl empty and no kept pebbles: they do nothing, pass. That's fine.
Now, note that the step count could be up to 1e6, which is fine.
Potential optimization: We might use a queue or something but not needed.
One subtlety: The total pebbles p is constant. But when we put, we add all kept of current candidate to bowl and reset that candidate's kept to 0. That's correct.
Now, is there any possibility that after a take, the condition is true but we might have passed? No, we check before passing.
Now, check (50,2) sample: output 1. Let's quickly simulate small case n=50 p=2 direction +1. Hard to simulate manually but plausible.
Thus answer.
But we must be cautious: The initial bowl is handed to candidate 0, and we start simulation with candidate 0's turn. That's correct.
Now confirm with sample (31,32) output 30. We'll trust.
Thus solution is simple simulation.
But wait: The problem statement says "If no pebbles are left in the bowl, the candidate puts all the kept pebbles, if any, into the bowl. Then, in either case, the bowl is handed to the next candidate to the right." So the order of actions: if bowl>0: take one, then hand over; if bowl==0: put all kept (if any) into bowl, then hand over. So exactly as we coded.
Now, the winner condition: "When a candidate takes the last pebble in the bowl, and no other candidates keep any pebbles, the game ends and that candidate with all the pebbles is the winner." So we check after taking.
Thus solution.
But one more nuance: Could there be a case where a candidate takes the last pebble, but at that moment, others have zero pebbles, so winner. In that case, the candidate does not hand over the bowl; the game ends immediately. So we break before passing.
Now, we should ensure that our simulation does not exceed 1e6 steps. Since constraints guarantee that, fine.
Now, we might also check for potential infinite loop due to some oversight. For example, if bowl becomes 0 and all kept are zero, then no one can ever get a pebble? But if bowl is 0 and all kept are 0, then the game would be stuck: every candidate sees bowl empty and has no kept, so nothing changes, and bowl stays 0, and we loop forever passing bowl. But can that happen? Let's think: Initially bowl = p >= 2, so not zero. The total pebbles is p, which is constant. If at some point bowl = 0 and sum kept = p, then there is at least one candidate with positive kept. So sum kept cannot be 0 while bowl = 0, because total p >0. So not possible. But could bowl be 0 and kept all zero? That would mean total pebbles = 0, but p>0. So impossible. So the simulation will always eventually reach a take that ends the game.
Thus safe.
Now we should double-check the sample (50,50) output 13. We'll trust.
Now write code.
But wait: There's a possible nuance about the order of indexing and direction. We used next = (cur+1)%n. But we earlier tested (3,2) gave winner 1. However we should also test (3,3) gave 0. Also test (3,50) should give 1. Let's quickly simulate for (3,50) maybe with code or reasoning? Since it's symmetric? But maybe we can test manually a few steps to see if it's plausible? Not necessary.
But we must also check the sample (10,29) output 5. Let's attempt to simulate quickly using mental code? That's too many steps. We can trust our simulation if we implement correctly.
But there is also possibility that the direction is (cur-1) mod n but our test of (3,2) gave winner 2, not 1. So not match. So definitely direction is +1.
Now, let's check the phrasing: "All the candidates, numbered from 0 to n-1 sit around the round table, in a counterclockwise order." So the order around the table is 0,1,2,... counterclockwise. "the bowl is handed to the next candidate to the right." If they are seated counterclockwise, moving to the right (clockwise) would go to lower numbers. But perhaps they intend "right" as the direction when facing outward from the table? Actually if you're sitting at a round table facing the center, your right is clockwise. But the numbers increase counterclockwise. So right is decreasing. So that's contradictory.
Maybe they consider the seating order as viewed from above, and "right" meaning "in the direction of increasing numbers"? That is, they may be using "right" in the sense of "the person on your right-hand side when you are facing the table"? Usually, if people sit around a table and are numbered in order around the table, the person to your immediate right is the next number if the numbering is clockwise. But here they explicitly said counterclockwise order. So if numbering is counterclockwise, then the person to your right is the one with the previous number (if you're facing the center). But they might be using a different perspective: maybe they are facing outward (away from the table). If you sit at a round table and face outward (like looking away from center), then your right hand side is opposite: that would be counterclockwise direction. But that's less common. However, typical puzzle phrasing: "They sit around a table. The bowl is passed to the right." and they give a numbering order (say clockwise). Here they give counterclockwise order, so "right" might be intended as clockwise (since they'd specify order separate from passing direction). But our simulation shows opposite. So maybe I mis-analyzed (3,2) and (3,3) with direction -1? Let's re-simulate direction -1 more carefully to ensure we didn't make an error that might actually yield winner 1 for (3,2) if we simulate longer? We stopped at candidate 2 winning at step 5. But maybe there is a later take that ends with candidate 1? But the game ends as soon as condition is satisfied; we don't continue. So winner would be candidate 2 at that step. So not match. So direction -1 yields winner 2, which is not sample.
Thus direction must be +1.
Given that, we proceed.
Now, note that the sample input includes 0 0 to terminate. So we read until both zero.
Implementation details: Use int for bowl and kept array. Since p <= 50, kept values can become up to p, so fits in int.
Now, we should ensure that we correctly handle the case when bowl is empty and the candidate has no kept pebbles: do nothing and pass.
Now, simulation loop: We'll have a step counter to ensure we don't go infinite, but not needed.
Now, is there any scenario where the simulation may take exactly 1e6 steps? The problem says "With the settings given in the input datasets, the game will end within 1000000 steps." So we can rely.
Now, I'll code in C++.
Pseudo-code:
#include
#include
using namespace std;
int main() {
int n, p;
while (cin >> n >> p && (n || p)) {
vector kept(n, 0);
int bowl = p;
int cur = 0;
int winner = -1;
while (true) {
if (bowl > 0) {
// take a pebble
bowl--;
kept[cur]++;
if (bowl == 0 && kept[cur] == p) {
winner = cur;
break;
}
} else {
if (kept[cur] > 0) {
bowl += kept[cur];
kept[cur] = 0;
}
}
cur = (cur + 1) % n;
}
cout << winner << endl;
}
return 0;
}
But wait: After taking, we check if bowl == 0 and kept[cur] == p. Could kept[cur] become p only when bowl becomes 0? Possibly. Since total = p, kept[cur] = p iff sum others = 0 and bowl = 0. So condition is correct.
Now, test sample.
Let's quickly simulate for (3,2) with our algorithm: yields 1 as we saw.
Now, check (3,3) yields 0.
Now, test (50,2). We can attempt a quick mental simulation for n=50 p=2? Not needed.
Now, one more nuance: The game ends when a candidate takes the last pebble and no other candidates keep any pebbles. In our simulation, we check after each take. That's correct.
Now, confirm (10,29) output 5. Might be correct.
Thus solution is straightforward.
But before finalizing, let's double-check the direction again with a different sample: (50,50) output 13. Might be computed by many solvers, and direction +1 likely yields that. So I'll go with that.
Now, could there be any other hidden rule? The problem says: "When a candidate is handed the bowl and if any pebbles are in it, one pebble is taken out of the bowl and is kept, together with those already at hand, if any. If no pebbles are left in the bowl, the candidate puts all the kept pebbles, if any, into the bowl. Then, in either case, the bowl is handed to the next candidate to the right." That's what we coded.
Now, we need to be careful about the order: The candidate checks if bowl has pebbles; if yes, they take one and keep it. After that, they hand over. If no pebbles, they put all their kept into bowl, then hand over. So we should not do anything else.
Now, the game ends only after a take action; not after a put.
Thus solution done.
But wait, maybe the condition "no other candidates keep any pebbles" includes the candidate themselves? The phrase says "no other candidates keep any pebbles", meaning all other candidates have zero. So we check sum of others. Using kept[cur] == p is equivalent because bowl=0.
Now, about handling large step counts: 1e6 is fine.
Now, we need to output winner number.
I think this solves.
But consider performance: n <= 50, p <= 50, each step is constant time, up to 1e6 steps, number of datasets unknown but probably not huge. So fine.
Now, I'll produce final answer.
However, I should test with sample input to ensure outputs match exactly.
Sample Input:
3 2
3 3
3 50
10 29
31 32
50 2
50 50
0 0
Output:
1
0
1
5
30
1
13
We'll assume our simulation produces these.
Let's test (3,50) manually with code in head? Could be that winner 1. Let's try to see pattern: For n=3, p mod something? Possibly the game is similar to a variant of Josephus? Not needed.
Now, I'm confident.
But wait: There might be a nuance about "the next candidate to the right". If they are seated counterclockwise, then "right" could be ambiguous. But our tests confirm +1 yields correct. So we stick.
Now, I'll prepare final answer.
But I'll also think about alternative solutions: Could there be a mathematical formula? Possibly, but simulation is fine.
Now, I'll write final solution.
However, we should check whether there is any possibility of infinite loop if we miscount steps? For instance, if bowl is 0 and kept[i] is 0, we just pass. That doesn't change state, but if all kept are 0, then bowl must be p? Actually if bowl 0 and all kept 0, total pebbles would be 0, contradiction since p>0. So cannot happen.
Thus okay.
Now, potential bug: In the condition after taking, we check if bowl==0 and kept[cur]==p. But what if p is large and kept[cur] becomes >p? That can't because total is p and bowl >=0. Kept[cur] <= p always. So fine.
Now, one more nuance: When a candidate takes a pebble, they might take the last pebble, but they might have had some kept already. The condition only cares about other candidates having zero. So winner can be a candidate who already had some pebbles; they'll end with all pebbles. That's allowed.
Thus done.
Now, I'll produce final answer in C++.
But to be thorough, I will test a few more small cases manually with our algorithm and perhaps compare with a known implementation. Since I can't run code, I'll simulate some.
For n=3, p=1? But p>=2, but let's see p=1 edge not allowed. So okay.
Now, I'm done.
One more test: n=4, p=2. Let's simulate manually with algorithm to see if it seems consistent.
We can try direction -1: would get different. Not needed.
Now, given that the game might have a structure similar to a "sieve" but anyway.
Thus solution is correct.
I'll now write final answer with code.
But wait: The problem statement says "the bowl is handed to the next candidate to the right." If we interpreted as +1, then "right" means counterclockwise? That seems contradictory. Let me double-check using geometry: Suppose we have a circle with numbers increasing counterclockwise. If you stand on the perimeter facing the center, your right side is clockwise, which is decreasing numbers. If you stand on the perimeter facing outward (away from center), your right side is counterclockwise. But the typical orientation for seating arrangements is people facing inward toward the table. The problem does not specify which direction they face. However, in many puzzles, they often say "sit around a table" and then "pass to the right" meaning clockwise. But here they specifically gave numbering order counterclockwise. In some contexts, they might define the order as counterclockwise but then "to the right" means along that order. Actually, sometimes they say "numbered in clockwise order". But here they said counterclockwise order. This might be a clue that they want the passing direction to be opposite of the numbering? Actually, if they number counterclockwise, then moving to the right (clockwise) would be opposite direction. But then they'd likely say "to the right" which is typical, and the numbering is just an ordering, not necessarily indicating facing direction. But in that case, my simulation of (3,2) with direction -1 gave winner 2, not 1. So maybe I made an error in the simulation of direction -1? Let's redo carefully for (3,2) with direction -1 to ensure we didn't prematurely terminate or miss a win condition.
We'll re-simulate from scratch with direction = (i-1)%3, and check every step. Write steps systematically.
State: n=3, p=2.
kept = [0,0,0], bowl=2, cur=0.
Step 1: cur=0, bowl>0 -> take: bowl=1, kept[0]=1.
Check win: bowl=1 not 0. Next cur = (0-1+3)%3 = 2.
Step 2: cur=2, bowl=1>0 -> take: bowl=0, kept[2]=1.
Check win: bowl=0, but kept[2]=1, total p=2, kept[2] !=2 (others kept[0]=1, kept[1]=0) -> not win. Next cur = (2-1)%3 = 1.
Step 3: cur=1, bowl=0 -> put kept[1]=0 -> bowl stays 0. Next cur = (1-1)%3 = 0.
Step 4: cur=0, bowl=0 -> put kept[0]=1 -> bowl=1, kept[0]=0. Next cur = (0-1)%3 = 2.
Thus direction -1 gives winner 2 for (3,2). Sample says 1. So direction must be +1.
Thus the phrase "to the right" likely means along the counterclockwise order. Possibly they consider the table from above, and "right" is the direction of increasing numbers if you go around the table to the right. That is ambiguous but we have empirical evidence.
Thus we go with +1.
Now I'll produce final answer.
But before final, we need to consider that the problem might be known as "Pebble Game" similar to "Bowl Game" from some contest. I've seen similar problems where the passing direction is to the next person in increasing index modulo n. So it's fine.
Now, I'll implement code accordingly.
However, we should also note that the game could take up to 1e6 steps, but we can also consider using a more efficient algorithm, but not needed.
Now finalize.
But wait: Let's verify sample (10,29) output 5 using a quick mental simulation? Not possible, but we can maybe implement a small script in mind? Hard. But to be thorough, I'll try to reason if any other nuance might affect. Could there be a case where after taking the last pebble, the candidate has all pebbles but also the bowl is empty, but we need to ensure that we check before passing. That we do.
Now, one more nuance: The problem says "When a candidate takes the last pebble in the bowl, and no other candidates keep any pebbles". It says "the last pebble in the bowl". That means at the moment of taking, the bowl had exactly one pebble? Actually "last pebble" means after taking, the bowl becomes empty. So bowl goes from 1 to 0. So we check when bowl becomes 0 due to take. That's what we did.
Now, if bowl had >1 and after taking still >0, not win. Good.
Now, if bowl had 0, then candidate does not take, they put. So no win.
Thus correct.
Now, the answer.
But wait, we need to consider: When a candidate puts all kept pebbles into the bowl, they might put many pebbles. Then the bowl becomes non-zero. That's fine.
Now, I think it's done.
Now, let's also test (50,2) quickly for direction +1 to see if winner 1? We can maybe simulate a pattern. For n=50, p=2, small. The game might be equivalent to something like: The bowl starts with 2 pebbles; candidates take turns taking one pebble until bowl empty, then someone puts all their kept. Could be that winner is 1. That seems plausible.
Now, final code.
Potential optimization: The simulation can be done using a while loop with step counter to prevent infinite loop if error, but not needed.
Now, I'll write final answer.
But we should also ensure reading input until 0 0, and output each winner on separate line.
Now, I'll output as required.
Thus final solution in C++:
#include
#include
using namespace std;
int main() {
int n, p;
while (cin >> n >> p && (n || p)) {
vector kept(n, 0);
int bowl = p;
int cur = 0;
while (true) {
if (bowl > 0) {
bowl--;
kept[cur]++;
if (bowl == 0 && kept[cur] == p) {
cout << cur << endl;
break;
}
} else {
if (kept[cur] > 0) {
bowl += kept[cur];
kept[cur] = 0;
}
}
cur = (cur + 1) % n;
}
}
return 0;
}
Given that we used while(true), it will break when winner found.
Now, I'll produce final answer accordingly.
But I want to double-check (10,29) with a quick simulation using code in mind? Let's try to simulate manually? Might be time-consuming but we can attempt to see if winner 5 emerges with our algorithm. Let's try to simulate with n=10, p=29, direction +1. But doing 29 pebbles and 10 people could be many steps, maybe tens or hundreds. Not feasible manually. However, we can trust the sample.
But maybe there is a known closed form. I've seen similar problem called "Bowl Game" from some programming contest (maybe ACM ICPC?). Actually I recall a problem "Bowl" from UVA or something? Not sure. The sample outputs given match known results? Possibly. I can quickly try to compute for small cases to verify my simulation for some random case. For instance, n=3, p=2 yields 1, which matches. n=3,p=3 yields 0. Good.
Now, confirm n=3,p=4? Not in sample, but we can compute manually with algorithm to see if it seems plausible.
Simulate n=3,p=4 with direction +1 to see pattern.
n=3, p=4:
Start: b4, k0=0, k1=0, k2=0 cur0.
0: take b3 k0=1 cur1
1: take b2 k1=1 cur2
2: take b1 k2=1 cur0
0: take b0 k0=2 -> b0 after take, check? b=0, k0=2, total p=4 -> not win because k0 !=4 (others have k1=1,k2=1) so continue. cur1
1: b=0 -> put k1=1 -> b=1, k1=0 cur2
2: b=1 take -> b=0 k2=2 -> check: b0, k2=2 !=4, others: k0=2,k1=0 -> not win. cur0
0: b=0 -> put k0=2 -> b=2 k0=0 cur1
1: b=2 take -> b1 k1=1 cur2
2: b=1 take -> b0 k2=3 -> b0, k2=3 !=4 (others: k0=0,k1=1) -> not win. cur0
0: b=0 -> put k0=0 (nothing) cur1
1: b=0 -> put k1=1 -> b=1 k1=0 cur2
2: b=1 take -> b0 k2=4 -> now b0 and k2=4 = p -> winner 2. So for (3,4) winner 2.
Interesting pattern: (3,2)->1, (3,3)->0, (3,4)->2. Possibly a pattern.
Now, check (3,5) maybe? But not needed.
Now, test (50,50) sample output 13; my simulation would compute it.
Thus I'm confident.
Now, about performance: The maximum steps is 1e6, and for each step we do O(1) operations. So for up to maybe 100 datasets, it's fine.
One thing: We use vector kept(n). That's okay.
Now, final answer.
But I should also consider that the simulation may be optimized by using a queue or something, but not necessary.
Thus the solution is as above.
Now, I'll provide final code with proper includes and main.
Additionally, we can add an optional step counter to guard against infinite loops, but not required.
I'll write final answer accordingly.
Now, write response.
```cpp
#include
#include
using namespace std;
int main() {
int n, p;
while (cin >> n >> p && (n != 0 || p != 0)) {
vector kept(n, 0); // pebbles kept by each candidate
int bowl = p; // pebbles in the bowl
int cur = 0; // candidate currently holding the bowl
code
while (true) {
if (bowl > 0) {
// take one pebble from the bowl
bowl--;
kept[cur]++;
// check winning condition
if (bowl == 0 && kept[cur] == p) {
cout << cur << endl;