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 <iostream>
using namespace std;

int main(){
	while(1){
		int n, p;
		scanf(" %d %d", &n, &p);
		if(n==0) break;
		
		int cand[50]; //candidate
		for(int i=0; i<n; ++i) cand[i]=0;
		
		int now=0;
		int stone=p;
		//ゲームシミュレーション
		while(1){
			//printf("situ: p=%d, now=%d, has %d\n", p, now, cand[now]);
			
			if(p==0){
				p=cand[now];
				cand[now]=0;		
			}
			else{ //石を１個取り次の人へ
				p--;
				cand[now]++;
				if(cand[now]==stone) break; //すべての石が１人のところにある
			}
			
			now=(now+1)%n;		
		}
		
		printf("%d\n", now);
	}
	return 0;
}

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

int n,p, c[51];

inline int next(int a) {
	return a == n - 1 ? 0 : a + 1;
}

int main(){
	int it;
	while (scanf("%d%d", &n, &p), n != 0) {
		for (int i = 0; i < 51; i++) {
			c[i] = 0;
		}
		it = 0;
		c[n] = p;
		while (true) {
			if (c[n] > 0) {
				c[it]++;
				c[n]--;
				if (c[it] == p) {
					printf("%d\n", it);
					break;
				}
			}
			else {
				c[n] += c[it];
				c[it] = 0;
			}
			it = next(it);
		}
	}
	
	return 0;
}

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

const int MAX_N=51;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,p,stone;
    vector<int> have(MAX_N);
    while(cin >> n >> p,n,p){
        for (int i=0;i<MAX_N;++i) have[i]=0;
        stone=p;
        for (int i=0;;i=(i+1)%n){
            if (have[i]==p){
                cout << i << endl;
                break;
            }
            if (stone) ++have[i],--stone;
            else if (have[i]) stone+=have[i],have[i]=0;
        }
    }
}

C++: #include<iostream>
using namespace std;
int main(){
    while(1){
        int stone[50]={},p,n,flag,winer=-1;
        cin >> n >> p;
        if(n==0&&p==0)  break;
        while(winer<0){
            for(int i=0;i<n;i++){
                flag = 0;
                for(int j=0;j<n;j++){
                    if(stone[j]!=0&&j!=i){
                        flag=1;
                        break;
                    }
                }
                
                if(p==1&&flag==0){
                    winer = i;
                    break;
                }
                else if(p==0) {
                    p+=stone[i];
                    stone[i]=0;
                }
                else{
                    p--;
                    stone[i]++;
                }
            }
        }
        cout << winer << "\n";
    }
}

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

int main(){

int N,M;
int n[51]={};
int i=0,p=0;
while(1){
scanf("%d %d",&N,&M);
if(N==0&&M==0)break;
for(int k=0;k<51;k++)
	n[k]=0;

p=M;i=0;
while(n[i]!=M)
{
if(p==0&&n[i]>0){p=n[i];n[i]=0;}
//else if(p==0&&n[i]==0)
else if(p>0){p--;n[i]++;}
i++;i=i%N;
//if(i==N-1)i=0;
}
cout<<i<<endl;
}
return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define REP(i,a,n) for(int i = a ; i < n ; i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int main(){
  int n,p,tp;
  int cnt[52];

  while(cin>>n>>p,n||p){
    tp = p;
    memset(cnt,0,sizeof(cnt));

    int now = 0;

    while(1){
      if(p > 0){
        cnt[now]++;
        p--;

        if(cnt[now] == tp){
          cout<<now<<endl;
          break;
        }
      }
      else{
        p += cnt[now];
        cnt[now] = 0;
      }

      now = (now + 1) % n;
    }
  }
}

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

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

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
    int n,p;
    while(scanf("%d %d",&n,&p),n!=0) {
        int wan=p;
        int idx=0,stone[50]={0};
        while(1) {
            if(wan>0) {
                stone[idx]++;
                wan--;
                if(stone[idx]==p) break;
            }else {
                wan=stone[idx];
                stone[idx]=0;
            }
            idx=(idx+1)%n;
        }
        printf("%d\n",idx);
    }
}

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

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

using namespace std;

long long INF=1e15;

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

    while(1){
        int n,p;
        cin>>n>>p;
        if(n+p==0)break;
        int pos=0;
        vector<int> a(n);
        bool flag=true;
        while(flag){
            if(p>0){
                a[pos]++;
                p--;
            }
            else{
                if(a[pos]>0){
                    p=a[pos];
                    a[pos]=0;
                }
            }

            bool f=(p==0);

            for(int i=0;i<n;i++){
                if(i==pos)continue;
                if(a[i]!=0)f=false;
            }

            if(f){
                cout<<pos<<endl;
                flag=false;
            }
            pos++;
            pos%=n;
        }
    }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int d[n],t=0,cnt=p;
		for(int i=0;i<n;i++)
		d[i]=0;
		while(1){
			if(d[t]==p)
			break;
			if(cnt==0){
				cnt=d[t];
				d[t]=0;
			}else{
				cnt--;
				d[t]++;
			}
			t++;
			if(t==n)
			t=0;
		}
		cout<<t<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <functional>
#include <sstream>
#include <complex>
#include <climits>
using namespace std;

#define REP(i,a,n) for(int i=(a);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define EPS 1e-8

int main(){
	int n,p;
	int owan[64],temoto[64];
	while(scanf("%d%d",&n,&p),n|p){
		rep(i,64)owan[i]=temoto[i]=0;
		owan[0] = p;
		int cur = 0;
		for(int i=0; i<=1000000; i++){
			if( owan[cur]>0 ){
				owan[(cur+1)%n] = owan[cur]-1;
				temoto[cur]++;
				if( owan[cur]-1==0 ){
					if( temoto[cur]==p ){
						printf("%d\n",cur);
						break;
					}
				}
			}else{
				owan[(cur+1)%n] = temoto[cur];
				temoto[cur] = 0;
			}
			//printf("next:owan[%d]:%d , temoto[%d]:%d\n",(cur+1)%n,owan[(cur+1)%n],cur,temoto[cur]);
			cur = (cur+1)%n;
		}
	}
	return 0;
}