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

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 (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl

ll f(ll N, ll P){
    vector<ll> A(N);
    ll i = 0;

    // Pは定数
    // stoneは変更可能
    ll stone = P;

    for(;;){
        if(stone>0){
            A[i]++;
            stone--;
            if(A[i]==P){
                // win
                return i;
            }
        }
        else{
            stone += A[i];
            A[i] = 0;
        }

        // 次の人へ
        i++;
        i %= N;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    for(;;){
        ll N, P;
        cin >> N >> P;

        if(N==0 && P==0){
            return 0;
        }

        ll ans = f(N, P);
        p(ans);
    }
    
    return 0;
}

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

using namespace std;

#define rep(i,a) for(int i=0;i<a;i++)

int main(){
    int n,p;
    while(cin>>n>>p,n||p){
        int stone = p;
        int turn = 0;
        vector<int> cand(n,0);
        while(1){
            if(stone){
                cand[turn]++;
                stone--;
            }
            else{
                stone = cand[turn];
                cand[turn] = 0;
            }
            if(cand[turn] == p){
                cout << turn << endl;
                break;
            }
            turn++;
            turn %= n;
        }
    }
}

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

using namespace std;

int main(){

	int n,p;
	while(cin >> n >> p && !(n == 0 && p == 0)){
		int wan=0;
		int cand[50];
		int tern = 0;
		wan = p;
		fill(cand,cand+50,0);
		while(1){
			if(wan==0){
				wan += cand[tern];
				cand[tern]=0;
			}
			else{
				wan--;
				cand[tern]++;
				if(cand[tern] == p)
					break;
			}
			tern = (tern + 1) % n;
		}
		cout << tern << endl;
	}

	return 0;
}

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

using namespace std;

int main(){
  int n,p;

  while(cin>>n>>p,n||p){
    int i=0;
    vector<int> t(n,0);
    int pp=p;
    while(1){
      if(p>0){
	p--;
	t[i]++;
      }
      else if(p==0){
	p=t[i];
	t[i]=0;
      }
      
      if(t[i]==pp)
	break;

      i++;
      if(i==n)
	i=0;
    }
    cout<<i<<endl;
  }
}

C++: // sonntyou.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//

#include <iostream>
using namespace std;
int n, p;
int main(void) {
	while (1) {
		cin >> n >> p;
		int scr[50] = {}, wan = p;
		int i = 0;
		if (n == 0 && p == 0) break;
		while (1) {
			if (wan >= 1) {
				scr[i]++;
				wan--;
				if (wan == 0 && scr[i] == p) {
					cout << i << endl;
					break;
				}
			}
			else {
				wan += scr[i];
				scr[i] = 0;
			}
			i = (i + 1) % n;
		}
	}
	return 0;
}

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

using namespace std;

int solve(int n, int p){
    vector<int> m(n, 0);
    int i = 0, x = p;
    for(; m[i]!=p; i==n-1? i=0: ++i){
        if(x)m[i]++, x--; else x = m[i], m[i] = 0;
    }
    return i;
}

int main(){
    int n, p;
    while(cin >> n >> p, n|p)cout << solve(n, p) << '\n';
    return 0;
}

C++: // 2012/05/26 Tazoe

#include <iostream>
using namespace std;

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

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

		int B = p;
		int C[50] = {0};

		int i = 0;

		while(true){
			if(B>0){
				B--;
				C[i]++;
			}
			else{
				B = C[i];
				C[i] = 0;
			}

			if(C[i]==p)
				break;

			i = (i+1)%n;
		}

		cout << i << endl;
	}

	return 0;
}

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

using namespace std;
#define ll long long

#define rep(i, a) for (int (i) = 0; (i) < (int) (a); (i)++)
#define reps(i, a, b) for (int (i) = (int) (a); (i) < (int) (b); (i)++)
#define rrep(i, a) for (int (i) = (int) a-1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int (i) = (int) (a)-1; (i) >= (int) (b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x
#define TRUE__ "Yes"
#define FALSE__ "No"
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}

#ifdef LOCAL
	#define lcout(a) cout << a;
	#define lcoutln(a) cout << a << endl;
	#define lcerr(a) cerr << a;
	#define lcerrln(a) cerr << a << endl;
#else
	#define lcout(a) 
	#define lcoutln(a) 
	#define lcerr(a) 
	#define lcerrln(a) 
#endif

int N, P;
int tyo[50];
int pos;
int wan;

signed main()
{
	cin >> N >> P;
	while (N) {
		wan = P;
		pos = 0;
		rep(i, N) tyo[i] = 0;
		while (true) {
			if (wan) {
				wan--;
				tyo[pos]++;
			} else {
				wan = tyo[pos];
				tyo[pos] = 0;
			}
			if (tyo[pos] == P) break;
			pos = (pos+1) % N;
		}
		cout << pos << endl;
		cin >> N >> P;
	}	
}

C++: #include <iostream>
using namespace std;
int n,m,dp[50],cnt;
int main(void){
    while(1){
    cin>>n>>m;
    if(n==0&&m==0)return 0;
    for(int j=0;j<50;j++)dp[j]=0;
    int i=0;
    cnt=m;
    while(1){
        if(m>0){
            dp[i]++;
            m--;
        }
        else if(m==0&&dp[i]>0){
            m+=dp[i];
            dp[i]=0;
        }
        if(dp[i]==cnt)break;
        i=(i+1)%n;
    }
    cout<<i<<endl;
    }
}

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

int main(){
	int n, p;
	while( cin >> n >> p , n || p ){
		vector<int> v(n, 0);
		int M = p, k = 0, ans;
		while( true ){
			if( p ){
				p--;
				v[k]++;
				if( p == 0 && v[k] == M ){
					ans = k;
					break;
				}
			}else{
				p += v[k];
				v[k] = 0;
			}
			k++;
			if( k == n ) k = 0;
		}
		cout << ans << endl;
	}
}

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

int main(){
	int mayors[50]{0};
	vector<int> ansvec;

	for(;;){
		int n, p;
		cin >> n >> p;
		if (n == 0) break;

		for (int i = 0; i < n; i++) mayors[i] = 0;
		int bowlp = p;
		int turn = 0;
		for(;;){
			if (bowlp == 1 && mayors[turn] == p - 1) break;
			else if (bowlp == 0){
				bowlp += mayors[turn];
				mayors[turn] = 0;
			}
			else{
				mayors[turn]++;
				bowlp--;
			}

			if (turn == n - 1) turn = 0;
			else turn++;
		}
	}

	for (int ans : ansvec){
		cout << ans << endl;
	}
	return 0;
}