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 <bits/stdc++.h>
using namespace std;

signed
main(){
	int n,p;
	while(cin >> n >> p && !(n==0 && p==0))
	{
			vector<int> s(n,0);
			int index = 0;
			int bowl = p;

			while(1)
			{
					if(bowl == 0)
					{
							bowl += s[index];
							s[index] = 0;
					}
					else
					{
							s[index]++;
							bowl--;
					}

					if(s[index]==p)
					{
							cout << index << endl;
							break;
					}else{
							index = (index+1) % n;
					}
			}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)

int main(){
    int n,p;
    while(cin>>n>>p,n|p){
        int a[51]={};
        int P=p,i=0;
        while(1){
            if(p){
                p--;
                a[i]++;
            }else{
                p=a[i];
                a[i]=0;
            }
            if(a[i]==P)break;
            if(++i==n)i=0;
        }
        cout<<i<<endl;
    }
    return 0;
}

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

int main()
{
	int n, p;
	while (cin >> n >> p) {
		if (n == 0 && p == 0)
			break;

		int ans = -1, bp = p;
		vector<int> r(n, 0);
		for (int i = 0; ; ++i) {

			if (p == 0) {
				p = r[i%n];
				r[i%n] = 0;
			} else {
				--p;
				++r[i%n];
				if (p == 0) {
					for (unsigned int j = 0; j < r.size(); ++j) {
						if (r[j] == bp) {
							ans = j;
							break;
						}
					}

					if (ans != -1)
						break;
				}
			}

		}
		cout << ans << endl;
	}

	return 0;
}

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

#define rep(i,n) for(int i = 0; i < n; i++)
#define rrep(i,n) for(int i = n - 1; i >= 0; i--)
#define REP(i,k,n) for(int i = k; i < n; i++)

#define vi vector<int>
#define pb push_back
using namespace std;
typedef long long ll;

int main(){
    int n,p;
    while(cin >> n >> p,n) {
        int ar[51] = {};
        int i = 0;
        int np = p;
        while(true) {
            if(np != 0) {
                np--;
                ar[i % n]++;
                if(ar[i % n] == p) {
                    cout << i % n << endl;
                    break;
                }
            }else{
                np = ar[i % n];
                ar[i % n] = 0;
            }
            i++;
        }
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a); i<(b); i++)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define sort(v,n) sort(v,v+n);
#define vsort(v) sort(v.begin(),v.end());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int,int> p;
typedef pair<ll,ll> lp;
bool is_uruu(int y) {
        return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
}
const ll MOD=1e9+7;
const double PI=acos(-1.0);
//---------------------------------------------------------------------------//
int n,s,q,now=0;
int main(){
        while(1) {
                cin>>n>>s;
                if(n==0) return 0;
                deque<int> r(n,0);
out:
                while(1) {
                        if(s!=0) {
                                q=r.front()+1;
                                r.pop_front();
                                r.push_back(q);
                                s--;
                                now++;
                                if(now%n==0) {
                                        now=0;
                                }
                        }else if(s==0) {
                                int cunt=0;
                                for(int i=0; i<n; i++) {
                                        if(r[i]!=0) {
                                                cunt++;
                                        }
                                }
                                if(cunt==1) {
                                        cout<<now<<endl;
                                        goto out;
                                }
                                q=r.front();
                                r.pop_front();
                                r.push_back(0);
                                s+=q;
                                now++;
                                if(now%n==0) {
                                        now=0;
                                }
                        }
                }
        }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define N_PRIME 1000000007
#define ll int64_t


int main(void){
    while(true){
        int n,p;
        int n_stone[64] = {0};
        cin >> n >> p;
        int index = 0;
        int p_max = p;
        
        if( n == 0 && p == 0 )return 0;
        
        while( true ){
            if( p > 0 ){
                p--;
                n_stone[index]++;
            }else{
                p += n_stone[index];
                n_stone[index]=0;
            }
            
            if( p == 0 && n_stone[index] == p_max ){
                cout << index << endl;
                break;
            }
            
            index = ( index + 1 ) % n;
        }
    }
    
    return 0;
}