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, i, j;
	int *ishi;

	// ループ
	cin >> n >> p;
	while((n != 0) || (p != 0)){
		bool isEndOfTheGame = false;
		// 入力のチェック
		if((n < 3) || (n > 50) || (p < 2) || (p > 50)){
			cerr << "入力が範囲外です" << endl;
			continue;
		}
		// メモリ領域の確保
		ishi = new int[n];
		// 確保できたかチェック
		if(ishi == NULL){
			cerr << "メモリ確保エラー" << endl;
			return -1;
		}
		// メモリの初期化
		for(i=0;i<n;i++){
			ishi[i] = 0;
		}
		// ゲーム
		for(i=0;(p > 0) || (isEndOfTheGame == false);i++){
			if(p > 0){
				p--;
				ishi[(i % n)]++;
			}else if(p == 0){
				p += ishi[(i % n)];
				ishi[(i % n)] = 0;
			}else{
				cerr << "バグ" << endl;
				delete [] ishi;
				return -1;
			}
			// ゲームの終了判定
			isEndOfTheGame = true;
			for(j=0;j < (i % n);j++){
				if(ishi[j] > 0){
					isEndOfTheGame = false;
					break;
				}
			}
			for(j++;j<n;j++){
				if(ishi[j] > 0){
					isEndOfTheGame = false;
					break;
				}
			}
		}
		// 結果の表示
		cout << ((i - 1) % n) << endl;
		// メモリ領域の解放
		delete [] ishi;
		// 入力
		cin >> n >> p;
	}

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

int winner(int n,int p);

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

        int ans=winner(n,p);

        cout<<ans<<endl;
    }

    return 0;
}


int winner(int n,int p){

    int cand[n];    for(int i=0;i<n;i++)cand[i]=0;
    int stoneSum=p;

    int step=0;
    int winnerNum=-1;
    while(winnerNum<0){
        for(int i=0;i<n;i++){
            if(cand[i]==stoneSum){
                winnerNum=i;
                break;
            }

            if(p>0){
                cand[i]++;
                p--;
                continue;
            }else{
                p+=cand[i];
                cand[i]=0;
                continue;
            }
        }

    }
    return winnerNum;

}

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

using namespace std;

int n[55]={};
int p[55] = {};
int have[55] = {};
int winner[100]={};
int judge=0;

int main()
{
    int i=0;
    while(1){
        scanf("%d %d",&n[i],&p[i]);
        if(n[i]==0 && p[i] == 0){
            break;
        }
        i++;
    }
    int times = i;

    for(i=0;i<times;i++){
        int pebble = p[i];
        for(int j=0;j<n[i];j++){
            have[j]=0;
        }
        while(1){
            for(int j=0;j<n[i];j++){
                if(pebble != 0){
                    have[j]++;
                    pebble--;
                    if(have[j] == p[i]){
                        winner[i] = j;
                        judge = 1;
                        break;
                    }
                }else{
                    pebble += have[j];
                    have[j] = 0;
                }
            }
            if(judge == 1){
                cout << winner[i] << endl;
                judge = 0;
                break;
            }
        } 
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#define rep(i,l,n) for(lint i=l;i<n;i++)
#define rer(i,l,n) for(lint i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int main(){
	int n, p;
	while (1){
		cin >> n >> p;
		vi d(55);
		if (n == 0 && p == 0) break;
		int ans = 0, now = 0, np = p;
		for (int i = 0;; i++){
			if (np){
				d[now]++;
				np--;
				if (np == 0 && d[now] == p){
					ans = now; break;
				}
			}
			else{
				np = d[now];
				d[now] = 0;
			}
			now++;
			if (now == n) now = 0;
		}
		o(ans);
	}
}

C++: #include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main(void){
	int n,p;
	int a[51];

	cin >> n; 
	cin >> p; 

	while (n != 0 || p != 0) {
		int fin = 0;
		int winner = 0;

		for (int i = 0; i < 51; i++) {
			if (i == 50) {
				a[i] = p;
			}else{
				a[i] = 0;
			}
		}
		while (!fin) {
			for (int i = 0; i < n && !fin; i++) {
				if (a[50] != 0) {
					a[i] += 1;
					a[50] -= 1;
				}else{
					a[50] += a[i];
					a[i] = 0;
					if (a[50] == p) {
						fin = 1;
						winner = i;
					}
				}
			}
		}
		cout << winner << endl;
		fin = 0;
		
		cin >> n;
		cin >> p;
	}

	return 0;
}

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

using namespace std;

#define REP(i,n) for(int i = 0;i < n ; i++)
	
int people[100];


bool judge(int n,int a)
{
	REP(i,n)
	{
		if(people[i] != 0 && i != a)return false;
	}
	
	return true;
}



int main()
{
	int n,p;
	
	while(cin >> n >> p,n)
	{
		int flag = false;
		REP(i,100)people[i] = 0;
		for(int i = 0;!flag;i++)
		{
			//cout << p << endl;
			if(p == 0)
			{
				p = people[ i % n ];
				people[i % n] = 0;
			}
			else
			{
				people[i % n]++;
				p--;
				if(p == 0)
				{

					if(judge(n,i%n))
					{
						flag = true;
						cout << i%n << endl;
					}
				}
			}
		}
		
	}
	
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>

#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>

#include <numeric>
#include <algorithm>
#include <functional>

#include <cctype>

#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define rep(i,n) for(unsigned int i=0;i<(n);i++)
#define tr(it,container) for(typeof(container.begin()) it = container.begin(); \
                             it != container.end(); ++it)

typedef long long ll;
typedef complex<double> P;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};
const double EPS = 1e-9;
const int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
const int daysleap[] = {31,29,31,30,31,30,31,31,30,31,30,31};


int main(){
    while(true){
        int n,p;
        cin >> n >> p;
        if(n == 0 and p == 0) break;
        int inip = p;
        vector<int> V(n);
        int now = 0;
        while(true){
            if(p == 0){
                if(*max_element(all(V)) == inip){
                    cout << max_element(all(V)) - V.begin() << endl;
                    break;
                }else{
                    p += V[now];
                    V[now] = 0;
                }
                now = (now+1)%n;
            }else{
                V[now]++;
                p--;
                now = (now+1)%n;
            }
        }
    }
    return 0;

}