Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

func main() {
	for sc.Scan() {
		if sc.Text() == "0 0" {
			break
		}
		chunks := strings.Split(sc.Text(), " ")
		n, _ := strconv.Atoi(chunks[0])
		p, _ := strconv.Atoi(chunks[1])
		cnts := make([]int, n, n)
		for i := 0; true; i++ {
			j := i % n
			if p == 0 {
				p, cnts[j] = cnts[j], 0
			} else {
				cnts[j]++
				p--
			}

			if p == 0 {
				noneCnt := 0
				winner := 0
				for j, c := range cnts {
					if c == 0 {
						noneCnt++
					} else {
						winner = j
					}
				}
				if noneCnt == n-1 {
					fmt.Println(winner)
					break
				}
			}
		}
	}
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int check(int a[51], int n)
{
	int c=0, r=-1;
	
	for(int i = 0; i < n; i++){
		if(a[i] > 0){
			c++;
			r = i;
		}
	}
	if(c > 1){
		r = -1;
	}
	
	return r;
}

int main()
{
	int n, p, s, t, a[51];
	
	while(1){
		cin >> n >> p;
		if(n == 0 && p == 0){
			break;
		}
		fill(a, a+51, 0);
		t = 0;
		bool f = true;
		for(int i = 0; i < 1000000; i++){
			if(p == 0){
				s = check(a, n);
				if(s != -1){
					cout << s << endl;
					f = false;
					break;
				}
				p += a[t];
				a[t] = 0;
			} else {
				p--;
				a[t]++;
			}
			
			t++;
			if(n == t){
				t = 0;
			}
		}
		
		if(f){
			cout << 0 << endl;
		}
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

int main(void){
    int n,q,w;
    int i;
    while(true){
        cin >> n >> q;
        if(n == 0) break;
        int p[n] = {0};
        i = 0;w = q;
        while(true){
            if(w){
                p[i]++;
                w--;
            }
            else{
                if(p[i] == q){
                    cout << i << endl;
                    break;
                }
                else{
                    w = p[i];
                    p[i] = 0;
                }
            }
            i = (i+1)%n;
        }
    }
}

C++: #include<bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n ; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;

int main(void){
    while(true){
        int n, p; cin >> n >> p;
        if(n == 0 and p == 0) return 0;
        else{
            vector<int> person(n, 0);
            int turn = 0, cup = p; // turnは今誰のターンか示す, cupは椀の中に入っている小石の個数
            while(true){
                if(cup != 0){ //cupに小石がある時
                    person[turn]++;
                    cup--;
                }
                else if(person[turn] != 0){ //小石を持っているとき
                    cup += person[turn];
                    
                    person[turn] = 0;
                }

                if(person[turn] == p) break;
                turn++;
                if(turn == n) turn = 0;
            }
            cout << turn << endl;
        }
    }
}

C++: #include<bits/stdc++.h>
using namespace std;

int N,P;
int D[55];


bool check(int ind){
  for(int i=0;i<N;i++){
    if( i == ind ) continue;
    if( D[i] ) return false;
  }
  return true;
}

int main(){
  while( cin >> N >> P && (N||P) ) {
    memset(D,0,sizeof(D));
    int ind = 0;
    while( 1 ){
      if( P ){
	P--;
	D[ind]++;
	if( P == 0 ){
	  if( check(ind) ) break;
	}
      } else {
	P += D[ind];
	D[ind]=0;
      }
      ind++;
      ind%=N;
    }
    cout << ind << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n, p;
  while(1){
    cin >> n >> p;
    if(n==0){
      break;
    }
    vector<int> a(n,0);
    int i = 0, tmp = p;
    while(1){
      if(tmp>0){
        tmp--;
        a[i]++;
      }else{
        if(a[i]==p){
          cout << i << endl;
          break;
        }
        tmp += a[i];
        a[i] = 0;
      }
      i++;
      if (i == n) {
        i = 0;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
using namespace std;

int main(){
	int n, p;
	int tp;
	while (1){
		cin >> n >> tp;
		if (n == 0 && tp == 0) break;
		p = tp;
		int win = 0;
		int N[50] = { 0 };
		//?????????????§?
		while (1){
			p--; //????????????
			N[win]++; //???????????????

			//?¢????????????¨???
			if (p == 0){
				//???????????¨?????????????????£??????????????????
				if (N[win] == tp){
					cout << win << endl;
					break;
				}
				else{
					p += N[win];
					N[win] = 0;
				}
			}

			win++;
			if (win == n) win = 0;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
	while(true){
		int n,p;	cin>>n>>p;
		if(n==0)	break;
		vector<int> a(n,0);
		int cur=0;
		int left=p;
		while(true){
			if(left){
				a[cur]++;
				left--;
			}
			else{
				left=a[cur];
				a[cur]=0;
			}
			if(left==0&&a[cur]==p)	break;
			cur=(cur+1)%n;
		}
		cout<<cur<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>
#include <random>
#include <cstring>

#define rep(i,n) for(int i=0;i<int(n);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back

using namespace std;
using ll = long long;

const ll mod = 1000000007;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

int main(){
  int n, _p;
  while(cin >> n >> _p) {
    if(n == 0 && _p == 0) break;
    vector<int> people(n, 0);
    int step = 0, p = _p;
    while(1){
      if(p){
	people[step]++;
	p--;
      } else {
	p = people[step];
	people[step] = 0;
      }
      
      if(people[step] == _p) {
	cout << step << endl;
	break;
      }
      step = (step + 1) % n;
    }
  }
  return 0;
}

C++: #include <iostream>
using namespace std;

int main() {
    while (true) {
        int n, p;
        cin >> n >> p;
        if ((n | p) == 0) {
            break;
        }
        int stone = p, player = 0, win = 0;
        int* players = new int [n];
        for (int i = 0; i < n; i++) {
            players[i] = 0;
        }
        while (true) {
            if (stone > 0) {
                players[player]++;
                stone--;
                if (stone == 0) {
                    bool winFlag = true;
                    for (int i = 0; i < n; i++) {
                        if (i != player && players[i] > 0) {
                            winFlag = false;
                            break;
                        }
                    }
                    if (winFlag) {
                        win = player;
                        break;
                    }
                }
            } else {
                stone += players[player];
                players[player] = 0;
            }
            if (player < n - 1) {
                player++;
            } else {
                player = 0;
            }
        }
        cout << win << endl;
        delete[] players;
    }
}