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 || p))
    {
        int a[n] = {0};
        int i = 0;
        while (true)
        {
            if (p == 0)
            {
                p = a[i];
                a[i] = 0;
            }
            else
            {
                p--;
                a[i]++;
                bool isZero = 0;
                if (p == 0)
                {
                    for (int j = 0; j < n; j++)
                    {
                        if (a[j] != 0 && i != j)
                        {
                            isZero = 1;
                        }
                    }
                    if (isZero == 0)
                    {
                        cout << i << endl;
                        break;
                    }
                }
            }
            i += 1;
            i %= n;
        }
    }
}

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

int n, p, np, id;

void PRINT(vector<int> &a) {
  cout << " -------" << endl;
  cout << "np : " << np << endl;
  for(int i = 0; i < n; ++i) {
    cout << "a[" << i << "]: " << a[i] << endl;
  }
  cout << endl;
}

bool step(vector<int> &a) {

  if(np > 0) {
    np--;
    a[id]++;

    if(np == 0 && count(a.begin(), a.end(), p) == 1) {
      return 1;
    }
  }
  else {
    np += a[id];
    a[id] = 0;
  }
  return 0;
}

int play() {
  vector<int> a(n, 0);
  id = 0;
  np = p;
  while(1) {
    if(step(a)) break;
    id++;
    id %= n;
  }
  return id;
}

int main() {
  while(cin >> n >> p && n && p) {
    cout << play() << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
using namespace std;

int main(){
int m,p;
int a[55]={0};
while(cin>>m>>p && m!=0){
int no=0,is=p;
while(1){
if(is>0){
is--;
a[no]++;
}
else if(a[no]>0){
is=is+a[no];
a[no]=0;
}
//cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<" "<<is<<" "<<no<<endl;
if(a[no]==p) break;

no=no+1;
no=no%m;

//cout<<no<<" "<<is<<endl;

}
cout<<no<<endl;
}

return 0;
}

C++: #include<iostream>
#include<climits>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<functional>
#include<stack>
#include<list>
#include<string>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define PII pair<int,int>
#define PCC pair<char,char>
#define PCI pair<char,int>
#define PIC pair<int,char>
#define MP make_pair
#define DEBUG(x) cout<<#x<<": "<<x<<endl;

int main()
{
	while (1)
	{
		int n, p,ans;
		cin >> n >> p;
		vector<int> S(n, 0);
		if (n == 0)break;
		int c = p;
		for (int i = 0;;)
		{
			if (c > 0)
			{
				S[i]++;
				c--;
			}
			else 
			{
				c = S[i];
				S[i] = 0;
			}
			if (c == 0 && S[i] == p)
			{
				ans = i;
				break;
			}
			if (i == n - 1)i = 0;
			else i++;
		}
		cout << ans << endl;
	}
	return 0;
}

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define pb            push_back
template<class T, class S> inline pair<T, S> mp(T x, S y){ return make_pair(x, y); }
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

static const int    INF = 1000000000;
static const ll     MOD = 1000000007LL;
static const double EPS = 1E-12;

int n, p;

int main()
{
  while(cin >> n >> p, n){
    ll mask = 0LL;
    int cnt = p;
    vi d(n, 0);
    bool ok = true;
    while(ok){
      rep(i, n){
        if(cnt){
          cnt -= 1;
          if((mask | 1LL << i) == (1LL << n) - 1LL){
            cout << i << endl;
            ok = false;
            break;
          }
          d[i] += 1;
          mask &= ~(1LL << i);
        }
        else{
          swap(cnt, d[i]);
          mask |= 1LL << i;
        }
      }
    }
  }

  return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 100000000

#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>

using namespace std;

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

static const double EPS = 1e-8;

const int tx[] = {-1,0,1,0};
const int ty[] = {0,1,0,-1};


bool check(map<int,int> hand,int step,int n){
	for(map<int,int>::iterator it = hand.begin(); it != hand.end(); it++){
		if(it->first == step % n) continue;
		if(it->second != 0) return false; 
	}
	return true;
}
int main(){
	int n,p;	
	while(~scanf("%d %d",&n,&p)){
		if(n==p && p==0) break;
		map<int,int> hand;
		int bowl = p;
		int step = 0;
		int res = 0;
		while(1){
			if(bowl > 0){			
				hand[step]++;
				bowl--;
				if(bowl == 0 && check(hand,step,n)){
					res = step;
					break;
				}
			}
			else if(bowl == 0){
				bowl += hand[step];
				hand[step] = 0;
			}
			step = (step+1) % n;
		}

		printf("%d\n",res);
	}
}

C++: #include<iostream>
using namespace std;
int main(){

  int n,q,wan,player[50],j;

  while(1){

    cin >>n>>q;

    if(n == 0 && q == 0)break;

    for(int i=0 ; i < n ; i++)player[i]=0;
    wan=q;
    j=0;
    
    while(1){
      
      if(wan != 0){
	player[j]++;
	wan--;
	if(wan == 0 && player[j] == q){
	  cout <<j<<endl; 
	  break;
	}
      }else{
	wan=player[j];
	player[j]=0;
      }   
      j++;
      if(j >= n)j=0;
    }
    
  }
  
  return 0;
}

C++: #include<cstdio>
using namespace std;
int main()
{
    int n,p1,p2,i,j,hum[50],flg[50],stok;
    for(;scanf("%d%d",&n,&p1),n+p1!=0;)
    {
        p2=p1;
        for(i=0;i<n;i++)
            hum[i]=0;
        for(i=0;;i++)
        {
            if(hum[i]==p1)
                break;
            if(p2==0)
            {
                p2=hum[i];
                hum[i]=0;
            }
            else if(p2!=0)
            {
                hum[i]++;
                p2--;
            }
            if(i==(n-1))
            {
                i=-1;
            }
        }
        printf("%d\n",i);
    }
    return 0;
}