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 <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i,a) for(int i = 0;i < (a); i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)
#define repd(i,a,b) for(int i = (a); i >= (b); i--)
#define repit(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++)
#define all(u) (u).begin(),(u).end()
#define rall(u) (u).rbegin(),(u).rend()
#define UNIQUE(u) (u).erase(unique(all(u)),(u).end())
#define pb push_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9
#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef vector<int> vi;

int main() {
    int n, p;
    while(cin >> n >> p, n || p){
        vi cand(n,0);
        int turn = 0;
        int st = p;
        while(1){
            if(st == 1 && cand[turn%n] == p-1){
                cout << turn%n << endl;
                break;
            }
            if(st == 0){
                st = cand[turn%n];
                cand[turn%n] = 0;
            }
            else{
                st--;
                cand[turn%n]++;
            }
            turn++;
        }
    }
    return 0;
}

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

int main(void) {
      int n, p;
      int bowl, turn, stones;


      while(1) {
          cin >> n >> p;
          if( (n+p) == 0 ) break;


          int* candidate = new int[n];
          for(int i=0; i < n; i++) {
            candidate[i] = 0;
          }
          turn = 0;
          stones = p;
          while(1) {

              if(stones > 0) { //?¢????????°???????????????????
                  stones--;
                  if(++candidate[turn%n] == p) break;
                  turn++;
              } else if(stones == 0) {  //?¢?????°?????????\??????
                  stones += candidate[turn%n];
                  candidate[turn%n] = 0;
                  turn++;
              }
          }
          cout << turn%n << endl;

      }




}

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

int main(){
	int mayors[50]{0};
	vector<int> ansvec;

	while (1){
		int n, p;
		cin >> n >> p;
		if (n == 0) break;

		for (int i = 0; i < n; i++) mayors[i] = 0;
		int bowlp = p;
		int turn = 0;
		while (1){
			if (bowlp == 1 && mayors[turn] == p - 1) break;
			else if (bowlp == 0){
				bowlp += mayors[turn];
				mayors[turn] = 0;
			}
			else{
				mayors[turn]++;
				bowlp--;
			}

			if (turn == n - 1) turn = 0;
			else turn++;
		}
		ansvec.push_back(turn);
	}

	for (int ans : ansvec){
		cout << ans << endl;
	}

	return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
  int n,p,data[50],i,d,result;
  while(1){
    cin >> n >> p;
    d=p;
    if(n==0&&p==0) break;
    for(i=0;i<n;i++) data[i]=0;
    i=0;
    while(1){
      if(p>0){
    p--;
        data[i]++;
    if(data[i]==d){
      result=i;
      break;
    }
      }else if(p==0){
        p=data[i];
        data[i]=0;
      } 
      if(i+1==n) i=0;
      else i++;
    }
    cout << result << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
    int n, p;

    for (;;) {
        cin >> n >> p;
        if (n == 0 && p == 0) break;

        vector<int> vi(n, 0);
        int i = 0;
        for (;;) {
            if (p > 0) {
                vi[i]++;
                p--;
            } else {
                bool win = true;
                for (int j = 0; j < n; j++) {
                    if (i == j) continue;
                    if (vi[j] > 0) win = false;
                }

                if (win) {
                    cout << i << endl;
                    break;
                } else {
                    p += vi[i];
                    vi[i] = 0;
                }
            }

            i = (i + 1) % n;
        }
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
typedef long long i64;

int main(){
    int n,p;
    while (true) {
        cin >> n >> p;
        if(n==0 && p==0) break;
        int currentp = p;
        vector<int> candidate;
        rep(i,n) candidate.push_back(0);
        int turn=0;
        while(true){
            if(currentp==0){
                currentp=candidate[turn];
                candidate[turn]=0;
            }else{
                candidate[turn]++;
                currentp--;
                if(currentp==0 && candidate[turn]==p) break;
            }
            turn=(turn+1)%n;
        }
        cout << turn << endl;
    }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int n, p;
int a, b[53];

int main(){
	while (cin >> n >> p){
		if (!n&!p) break;
		for (int i = 0; i < 53; i++) b[i] = 0;
		int i = 0;
		a = p;
		while (true){
			if (a > 0) {
				b[i]++; a--;
			}
			else if (a == 0){
				a = b[i]; b[i] = 0;
			}
			if (b[i] == p) break;
			if (i == n - 1) i = 0;
			else i++;
		}
		cout << i << endl;
	}
}

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

int main()
{
	int n,p;
	while(cin>>n>>p){
		if(n==0 && p==0) return 0;
		vector<int> have(n,0);
		int now=0;
		while(true){
			if(p>0){
				have[now]++;
				p--;
				if(p==0){
					bool ok=true;
					for(int i=0;i<n;i++){
						if(i==now) continue;
						if(have[i]>0){
							ok=false;
							break;
						}
					}
					if(ok){
						cout<<now<<endl;
						break;
					}
				}
			}
			else{
				p+=have[now];
				have[now]=0;
			}
			now++;
			if(now==n) now=0;
		}

	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
using namespace std;

int main(){
int m,p;
int a[55]={0};
while(cin>>m>>p && m!=0){
int no=0,is=p;
while(1){
if(is>0){
is--;
a[no]++;
}
else if(a[no]>0){
is=is+a[no];
a[no]=0;
}

if(a[no]==p) break;
no++;
no=no/m;
}
cout<<no<<endl;
}

return 0;
}

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

int main(){
  for(int n, p; cin >> n >> p && (n|p); ) {
    int q = p;
    vector<int> a(n, 0);
    for(int k = 0; ; k = (k + 1) % n) {
      if(p) {
        a[k] += 1;
        p -= 1;
        if(a[k] == q) {
          cout << k << endl;
          break;
        }
      } else {
        p += a[k];
        a[k] = 0;
      }
    }
  }
  return 0;
}