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>
using namespace std;
int main()
{
    int n, p;
    while (cin >> n >> p && n != 0 && p != 0) {
        int rem = p;
        int players[n] = {0}, i = 0;
        while (1) {
            if (--rem < 0) {
                rem = players[i];
                players[i] = 0;
            } else if (++players[i] == p) {
                cout << i << endl;
                break;
            }
            i = ++i != n ? i : 0;
        }
    }
}

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


typedef struct {
    int stone;
} person;

int main(){
    int n, p;
    while (1){
	cin >> n >> p;
	if (n == 0 && p == 0) break;
	int def = p;
	person entree[50];
	for (int i = 0; i < 50; i++)
	    entree[i].stone = 0;

	int e = 0;

	int j = 0;
	while (1){
	    
	    if (entree[j%n].stone == def) {
		e = j%n;
		break;
	    }
	    if (p == 0){
		p = entree[j%n].stone;
		entree[j%n].stone = 0;
	    }
	    // take one stone
	    else {
		p--;
		entree[j%n].stone++;
	    }
	    j++;
	}

	
	cout << e << endl;
    }
}

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

const int N = 50;

int judge(int* a, int l){
	int s = 0;
	for(int i = 0;i < l;++i){
		s += a[i];
	}
	return s;
}

int main(){
	int n, p, i;
	int ss[N];
	while(cin >> n >> p, n){
		i = 0;
		fill(ss, ss + n, 0);
		while(1){
			if(!p){
				p += ss[i];
				ss[i] = 0;
			}
			else{
				++ss[i];
				--p;
			}
			if(!p){
				if(ss[i] == judge(ss, n)){
					break;
				}
			}
			++i;
			i = i == n ? 0 : i;
		}
		cout << i << endl;
	}
	return 0;
}

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

int main()
}
	int n,p;
	while(cin >> n >> p, n||p)
	{
		int s=p;
		int snum[64] = {0};

		for(int i=0; true; i=(i+1)%n)
		{
			if(s)
			{
				snum[i]++;
				if(snum[i]==p)
				{
					cout << i << endl;
					break;
				}
			}
			else
			{
				s=snum[i];
				snum[i]=0;
			}
		}
		
	}
}

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

using namespace std;
int main(){
    int n,p;
    while(cin >> n >> p && n){
        vector<int> stone(n , 0);
        int cup=p;
        bool flag=true;
        while(flag){
            for(int i =0; i<n;i++){
                if (stone[i]==p)
                {
                    cout << i <<endl;
                    flag =false ;
                    break;
                }

                if(cup>0){
                    stone[i]++;
                    cup--;
                }else if(cup==0){
                    cup+=stone[i];
                    stone[i]=0;
                }
            }
        }
    }
    return 0;
}

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

#define DEBUG(x) cerr << #x << " = " << x << endl

int main()
{
    int n, p;
    while (cin>>n>>p, n||p) {
        int get[50]={0}, stone=p, win=-1, i=0;
        while (1) {
            if (stone==0) {
                stone+=get[i];
                get[i]=0;
            } else {
                stone--;
                get[i]++;
                if (stone==0) {
                    for (int j=0; j<n; j++) {
                        if (get[j] != 0 && i!= j){
                          goto end;
                        }
                    }
                    win = i; break;
                end:;
                }
            }
            i= (i+1)%n;
        }
        cout << win << endl;
    }
    return 0;
}

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

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define all(v) (v).begin(),(v).end()

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 51;

int a[MAX_N];

int main()
{
    while(1){
        int n,p;
        cin >> n >> p;
        if(n == 0){
            break;
        }
        rep(i,n){
            a[i] = 0;
        }
        int np = p;
        bool flag = false;
        int ans;
        while(1){
            rep(i,n){
                if(np == 0){
                    np = a[i];
                    a[i] = 0;
                }else if(np == 1){
                    a[i]++;
                    np--;
                    if(a[i] == p){
                        flag = true;
                        ans = i;
                        break;
                    }
                }else{
                    a[i]++;
                    np--;
                }
            }
            if(flag){
                break;
            }
        }
        cout << ans << "\n";
    }
	return 0;
}

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

int main() {
  while(true) {
    int ncandid, nstone;
    cin >> ncandid >> nstone;
    if(ncandid == 0 && nstone == 0) break;
    int lastGiveStoneC = -1;
    vector<int> candides(ncandid, 0);
    int count = 0;
    while(true) {
      for(int i = 0; i < ncandid; ++i) {
        candides[(lastGiveStoneC + 1 + i) % ncandid] += nstone / ncandid;
        if(i < nstone % ncandid) {
          ++candides[(lastGiveStoneC + 1 + i) % ncandid];
        } else if(nstone / ncandid == 0) {
          break;
        }
      }
      lastGiveStoneC += nstone + 1;
      nstone = candides[lastGiveStoneC % ncandid];
      candides[lastGiveStoneC % ncandid] = 0;
      (nstone == 0) ? (++count) : (count = 0);
      if(count == ncandid - 1) break;
    }
    cout << (lastGiveStoneC + 1) % ncandid << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
using namespace std;

typedef vector<int> Ivec;

int main() {
	int n, p;

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

		int turn = 0, wan = p;
		array<int, 50> mayer = {};
		while (1) {

			if (wan == 0) {
				wan = mayer[turn];
				mayer[turn] = 0;
			}
			else if (wan == 1 && mayer[turn]+1 == p) 
				break;
			else {
				wan--;
				mayer[turn]++;
			}
			turn++;
			turn -= ((int)(turn / n) * n);
		}
		printf("%d\n", turn);
	}

	return 0;
}

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

int main()
{
	int p_set,i; //p_set 最初の石の数　i 立候補者の数

	cin>>i>>p_set;
	while(i!=0 || p_set!=0){
		int n[50]={0},p=p_set;
		long int turn=0;
		while(1){
			if(p>0){
				n[turn%i]++;
				p--;
				if(p==0 && n[turn%i]==p_set){
					cout<<turn%i<<endl;
					break;
				}
			}else if(p==0){
				p=n[turn%i];
				n[turn%i]=0;
			}
			turn++;
		}
	cin>>i>>p_set;
	}
}