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 <stdio.h>
#include <stdlib.h>

int game(int n,int p,int *data){
	int i,p2;

	i = 0;
	p2 = p;

	while(1){
		if(p2 == 0){
			p2 += data[i];
			data[i] = 0;
		}else{
			p2--;
			data[i]++;
			if(data[i] == p){
				break;
			}
		}
		i = (i + 1) % n;
	}

	return i;
}

int main(){
	int p,n;
	int *data;

	while(1){
		scanf("%d %d",&n,&p);
		if(n == 0 && p == 0){
			break;
		}

		data = (int *)calloc(n,sizeof(int));

		printf("%d\n",game(n,p,data));
	}

	return 0;
}

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

int main()
{
  int n,p;

  for(;cin>>n>>p,n||p;)
    {
      int count[n];
      memset(count,0,sizeof(count));
      int pp=p;
      for(int i=0;;i++)
	{
	  if(i>=n)
	    i=0;
	  if(pp!=0)
	    {
	      count[i]++;
	      pp--;
	    }
	  else
	    {
	      pp=count[i];
	      count[i]=0;
	    }
	  if(count[i]==p)
	    {
	      cout<<i<<endl;
	      break;
	    }
	}
      
    }
}

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

int main()
{
  while(true){
    int n,p;
    scanf("%d%d",&n,&p);
    if(n==0&&p==0)break;
    int pebbles[MAX]={0,};
    int left=p;
    int now=0;
    int winner=-1;
    while(winner<0){
      if(left>0){
	--left;
	pebbles[now]++;
      }else{
	left+=pebbles[now];
	pebbles[now]=0;
      }
      now++;
      if(now==n)now=0;
      for(int i=0;i<MAX;++i){
	if(pebbles[i]==p){
	  winner=i;
	  break;
	}
      }
    }
    printf("%d\n",winner);
  }
  return 0;
}

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

int main() {
  int p, ini_p;
  long long i = 0, candidate;

  cin >> candidate;
  cin >> p;
  while (candidate != 0) {
    int n[50] = {0};
    ini_p = p;
    for (i = 0; n[i%candidate] != ini_p; i++) {
      if (p == 0) {
        p = n[i%candidate];
        n[i%candidate] = 0;
      } else if (p > 0) {
        p--;
        n[i%candidate]++;
      }
      // cout << p << n[0] << n[1] << n[2] << endl;
    }
    cout << i % candidate << endl;
    cin >> candidate;
    cin >> p;
  }
  return 0;
}

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

using namespace std;

#define LOG(...) fprintf(stderr,__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define RFOR(i,a,b) for(int i=(int)(b-1);i>=(int)(a);--i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define RREP(i,n) for(int i=(int)(n-1);i>=0;--i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x)*(x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

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

C++: #include <stdio.h>
#include <cmath>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;

int main(void) {
	vector<int> r;
	while (true)
	{
		int n=0,p=0;
		scanf("%d %d",&n,&p);
		if (n==0 && p==0){
			break;
		}
		int wan=p,candidate[50]={0};
		int i=0;
		while (true)
		{
			if (wan > 0){
				wan--;candidate[i]++;
			} else {
				wan+=candidate[i];candidate[i]=0;
			}
			if (candidate[i]==p){break;}
			i++;
			i=i%n;
		}
		r.push_back(i);
	}
	for(int i=0; i<r.size(); i++)
	{
		cout << r[i] << endl;
	}
	return 0;
}

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

int n, p;

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	while(1){
		cin >> n >> p;
		if(n == 0 && p == 0) return 0;
		int a[n] = {};
		int t = p;
		int cnt = 0;
		while(1){
			if(t > 0){
				--t;
				++a[cnt%n];
				if(t == 0 && a[cnt%n] == p){
					cout << cnt%n << "\n";
					break;
				}
			}else{
				t += a[cnt%n];
				a[cnt%n] = 0;
			}
			++cnt;
		}
	}
}

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

int main(int argc, char const *argv[])
{
	int n,p;
	int s;
	int i1;

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

		vector<int> v(n);
		i1=0;
		s=p;

		while(1){
			if (s>0){
				v[i1]++;
				s--;	
			}
			else {
				s=v[i1];
				v[i1]=0;
			}

			if (v[i1] == p) break;

			i1++;
			if(i1==n) i1=0;
		}

		cout<<i1<<endl;

	}
	return 0;
}

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

using namespace std;

typedef long long int ll;
typedef pair<int,int> P;

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

const ll mod = 1e9+7;
const ll INF = 1e9;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int simulation(int n,int p){
	int res=p;
	vector<int> a(n);
	int pos=0;
	while(p>0){
		a[pos]++;
		p--;
		pos++;
		pos%=n;
	}

	while(true){
		if(p==0){
			p+=a[pos];
			a[pos]=0;
		}else{
			p--;
			a[pos]++;
		}
		if(a[pos]==res)break;

		pos++;
		pos%=n;
	}
	return pos;
}

int main()
{
	int n,p;
	while(cin>>n>>p,(n!=0&p!=0)){
		cout<<simulation(n,p)<<endl;
	}

	// int n=10;

	// repi(p,2,51){
	// 	cout<<p<<" "<<simulation(n,p)<<endl;
	// }

	return 0;
}

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

int main() {
	int n, p;
	while(cin >> n >> p && (n || p)){
		int stone[51] = {0}, bowl = p, turn = 0, ans = -1;
		while(1){
			if(bowl > 0){
				bowl -= 1;
				stone[turn]++;
			}
			else{
				bowl += stone[turn];
				stone[turn] = 0;
			}
			
			if(stone[turn] == p){
				ans = turn;
				break;
			}
			turn++;
			if(turn == n)
				turn = 0;
		}
		cout << ans << endl;
	}
	return 0;
}