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>
#include <vector>
#include <algorithm>
using namespace std;
 
int main() {
    int n, p;
    while(cin >> n >> p, n) {
        const int total_p = p;
        int current_player = 0;
        vector<int> v(n);
        int winner = 0;
        while(true) {
            if(p > 0) {
                ++v[current_player];
                --p;
                current_player = (current_player+1)%n;
            } else if(p == 0) {
                if(v[current_player] == total_p) {
                    winner = current_player;
                    break;
                } else {
                    p += v[current_player];
                    v[current_player] = 0;
                    current_player = (current_player+1)%n;
                }
            }
        }
        cout << winner << endl;
    }
}

C++: //34
#include<iostream>

using namespace std;

int main(){
  for(int n,p;cin>>n>>p,n|p;){
    int player[50]={};
    int w=p;
    int i;
    for(i=0;;i=(i+1)%n){
      if(w){
	w--;
	player[i]++;
	if(player[i]==p)break;
      }else{
	w=player[i];
	player[i]=0;
      }
    }
    cout<<i<<endl;
  }
  return 0;
}

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

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

    vector<int> stone(n, 0);
    int man = 0, wan = p;
    
    while(true){
      if(wan != 0){
	wan--;
	stone[man]++;

	if(wan == 0){
	  bool flag = true;
	  for(int i=0; i<n; i++){
	    if(i != man && stone[i] != 0)
	      flag = false;
	  }
	  if(flag == true)
	    break;
	}
      }else{
	wan = stone[man];
	stone[man] = 0;
      }

      man = (man+1)%n;
    }

    cout << man << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p){
		if(n==0 && p==0) break;
		int stone[51]={0};
		int wan=p,j=0,winner;
		while(1){
			if(wan>0){
				stone[j]++;
				wan--;
			}else{
				wan=stone[j];
				stone[j]=0;
			}
			if(wan==p){
				winner=j;
				break;
			}
			j++;
			if(j==n) j=0;
		}
		cout<<winner<<endl;
	}
	return 0;
}

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

using namespace std;

typedef long long LL;
typedef vector<int>    VI;
typedef vector<bool>   VB;
typedef vector<string> VS;

#define MP make_pair
#define PB push_back
#define ALL(x)  (x).begin(),(x).end()
#define rep(i, N)  for (int i = 0; i < (int)(N); i++)
#define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

int n, p;
VI as;

int check(int c) {
	rep (i, as.size()) {
		if (as[i] == c) return i;
	}
	return -1;
}

int main(void) {
	while (1) {
		as.clear();
		cin >> n >> p;
		if (n == 0 && p == 0) break;
		as.resize(n);
		int ans;
		int c = p;
		rep (i, 1000010) {
			if ((ans = check(c)) != -1) {
				break;
			}
			if (p > 0) {
				as[i % n]++;
				p--;
			} else {
				p = as[i % n];
				as[i % n] = 0;
			}
		}
		cout << ans << endl;
	}
    return 0;
}

C++: #include<iostream>

using namespace std;

int main(){
  int n, p;

  while(cin >> n >> p){
    if(n == 0 && p == 0) break;
    int max = p;
    int num = 0;
    int data[n];
    for(int i = 0; i < n; i++) data[i] = 0;

    while(1){
      if(p == 0){
	p = data[num];
	data[num] = 0;
      }else{
	data[num]++;
	p--;
	if(data[num] == max){
	  cout << num << endl;
	  break;
	}
      }
      num++;
      if(num == n)num = 0;
    }
  }
  return 0;
}

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

int main(void){
    while(true){
        int n, p;
        cin >> n >> p;

        if(n == 0 && p == 0){ break; }

        int bucket = p;
        vector<int> stone(n, 0);
        for(int i = 0; ; i = (i + 1) % n){
            if(bucket ==0){
                bucket += stone[i];
                stone[i] = 0;
            }
            else{
                --bucket;
                ++stone[i];

                if(stone[i] == p){
                    cout << i << endl;
                    break;
                }
            }
        }
    }

    return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
typedef unsigned int ui;
int main() {
	int n, q;
	while (cin >> n >> q) {
		if (n == 0 && q == 0)break;
		int wan=q;
		int a[50] = {};
		int j=0;
		while (true) {
			if (wan == 0) {
				wan = a[j]; a[j] = 0;
			}
			else {
				wan--; a[j]++;
			}
			if (a[j] == q) {
				cout << j << endl; break;
			}
			j=(j+1)%n;
		}
	}
	return 0;
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define vec vector

int main(void)
{
	while(1) {
		int n, p;
		cin >> n >> p;
		if (!n) break;
		int init_p = p;
		int winner = 0;
		vec<int> rock(n, 0);
		while(1) {
			if (p > 0) {
				p--;
				rock[winner]++;
			} else {
				p += rock[winner];
				rock[winner] = 0;
			}
			if (!p && rock[winner] == init_p) break;
			winner++;
			if (winner == n) winner = 0;
		}
		cout << winner << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
  while (1){
    int n, p;
    cin >> n >> p;
    if (n == 0 && p == 0){
      break;
    }
    int p2 = p;
    vector<int> cnt(n, 0);
    int x = 0;
    while (1){
      if (p == 0){
        p = cnt[x];
        cnt[x] = 0;
      } else {
        p--;
        cnt[x]++;
        if (cnt[x] == p2){
          cout << x << endl;
          break;
        }
      }
      x++;
      if (x == n){
        x = 0;
      }
    }
  }
}