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 <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> VI;

int main ( void )
{
	int n, p;
	while ( cin >> n >> p, n | p ) {
		VI user( n, 0 );
		int size = p,
			turn = 0;

		while ( 1 ) {
			if ( p > 0 ) {
				p--;
				++user[turn];
				if ( p == 0 ) {
					bool end = false;
					for ( int i = 0; i < n; ++i ) {
						end |= user[i] == size;
					}
					if ( end ) {
						break;
					}
				}
			}
			else {
				p += user[turn];
				user[turn] = 0;
			}
			turn = ( turn + 1 ) % n;
		}
		cout << turn << endl;
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iterator>
#include <cstring>

using namespace std;
const int INF = (1<<30);
typedef pair<int, int> P;
typedef long long ll;
#define PB push_back
#define rep(i, a) for (int i = 0; i < a; i++)

int sx, sy;

int gx, gy;

int qqq;

//各座標までの最短距離
int d[100][100];


//移動方向のベクトル
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };

//汎用
ll ans = 0;
pair<int, int> red[101], blue[101];
ll BIG = 10000000000;

//累乗
ll POW(ll a, ll b) {
ll c = 1;
for (int i = 0; i < b; ++i) {
c *= a;
}
return c;
}

//階乗
int Kaijou(int a) {
if (a < 2) return 1;
return a * Kaijou(a - 1);
}


int A[200000];
int kinds[200001];


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

		int stone = p;
		vector<int> s(n);
		rep(i, n) s[i] = 0;
        int turn = 0;

		while (1) {

			if (stone) {
				s[turn]++;
				stone--;
				if (s[turn] == p) break;
			}
			else {
				if (s[turn]) {
					stone += s[turn];
					s[turn] = 0;
				}
			}

			turn = (turn + 1) % n;
		}

		cout << turn << endl;
	}

	cin >> qqq;
}

C++: #include<iostream>

using namespace std;

void shokika(int d[]){
	for(int i=0;i<51;i++){
		d[i]=0;
	}
}

int main(){
	int n,p,stoneNum;
	int haveStone[51];
	while(cin>>n>>p, n&&p){
		
		shokika(haveStone);
		stoneNum=p;
		
		for(int j=0;j<=n;j++){
			
			if(j==n){
				j=0;
			}
			
			//お椀が空の時
			if(p==0){
				//全ての石を自分で持っていないなら
				if(haveStone[j]!=stoneNum){
					p=haveStone[j];
					haveStone[j]=0;
				}
			}else{
				haveStone[j]++;
				p--;
			}
			//全て自分で持っているなら
			if(haveStone[j]==stoneNum){
				cout<<j<<endl;
				break;
			}
		}
	}
}

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

int main(){
	int N,P,R,P2,P3[100],A;
	R=0;
	while(true){
		cin>>N>>P;
		P2=P;
		A=0;
		memset(P3,0,sizeof(P3));
		if(N==0 && P==0){
			break;
		}
		while(true){
			for(int i=0;i<N;i++){
				if(P3[i]==P){
					R=i;
					goto Exit;
				}
			}
			if(P2==0){
				P2=P3[A%N];
				P3[A%N]=0;
			}
			else{
				P2 -= 1;
				P3[A%N] += 1;
			}
			A++;
		}
		Exit:
		cout<<R<<endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<bitset>
#include<utility>
#include<sstream>
#include<iomanip>
using namespace std;

#define ll long long
#define MP make_pair

int main(void) {
    int n,p,ans;
    while(1){
        cin>>n>>p;
        if(n==0&&p==0)break;
        int a[50]={};
        int jud=0;
        while(1){
            for (int i = 0; i < n; i++) {
                if(p>0){
                    p--;
                    a[i]++;
                }else if(p==0){
                    p+=a[i];
                    a[i]=0;
                }
                jud=0;
                for (int ji = 0; ji < n; ji++) {
                    if(a[ji]!=0)jud=1;
                }

                if(jud==0){
                    ans=i;
                    break;
                }
            }
            if(jud==0)break;
        }
        cout<<ans<<"\n";

    }
    
    
    return 0;
}

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

using namespace std;

bool is_game_end(uint32_t num, vector<int32_t> &players)
{
  uint32_t has_stones = 0;
  uint32_t count = 0;

  for(uint32_t i = 0; i < players.size(); i++){
    if(players[i] > 0){
      has_stones++;
    }
    count += players[i];
  }

  return has_stones == 1 && count == num;
}

int32_t main()
{
  while(true){
    int32_t n, p;
    cin >> n >> p;
    if(n == 0) break;
    vector<int32_t> players(n, 0);

    int32_t in_pot = p;
    do{
      for(uint32_t i = 0; i < players.size(); i++){
        if(in_pot > 0){
          players[i]++;
          in_pot--;
        } else {
          in_pot += players[i];
          players[i] = 0;
        }
      }
    } while(!is_game_end(p, players));

    for(uint32_t i = 0; i < players.size(); i++){
      if(players[i] > 0){
        cout << i << endl;
      }
    }
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int cand[52];
int n, p;

bool is_game_over (void ){
	rep (i, n ){
		if (cand[i] == p ) return true;
	} // end rep

	return false;
}

int main()
{
	ios_base::sync_with_stdio(0);
	while (cin >> n >> p, n ){
		memset (cand, 0, sizeof (cand ) );
		int res = -1;
		int i = 0, j = p;
		while (true ){
			if (j > 0 ){
				cand[i%n]++;
				j--;
				if (j == 0 && is_game_over () ){
					res = i%n;
					break;
				} // end if
				i++;
			}else{
				j = cand[i%n];
				cand[i%n] = 0;
				i++;
			} // end if
		} // end while
		cout << res << endl;
	} // end while		
		
	return 0;
}

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

int main(){
int n,p;
int c[50];
for(;;){
cin >> n >> p;
if(!n && !p)break;
for(int i=0;i<n;i++)c[i] = 0;

int pos = -1;
for(;;){
pos = (pos+1)%n;
if(p){
c[pos]++;
p--;
}else{
p = c[pos];
c[pos] = 0;
}
if(!P){
int cnt = 0;
for(int i=0;i<n;i++)
if(c[i])cnt++;
if(cnt==1)break;
}
}
cout << pos << endl;
}
}

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

int main(){
  int n;
  int p;
  while(cin>>n>>p && n+p){
    vector<int> vec(n);
    int stone=p;
    int pos = 0;
    while(true){
      int i=pos%n;
      pos++;
      if(stone == 0 && vec[i] == p){
	cout<<i<<endl;
	break;
      } 
      if(stone <= 0){
	stone = vec[i];
	vec[i]=0;
      }
      else{
	stone--;
	vec[i]++;
      }
    }
  }
  return 0;
}