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 <algorithm>
#include <utility>

int doit(int n, int p) {
    std::vector<int> tokens(n);
    int current = p;
    int index = 0;

    while(true) {
        if (current == 1) {
            bool end = true;
            for (int i = 0; i < n; i++) {
                end = end && (index == i || !tokens[i]);
            }
            if (end) {
                return index;
            }
        }
        if (current != 0) {
            tokens[index]++;
            current--;
            index = (index + 1) % n;
        } else {
            current = tokens[index];
            tokens[index] = 0;
            index = (index + 1) % n;
        }
    }
}

int main() {
    int n, p;
    while (std::cin >> n >> p) {
        if (n == 0 && p == 0) {
            break;
        } else {
            std::cout << doit(n, p) << std::endl;
        }
    }
}

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

int main(void)
{
	int n, p;
	while (cin >> n >> p, n && p){
		int stone[n];
		int now = 0, all = p, ret = 0;
		for (int i = 0; i < n; i++){
			stone[i] = 0;
		}
		while (1){
			if (all == 0){
				all += stone[now];
				stone[now] = 0;
			}
			else {
				all--;
				stone[now]++;
				if (all == 0 && stone[now] == p){
					ret = now;
					break;
				}
			}
			now++;
			now %= n;
		}
		cout << ret << endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <tuple>
#include <cassert>
#include <exception>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define sz size()
#define print(X) cout << (X) << endl
static const int INF = 1e+9+7;
ll n,m,l;
string s,t;
int d[200010],dp[1010][1010];
double w[1000],v[1000];
double box[200010];

int main(){
  while(1){
    cin >> n >> l;
    if(n == 0 && n == l)break;
    clr(d,0);
    m = 0;
    int mx = l;
    while(1){
      bool f = false;
      rep(i,0,n){
        if(!l){
          l += d[i];
          d[i] = 0;
          continue;
        }
        l--; d[i]++;
        if(d[i] == mx){
          m = i;
          f = true;
          break;
        }
      }
      if(f)break;
    }
    print(m);
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
//ranker
using namespace std;

#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using pis = pair<int, string>;
using psi = pair<string, int>;
using D = double;


int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
    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 <bits/stdc++.h>
using namespace std;

int main(){
    int n,p;

    while(cin>>n>>p,n|p){
        int c=p;
        int res;
        vector<int> v(n,0);
        for(int i=0;i<n;i++){
            if(c>0){
                if(++v[i] == p){
                    res=i;
                    break;
                }
                c--;
            }else{
                c+=v[i];
                v[i]=0;
            }
            if(i==n-1) i=-1;
        }

        cout << res << endl;
    }
}

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

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

using namespace std;

//n:候補者の人数,p:最初にお椀に入っている小石の数
int solve(int n,int p){
	int *pebbles = new int[n];//それぞれの候補者の持っている小石の数
	fill(pebbles,pebbles+n,0);
	int pos=n-1;//お椀の場所。スタートしたら0の人に渡す
	int inpebble = p;//お椀に入っている小石の数
	while(pebbles[pos] != p){
		pos= (pos+1)%n;
		if(inpebble==0){//お椀が空っぽ
			inpebble += pebbles[pos];
			pebbles[pos] = 0;
		}else{
			inpebble --;
			pebbles[pos]++;
		}
	}
	delete[] pebbles;
	return pos;
}

int main(){
	int n,p;
	while(true){
		cin >> n >> p;
		if(n==0 && p==0){
			break;
		}
		cout << solve(n,p) << endl;
	
	}

}

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

C++: #include<stdio.h>

int main(void)
{
	int n, p;
	int i, t, k;
	int num[1000][50];
	int wan[1000];
	int ans[1000];
	int lose;
	int change;
	for (i = 0; i < 1000; i++)
	{
		lose = 0;
		scanf("%d %d", &n, &p);
		if (n == 0 && p == 0)
		{
			break;
		}
		for (t = 0; t < n; t++)
		{
			num[i][t] = 0;
		}
		wan[i] = p;
		while (1)
		{
			if (lose == n - 1)
			{
				break;
			}
			
			for (t = 0; t < n; t++)
			{
				lose = 0;
				if (wan[i] == 0)
				{
				
					if (num[i][t] == 0)
					{
						continue;
					}
					else if (num[i][t] != 0)
					{
					
						change = num[i][t];
						wan[i] = change;
						num[i][t] = 0;
						continue;
					}
				}

				wan[i]--;
				num[i][t]++;
				if (wan[i] == 0)
				{
					for (k = 0; k < n; k++)
					{
						if (num[i][k] == 0)
						{
							lose++;
						}
					
					}
				}
				if (lose == n - 1)
				{
					break;
				}
			}
		}
		for (t = 0; t < n; t++)
		{
			if (num[i][t] != 0)
			{
				ans[i] = t;
				if (ans[i] == -1)
				{
					ans[i] = n - 1;
				}
			}
		}
	}
	for (k = 0; k < i; k++)
	{
		printf("%d\n", ans[k]);
	}
	return 0;
}