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 <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;

#define ll long long
#define pb push_back

ll MAX(ll a, ll b) {

	if (a > b)return a;
	else return b;

}
ll MIN(ll a, ll b) {

	if (a < b)return a;
	else return b;

}
ll powsur(ll a, ll b, ll p) {

	if (b == 0) {

		return 1;

	}
	else if (b % 2 == 0) {

		ll d = powsur(a, b / 2, p);
		return ((d%p)*(d%p)) % p;

	}
	else {

		return ((a%p)*powsur(a, b - 1, p) % p) % p;

	}

}//pは素数,aのb乗をpで割った余り
ll CMOD(ll n, ll r, ll p) {

	ll a = 1, b = 1, c = 1, re;
	for (int i = 1; i <= n; i++) {

		a *= i;
		a = a%p;

	}
	for (int i = 1; i <= r; i++) {

		b *= i;
		b = b%p;

	}
	for (int i = 1; i <= n - r; i++) {

		c *= i;
		c = c%p;

	}
	b = powsur(b, p - 2, p);
	c = powsur(c, p - 2, p);
	re = ((a%p)*(b%p)) % p;
	re = ((re%p)*c%p) % p;

	return re;

}//pは素数,(nCr)%p
ll gcd(ll a, ll b) {

	if (a < b) {

		ll tmp = a;
		a = b;
		b = tmp;

	}
	if (a%b == 0)return b;
	return gcd(b, a%b);

}//最大公約数
ll lcm(ll a, ll b) {

	return a / gcd(a, b)*b;

}//最小公倍数

bool check_continue(int P,vector<int> V){
	if(P!=0)return true;
	int cnt=0;
	for(int i=0;i<V.size();i++){
		if(V[i]>0)cnt++;
	}
	if(cnt==1)return false;
	return true;
}

int main() {
	
	while(1){
		int N,P;
		cin>>N>>P;
		if(N==0&&P==0)break;
		vector<int> V;
		for(int i=0;i<N;i++)V.pb(0);
		ll cnt=0;
		while(check_continue(P,V)){
			if(P==0){
				P=V[cnt%N];
				V[cnt%N]=0;
			}else{
				P--;
				V[cnt%N]++;
			}
			cnt++;
		}
		for(int i=0;i<N;i++){
			if(V[i]>0){
				cout<<i<<endl;
				break;
			}
		}
	}

	return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;

#define ISEQ(c) (c).begin(), (c).end()
typedef long long ll;

int winner(vector<int> &v) {
  int n = v.size();
  int count, winner;
  count = winner = 0;
  for (int i = 0; i < n; i++) {
    if (v[i] > 0) {
      count++;
      winner = i;
    }
  }
  if (count != 1) return -1;
  return winner;
}

int main() {
  while (true) {
    int n, p;
    cin >> n >> p;
    if (n == 0) break;
    vector<int> v(n,0);
    int res, index = 0;
    while (true) {
      if (p > 0) {
	v[index]++;
	p--;
      } else {
	p += v[index];
	v[index] = 0;
      }
      if (p == 0 and (res = winner(v)) != -1) {
	break;
      }
      index = (index+1) % n;
    }
    cout << res << endl;
  }
}

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

int main(){
int n,p;
int c[50];
for(;;){
cin >> n >> p;
if(!n && !p)break;
for(int i=0;i<n;i++)c[i] = 0;

int cnt = 0;
int pos = -1;
while(cnt!=1 || p){
pos = (pos+1)%n;
if(p){
if(!c[pos])cnt++;
c[pos]++;
p--;
}else{
if(!c[pos])cnt--;
p = c[pos];
c[pos] = 0;
}
pos = (pos+1)%n;
}
cout << pos << endl;
}
}

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

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

int main(){
    int n, p;
    
    while(true){
        int ans = 0;
        cin >> n >> p;
        vector<int> v(n);
        if(n == 0 && p == 0) break;

        int flag = 0, i = 0;
        int cnt = 0;
        while(true){
            if(p == 0){
                p += v[i];
                v[i] = 0;
            }
            else {
                p--;
                v[i]++;
            }
            if(p == 0) for(int j=0; j<n; j++) flag += (v[j] != 0);
            if(flag == 1) break;
            else flag = 0;
            i++;
            i %= n;
        }
        cout << i << endl;
    }
    return 0;
}

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

int main(){
	int people;
	int stone;
	int hand[55];

	while(1){
		int j=0;
		for(int i=0; i < 51; i++) hand[i] = 0;

		cin >> people >> stone;

		if((people|stone) == 0) break;
		
		int box = stone;

		while(1){
			if(box == 1){
				int cont = 0;
				for(int i=0; i < people; i++){
					if(hand[i] == 0) cont++;
				}
				if(people == cont+1) break;
				else{
					hand[j % people]++;
					box--;
				}
			}
			else if(box != 0){
				hand[j % people]++;
				box--;
			}
			else if(box == 0){
				box += hand[j % people];
				hand[j % people] = 0;
			}
			j++;
		}
		int ans = j % people;
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

int main(){
	int num,stone;
	while(1){
		cin >> num >> stone;
		if(num==0&&stone==0){ break; }
		vector<int> vec(num, 0);
		int flag, who;
		while(1){
			for(int i=0;i<vec.size();i++){
				flag = 0;
				if(stone>1){
					vec[i]++;
					stone--;
				}else if(stone==1){
					for(int j=0;j<vec.size();j++){
						if(vec[j]!=0 && i!=j){
							flag = 1;
							break;
						}
					}
					vec[i]++;
					stone--;
					if(flag==0){
						who = i;
						flag = 2;
					}	
				}else{
					stone += vec[i];
					vec[i] = 0;
				}
				if(flag==2){ break;	}
			}
			if(flag==2){ break;	}
		}
		cout << who << endl;
	}	
	return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
  int n,p;
  int m[100];

  while( cin>>n>>p && (n|p) ){
    for( int i=0;i<n;i++ )
      m[i]=0;

    int z=0;
    while(true){
      if( p>0 ){
	p--;
	m[z]++;
	if(p==0){
	  bool f=true;
	  for( int i=0;i<n;i++){
	    if( i==z)continue;
	    if( m[i]>0 ){
	      f=false;
	      break;
	    }
	  }
	  if( f ){
	    cout << z << endl;
	    break;
	  }
	}
      }else{
	if( m[z]>0 ){
	  p+=m[z];
	  m[z]=0;
	}
      }
      z=(z+1)%n;
    }

  }
  return 0;
}

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

void main() {
	int n, p;

	while (1) {
		int flag = 0;
		int player[50] = { 0 };
		cin >> n >> p;
		if (n == 0 && p == 0) break;
		int p_max = p;

		while (1)
		{
			for (int j = 0; j < n; j++) {
				if (p != 0) player[j] += 1, p--;
				else p = player[j], player[j] = 0;
				if (player[j] == p_max) {
					flag = -1;
					cout << j << endl;
					break;
				}
			}
			if (flag == -1) break;
		}
	}
}