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;

int main(void){
	int n,p,can[55],bowl,i;
	while(true){
		cin >> n >> p;
		if(n==0 && p==0){
			break;
		}
		for(i=0; i<n; i++){
			can[i]=0;
		}
		bowl=p;
		i=0;
		while(true){
			if(bowl==0){
				bowl = can[i];
				can[i] = 0;
			}else{
				bowl--;
				can[i]++;
				if(can[i]==p){
					break;
				}
			}
			i++;
			i%=n;
		}
		cout << i << endl;
	}
	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;}
      }
    }

    cout << ans << endl;
  }
}

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

void main() {
	int n, p;

	while (1) {
		int flag = 0;
		int player[50] = { 0 };
		cin >> n >> p;
		if (n == 0 && p == 0) break;
		int p_max = p;

		while (1)
		{
			for (int j = 0; j < n; j++) {
				if (p != 0) player[j] += 1, p--;
				else p = player[j], player[j] = 0;
				if (player[j] == p_max) {
					flag = -1;
					cout << j;
					break;
				}
			}
			if (flag == -1) break;
		}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <fstream>

using namespace std;
#define SZ(x) (int)(x).size()
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REPR(i,n) for(int i=(n)-1;i>=0;i--)
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase( unique(ALL(v)) , v.end());

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<bool> vb;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = (1 << 29);
const ll LINF = 1e18;
const double PI = acos(-1);

template<typename T>vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T, typename... Ts>auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); }
template<typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value != 0>::type
fill_v(U & u, const V... v) { u = U(v...); }
template<typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value == 0>::type
fill_v(U & u, const V... v) { for (auto& e : u) fill_v<T>(e, v...); }
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, P;
	cin >> N >> P;
	while (N != 0) {
		vi S(N);
		int cur = P;
		int ind = 0;
		while (true) {
			if (cur == 0) {
				cur += S[ind];
				S[ind] = 0;
			}
			else {
				S[ind]++;
				cur--;
				if (cur == 0 && S[ind] == P) {
					cout << ind << endl;
					break;
				}
			}
			ind++;
			ind %= N;
		}
		cin >> N >> P;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <math.h>
#include <complex>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <functional>
#include <assert.h>
#define int long
#define ll long long
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define SZ(x) x.size()
#define fi first
#define se second
const int INF = 1e9;
using namespace std;

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

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;}

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout<<fixed<<setprecision(20);

    while(true){
        int n,p; cin >> n >> p;
        if(n==0 && p==0) break;
        int A[n];
        rep(i,n) A[i]=0;
        int now=p;
        bool frag=false;
        while(true){
            rep(i,n){
                if(now==0){
                    now+=A[i];
                    A[i]=0;
                } else {
                    now--;
                    A[i]++;
                    if(A[i]==p) {
                        cout << i << endl;
                        frag=true;
                        break;
                    }
                }
            }
            if(frag) break;
        }
    }
}

C++: #include <cstdio>
#include <iostream>
using namespace std;
static const int MAX_N = 50;
int ps[MAX_N];

int main() {
    int n, p, tp, i;
    for (; ;) {
        scanf("%d %d", &n, &p);
        if (n == 0 && p == 0) break;
        fill(ps, ps+n, 0);
        i = 0, tp = p;
        for (; ; ) {
            if (tp > 0) {
                ps[i]++; tp--;
                if (ps[i] == p) break;
                i=(i+1)%n; continue;
            }
            else if (tp == 0) {
                tp = ps[i]; ps[i] = 0;
                i=(i+1)%n;
            }
        }
        printf("%d\n", i);
    }
    return 0;
}

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