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 <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define dbg(x) cout<<#x"="<<x<<endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000

int stone[50];

int main(){

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

    rep(i,n) stone[i]=0;
    int bowl=p;
    int idx=0;
    while(true){
      if(stone[idx]+1==p) break;
     //??????????????????
      while(bowl>0){
	stone[idx]++;
	bowl--;
	idx = (idx+1)%n;
      }

      //?¬???????????????£????????????
      while(stone[idx]==0) idx=(idx+1)%n;

      //????????\??????
      bowl=stone[idx];
      stone[idx]=0;
      idx= (idx+1)%n;
    }

    cout << idx << endl;

  }
}

C++: 


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include <functional>

using namespace std;

int INF = 10000000;

int n , p;

int k[50];


int main()
{

	while(cin >> n >> p && n && p)
	{
		
		memset(k,0,50*sizeof(int));
		int turn = 0;
		int pmax = p;

		while(1)
		{
			if(p > 0)
			{
				k[turn]++;p--;
				if(k[turn] == pmax)break;

			}else
			{
				p = k[turn];
				k[turn] = 0;
			}

			turn++;
			turn = turn % n;
		}

		cout << turn << endl;
	}

  return 0;
}

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

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <complex>
using namespace std;

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

#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp make_pair
#define ERASE(v,i) (v).erase(remove(all(v),i),(v).end())
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define debug(x) cerr<<#x<<" = "<<(x)<<endl;
#define LINE cerr<<"LINE: "<<__LINE__<<endl;

inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
template<class T> void preview(T a,T b){for(T it=a;it!=b;++it)cerr<<*it<<" ";cerr<<endl;}

const int INF = 100000000;
const double PI = acos(-1.0), EPS = 1e-10;

int main() {
	for(int n, p; cin >> n >> p, n || p;) {
		vector<int> v(n, 0);

		int player = 0;
		int win;
		while(true) {
			if(p == 0) {
				p += v[player];
				v[player] = 0;
			} else {
				p--;
				v[player]++;
				if(p == 0 && accumulate(all(v), 0) - v[player] == 0) {
					win = player;
					break;
				}
			}

			player++;
			player %= n;
		}
		cout << win << endl;
	}
	return 0;
}

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

#define loop(lp) for(int i = 0; (i) < (lp); (i) ++)
#define ascent(i, a, b) for(int i = (a); (i) < (b); (i) ++)
#define descent(i, a, b) for(int i = (a); (i) > (b); (i) --)

#define pb push_back

using namespace std;

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

    int stone[50];
    ascent(i, 0, 50) {
      stone[i] = 0;
    }
    
    //  stone[0] = p;
    int i = 0;
    while(true) {
      if(p > 0) {
	stone[i] ++;
	p --;
	
	if(p == 0) {
	  bool flg = true;
	  ascent(j, 0, n) {
	    if(j != i && stone[j] > 0) flg = false;
	  }
	  if(flg) break;
	}
      }
      else if(p == 0) {
	p += stone[i];
	stone[i] = 0;
      }
      
      i ++;
      i = i%n;
    }
    
    cout << i << endl;
  }
  return 0;
}

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


using namespace std;

int main(){
  int n,p,pp,flag;
  while(1){
    cin >> n >> p;
    pp=p;
    flag=0;
    if (n==0) return 0;
    vector<int> hv(n,0);
    for(int turn=0;turn<1000000;turn++){
      if (flag==1) break;
      if (p>0){
        p--;
        hv[turn%n]++;
        if (p==0 && hv[turn%n]==pp){
          cout << turn % n << endl;
          flag=1;
        }
      } else {
        p = hv[turn%n];
        hv[turn%n]=0;
      }
    }
  }
}

C++: #include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  int n, p, all;
  vector<int> r;
  while (cin >> n >> p && n && p) {
    bool flag = false;
    all = p;
    map<int, int> m;
    rep(i, n) m[i] = 0;
    while (!flag) {
      // cout << n << ' ' << p << endl;
      rep(i, m.size()) {
        if (p > 0) {
          p--;
#include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  int n, p, all;
  vector<int> r;
  while (cin >> n >> p && n && p) {
    bool flag = false;
    all = p;
    map<int, int> m;
    rep(i, n) m[i] = 0;
    while (!flag) {
      // cout << n << ' ' << p << endl;
      rep(i, m.size()) {
        if (p > 0) {
          p--;
          m[i]++;
        } else {
          p += m[i];
          m[i] = 0;
        }
      }
      rep(i, m.size()) {
        if (m[i] == all) {
          r.push_back(i);
          flag = true;
        }
      }
    }
  }
  rep(i, r.size()) { cout << r[i] << endl; }
  return 0;
}
          m[i]++;
        } else {
          p += m[i];
          m[i] = 0;
        }
      }
      rep(i, m.size()) {
        if (m[i] == all) {
          r.push_back(i);
          flag = true;
        }
      }
    }
  }
  for (auto i : r) {
    cout << i << endl;
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

main(){
  int n, p, data[50], wan;
  while(cin >> n >> p && (n|p)){
    for(int i=0;i<50;i++) data[i] = 0;
    wan = p;
    int pos = 0;
    for(;;){
      if(wan){
        if(++data[pos] == p) break;
        wan--;
      }else{
        wan += data[pos];
        data[pos] = 0;
      }
      pos = (pos + 1) % n;
    }
    cout << pos << endl;
  }
}

C++: #include<iostream>
using namespace std;
int main() {
	int n, p;
	while (cin >> n >> p, n, p) {
		int k[60] = {};
		int w = p, now = 0;
		while (true) {
			if (w) {
				k[now]++; w--;
				if ((!w)&&k[now] == p) {
					cout << now << endl;
					goto end;
				}
			}
			else if (!w) {
				w = k[now];
				k[now] = 0;
			}
			now++;
			if (now == n)now = 0;
		}
	end:;
	}
}

C++: #include <iostream>
#include <string>
#include <queue>
#include <numeric>
#include <algorithm>
using namespace std;

int main(){

	int n, p;
	while(cin >> n >> p, n|p){

		vector<int> people(n);
		int stone = p;
		bool finish = false;
		for(int i=0; !finish; i++){
			i = i%n;
			if(stone != 0){
				people[i]++;
				stone--;
			}else{
				stone += people[i];
				people[i] = 0;
			}
			if(people[i] == p){
				cout << i << endl;
				finish = true;
			}
		}
	}
	return 0;
}