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 <cstring>

using namespace std;

#define repeat(i,n) for(int i=0;i<(n);i++)
#define N_MAX 50

int n, p;
int cup;
int cur_player;
int player[N_MAX];

bool finished() {
    if (cup != 0) {
        return false;
    }
    repeat(i,n) {
        if (player[i] == p) {
            return true;
        }
    }
    return false;
}

void step() {
    if (cup == 0) {
        cup = player[cur_player];
        player[cur_player] = 0;
    } else {
        cup--;
        player[cur_player]++;
    }
    cur_player++;
    if (cur_player == n) {
        cur_player = 0;
    }
}

int main() {
    while (cin >> n >> p) {
        if (n == 0 && p == 0) break;
        memset(player, 0, sizeof(player));
        cup = p;
        cur_player = 0;
        while (!finished()) {
            step();
        }
        int winner;
        repeat(i, n) {
//            cout << player[i] << ' ';
            if (player[i] == p) winner = i;
        }
        cout << winner << endl;
    }
    return 0;
}

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

using namespace std;

int n=0;
int p=0;
int have[55] = {};
int winner=0;
int judge=0;

int main()
{
    while(1){
        scanf("%d %d",&n,&p);
        if(n==0 && p == 0){
            break;
        }
        int pebble = p;
        for(int j=0;j<n;j++){
            have[j]=0;
        }
        while(1){
            for(int j=0;j<n;j++){
                if(pebble != 0){
                    have[j]++;
                    pebble--;
                    if(have[j] == p){
                        winner = j;
                        judge = 1;
                        break;
                    }
                }else{
                    pebble += have[j];
                    have[j] = 0;
                }
            }
            if(judge == 1){
                cout << winner << endl;
                judge = 0;
                break;
            }
        } 
    }

    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define FORR(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define CHMIN(a,b) (a) = min((a),(b))
#define CHMAX(a,b) (a) = max((a),(b))

int main()
{
    int n,p;
    while(1==1)
    {
        scanf("%d %d\n",&n,&p);
        if(n==0&&p==0)return 0;
        int ko[55]={};
        int zan=p;
        int poi=0;
        int judge=0;
        while(judge==0)
        {
            for(int i=0;i<n;i++)ko[i]+=zan/n;
            zan-=(zan/n)*n;
            while(zan>0)
            {
                ko[poi]++;
                if(ko[poi]==p)
                {
                    printf("%d\n",poi);
                    judge=1;
                    break;
                }
                zan--;
                poi++;
                if(poi>=n)poi-=n;
            }
            if(judge==1)break;
            while(zan==0)
            {
                if(ko[poi]>0)
                {
                    zan=ko[poi];
                    ko[poi]=0;
                }
                poi++;
                if(poi>=n)poi-=n;
            }
        }
    }
    return 0;
}

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

C++: #include <iostream> // C++の入出力
#include <stdio.h> // C の入出力
#include <algorithm> // sort などのアルゴリズム
#include <string.h> // C の文字列用
#include <ctype.h> // C の文字種用
#include <string> // C++の文字列
using namespace std;
int main(void){
    while(1){
        int n,p,step=0;
        cin>>n>>p;
        if(n==0&&p==0)break;
        int data[n];
        for(int i=0;i<n;i++)data[i]=0;
        int box=p;
        for(int i=0;;i++){
            if(i==0){
                data[0]+=1;
                box-=1;
                continue;
            }
            if(box>0){
                data[i%n]++;
                box--;
            }else if(box==0){
                box+=data[i%n];
                data[i%n]=0;
            }
            if(box==0&&data[i%n]==p){
                step=i%n;
                break;
            }
        }
        cout<<step<<endl;
    }
    return 0;
}

C++: #ifndef _WIN32
#include<iostream>
#endif // !_WIN32

#include<algorithm>
#include<vector>
#include<string>
#include<iomanip>
#include<map>
using namespace std;
#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
int main() {
	int n,p;
	while (cin >> n>>p, n+p) {
		int q = p;
		vector<int> a(n);
		for (int i=0;;(i+=1)%=n) {
			if (p == 0) {
				p = a[i]; a[i] = 0;
			}
			else {
				a[i] ++;
				p--;
				if (a[i] == q) { cout << i << endl; break; }
			}
		}
	}

}

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

#define rep(i, n) for(int i = 0; i < (n); i++)

using namespace std;

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

int main(){
	//freopen("test.txt", "r", stdin);
	int n, p;
	while(cin >> n >> p, n|p){
		vi v(n, 0);
		int turn = 0;
		while(1){
			if(p){
				p--;
				v[turn]++;
			}else{
				p = v[turn];
				v[turn] = 0;
			}
			if(!p){
				int cnt = 0;
				rep(i, n){
					cnt += (v[i]!=0);
				}
				if(cnt == 1)break;
			}
			turn = (turn+1)%n;
		}
		rep(i, n){
			if(v[i]) cout << i << endl;
		}
	}


	return 0;
}

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

int check(vector<int>& hand){
	int winner =-1;
	for(int i=0;i<hand.size();i++){
		if(hand[i]!=0){
			if(winner==-1){
				winner=i;
			}else{
				return -1;
			}
		}
	}
	return winner;
}

int main(){
	while(true){
		int n,p,pos=0;
		vector<int> hand;
		cin>>n>>p;
		if(n==0)break;
		hand.resize(n);
		while(true){
			int tmp;
			tmp=p/n;
			for(vector<int>::iterator it=hand.begin();it!=hand.end();++it)*it+=tmp;
			for(tmp=p%n;tmp>0;tmp--){
				hand[pos]++;
				pos==n-1?pos=0:pos++;
			}
			tmp=check(hand);
			if(tmp!=-1){
				cout<<tmp<<endl;
				break;
			}
			p=hand[pos];
			hand[pos]=0;
			pos==n-1?pos=0:pos++;
		}
	}
	return 0;
}

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

int main(void){
  while(1){
    int stone[50];
    for(int k = 0 ; k < 50 ; k++){stone[k] = 0;}

    int bowl;
    int n, p;  // n：候補者人数，p：小石の総数
    cin >> n;
    cin >> p;

    if(n == 0 && p == 0){return 0;}

    bowl = p;

    bool flg = true;
    int ans;

    for(int i = 0 ; flg ; i = (i+1) % n){

      if(bowl == 0){
        bowl = bowl + stone[i];
        stone[i] = 0;
      }else{
        stone[i]++;
        bowl--;
        if(bowl == 0 && stone[i] == p){ans = i; flg = false;}
      }
    }

    for(int i = 0 ; i < 10000000 ; i++){
        for(int j = 0 ; j < 10000000 ; j++){
            for(int k = 0 ; k < 10000000 ; k++){
                int K = i + j + k;
            }
        }    
    }


    cout << ans << endl;
  }
}