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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <stdio.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define MT(a,b,c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second


//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define EPS 1e-8

int a[55];

int main(){
	while(1){
		int n,m,tmp,i=0;
		cin>>n>>m;
		tmp=m;
		if(n==0)return 0;
		memset(a, 0, sizeof(a));
		while(1){
			if(tmp>0){
				a[i]++;
				tmp--;
				if(a[i]==m){
					cout<<i<<endl;
					break;
				}
			}else{
				tmp=a[i];
				a[i]=0;
			}
			i++;
			if(i==n)i=0;
		}
	}
}

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

int main(){
    int n,p;
    while(cin>>n>>p){
        if(n==0 and p==0) return 0;
        vector<int> d(n,0);
        int pp = p;
        int t = 0;
        while(1){
            if(p>0){
                p--;
                d[t]++;
            }else{
                p += d[t];
                d[t] = 0;
            }
            if(d[t]==pp){
                cout << t << endl;
                break;
            }
            t = (t+1)%n;
        }
    }
}

C++: #include <iostream>
#include <unistd.h>
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;}
      }
    }

    usleep(1000000);

    cout << ans << endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int n,p;
  while(cin>>n>>p,n+p) {
    int i=0;
    int wan = p;
    vector<int> a(n);
    
    while(true) {
//      cout << i << " " << a[i] << " "<<wan << endl;
      if (wan) {
        wan--;
        a[i]++;
        if (wan==0&&a[i]==p)
          break;
      } else {
        wan = a[i];
        a[i]=0;
      }
      i++;
      if (i>=n) i=0;
    }
    cout << i << endl;
  }
}

C++: #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
  
int main()
{
	int n,m;
	int p[50]={0};
	int i,j;
	int ans;

	while(1){
	scanf("%d %d",&n,&m);
	if((n==0)&&(m==0))
		break;
	//ここまでテンプレ
	j=m;
	ans=-1;
	while(1){
		for(i=0;i<n;i++){
			if(j>0){
				p[i]++;
				j--;
				if(p[i]==m){
					ans=i;
					break;
				}
			}else if(j==0){
				j+=p[i];
				p[i]=0;
			}

		}
		if(ans>-1)
			break;
	}

	printf("%d\n",ans);


	//ここからテンプレ
	}
	return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int main(void)
{
	int n,r,p,c,i,j,a[50],x,pp;
	while(1)	{
		scanf("%d %d",&n,&p);
		if(n==0 && p==0)	break;
		for(i=0;i<n;i++)	a[i]=0;
		x=0;
		pp=p;
		while(1)	{
			if(pp!=0)	{
				a[x]++;
				pp--;
			}
			else {
				pp=a[x];
				a[x]=0;
			}
			if(a[x]==p)	{
				break;
			}
			x++;
			x=x%n;
		}
		printf("%d\n",x);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

int n, p;

int main() {
    while(1) {
        cin >> n >> p;
        if(n == 0 && p == 0) break;
        vector<int> stone(n, 0);
        int now = 0;
        int wan = p;
        while(1) {
            if(wan == 0) {
                wan = stone[now%n];
                stone[now%n] = 0;
            }else {
                stone[now%n]++;
                wan--;
                if(wan == 0) {
                    bool flg = true;
                    for(int i = 0; i < n; ++i) {
                        if(i == now%n) continue;
                        if(stone[i] != 0) {
                            flg = false;
                            break;
                        }
                    }
                    if(flg) {
                        cout << now%n << endl;
                        break;
                    }
                }
            }
            now++;
        }
    }
}

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

int candidate[50];

int main() {
    int n,p;
    while(cin>>n>>p, n|p) {
        for(int i=0; i<50; ++i) candidate[i] = 0;
        int count = 0,cup = p,idx = 0,stepc = 0;
        while(true) {
            stepc++;
            if(cup > 0) {
                candidate[idx]++;
                cup--;
                if(candidate[idx] == 1) count++;
                //if(cup == 0) cout<<idx<<endl;
                if(cup == 0 && count == 1) {
                    cout<<idx<<endl;
                    break;
                }
            }else{
                if(candidate[idx] > 0) count--;
                cup = candidate[idx];
                candidate[idx] = 0;
            }
            idx = (idx+1)%n;
            //if(stepc >= 1000000) cout<<"efaw"<<endl;
        }
    }
}