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 owan, koho[50];
	int n, i ,p;

	while (cin >> n >> p){							//????£????&?°????'s ??°

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

		for (i = 0; i < 50; i++){					//????????????????????¢??¬
			koho[i] = 0;
		}

		owan = p;

		for (i = 0;; i = (i + 1) % n){				//????£??????°??§?????????????¶????????????????

			if (owan == 0){							//?°???????????????????
				owan = koho[i];						//????????£??????????????????????????£???
				koho[i] = 0;
			}else{									//?°???????????????????
				owan--;								//????????£??????????????????????????£???(?????????)
				koho[i]++;							//???????¢?????????????

				if (owan == 0 && p == koho[i]){		//???????????£???&????????¨??¨?????£??????
					cout << i << endl;
					break;

				}
			}
		}
	}
	return 0;
}

C++: #include<iostream>

using namespace std;
int cd[100];

int main(){

    while(1){
        int n, p;
        cin>> n>> p;
        if(n==0&&p==0) break;
        for(int i=0; i<n; i++) cd[i]=0;
        int turn=0;
        while(1){
            int c=0, ans;
            for(int i=0; i<n; i++){
                if(cd[i]!=0&&p==0){
                    ans=i;
                }else{
                    c++;
                }
            }
            if(c==n-1){
                cout<< ans<< endl;
                break;
            }
            if(p!=0){
                p--;
                cd[turn]++;
            }else{
                p+=cd[turn];
                cd[turn]=0;
            }
            turn=(turn+1)%n;
        }
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define int             long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )    for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )  FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP              make_pair

typedef pair<int, int> P;

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

const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main(){
    int n, p;
    while( cin >> n >> p, n | p ){
        vector<int> cand( n );
        int win = -1, stone = p;
        for( int i = 0; ; i++ ){
            if( stone > 0 ){
                cand[i % n]++;
                stone--;
            }else{
                FOR( j, i, n + i  ){
                    if( cand[j % n] ){
                        stone = cand[j % n];
                        if( stone == p ) win = j % n;
                        cand[j % n] = 0;
                        i = j;
                        break;
                    }
                }
            }
            if( win != -1 ){
                cout << win << endl;
                break;
            }
        }
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

int field, hand[50];

int main()
{
	int n, p;
	while (scanf("%d %d", &n, &p), n + p){
		field = p;
		memset(hand, 0, sizeof(hand));
		
		int now = 0;
		int res = 0;
		while (true){
			if (field > 0){
				field--;
				hand[now]++;
				
				if (field == 0 && hand[now] == p){
					res = now;
					break;
				}
			}
			else {
				field += hand[now];
				hand[now] = 0;
			}
			
			now = (now + 1) % n;
		}
		
		printf("%d\n", now);
	}
	
	return 0;
}

C++: #include<iostream>

using namespace std;

int main() {

	int store[50];
	int pos;
	int move;

	int cnum;
	int snum;

	int stop = 0;
	for (;;) {
		cin >> cnum >> snum;

		if ( cnum == 0 ) break;

		pos = 0;
		move = snum;
		for ( int i=0; i<cnum; i++ ) {
			store[i] =0;
		}

		for ( int i=0;;i++) {
			if ( move > 0 ) {
				for ( int j=0; j<cnum; j++ ) {
				store[(i+j)%cnum]+=move/cnum;
				}
				for ( int j=0; j<(move%cnum);j++) {
					store[(i+j)%cnum]++;
				}
				i+=move-1;
				move = 0;
			}
			else {
				if ( store[i%cnum] == snum ) {
					pos = i % cnum;
					break;
				} else {
					move = store[i%cnum];
					store[i%cnum] = 0;
				}
			}
		}
		cout << pos << endl;
	}
}

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

    for(int i = 0 ; i < 10000000 ; i++){
        for(int j = 0 ; j < 10000000 ; j++){
            for(int k = 0 ; k < 10000000 ; k++){
                for(int l = 0 ; l < 10000000 ; l++){
                    int K = l + i + j + k;
                }
            }
        }    
    }


    cout << ans << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int n,p;
  int per[50];
  int wan;
  int idx;
  while(cin>>n>>p,n|p){
    memset(per,0,sizeof(per));
    wan=p;
    idx=0;
    while(1){
      if(idx==n)idx=0;
      if(wan){
	wan--;
	per[idx]++;
	if(per[idx]==p)break;
      }
      else{
	wan=per[idx];
	per[idx]=0;
      }
      idx++;
    }
    cout<<idx<<endl;
  }
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<iomanip>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{"; for(auto nth : list){ cout << nth << " ";}cout << "}" << endl;

using namespace std;

int main(){

	while(true){
		int a,b;
		cin >> a >> b;
				
		if(a==0&&b==0) break;
		

		int people[a];
		int stone=b;
		for(int i=0;i<(sizeof(people)/sizeof(int));i++){
			people[i]=0;
		}

		
		int count=0;
		bool flag = false;
		while(true){
			for(int i=0;i<(sizeof(people)/sizeof(int));i++){
				if(people[i]==b){
					cout << i << endl;
					flag = true;	
				}
			}
			
			if(flag){
				break;
			}

			if(stone>0){
				people[count]++;
				stone--;
			}
			else if(stone<=0){
				stone+=people[count];
				people[count]=0;
			}

			count++;
			if(count>=a) count=0;
		}
	
	}
}

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

using namespace std;

int main() {
	int n, p;
	while (cin >> n >> p && (n || p)) {
		vector<int> a(n, 0);
		int i = 0, num = p;
		while (1) {
			if (p != 0) {
				a[i]++;
				p--;
				if (a[i] == num) {
					cout << i << endl;
					break;
				}
			} else if (a[i] != 0) {
				p = a[i];
				a[i] = 0;
			}
			i++;
			if (i >= n) i = 0;
		}
	}
	return 0;
}