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 <stdio.h> 
#include <stdlib.h> 

int main(){ 

// 各種変数宣言 

    int n,p,i,j,k,c,a=0; int count=0;

// 配列宣言
    int cand[1000],peb;

// メインループ 

    while(1){


// nとpを読み込む
    scanf("%d",&n);
    scanf("%d",&p);


// 否定条件
    if( (n==0) && (p==0) ) break;
       
// 石の個数を初期化
    for(i=0;i<n;i++){
        cand[i]=0;
        
    }
        //椀の中の石の数
    peb=p;

// 候補者の１動作
    j=0;
    while(1){
        if(peb>0){      //椀に石がある場合
            cand[j]++;
            peb--;
            
        }
        else if(peb==0){  //椀に石がない場合
            count=0;
            for(i=0;i<n;i++){  //手持ちに石がない人の数を数える
                if(cand[i]==0){
                    count++;
                    
                }
                
            }
            
            if(count==n-1)break;//一人だけ石を持っている状態ならbreak
            
            peb=cand[j];  //手持ちに石を持っている人が複数人いて、椀に石が０の時の動作
            cand[j]=0;
            
        }
        
        j++;
        if(j==n)
            j=0;
        
    }
    
    for(i=0;i<n;i++){    //全ての石を持っている人の番号を表示
        if(cand[i]==p)
            printf("%d\n",i);
        
    }
       
    }
       
       
       
// 終了
    return 0;
    
}

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

int main(){
	int n,p;
	while(cin>>n>>p and n>0){
		int win=p;
		int a[n+1]={};
		a[0]=1; p--;
		int i=1,ans;
		while(true){
			if(p>0){
				p--; a[i]++;
				if(a[i]==win){
					ans=i; break;
				}
			}else{
				p=a[i]; a[i]=0;
			}
			i++;
			if(i==n) i=0;
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string.h>

using namespace std;

int main()
{
	int n,p;
	int stone[50];
	
	cin>> n >> p;
	
	while ( n+p > 0)
	{
		int bowl = p;
		memset(stone,0,sizeof(stone));
		for (int i=0;i<1000000;++i)
		{
			if (stone[i % n] == p) 
			{
				cout << (i%n) << endl;
				break;
			}
			
			if (bowl > 0) 
			{
				++stone[i%n];
				--bowl;
			}
			else if (stone[i%n] > 0)
			{
				bowl = stone[i%n];
				stone[i%n] = 0;
			}
			
		}
		
		cin>> n >> p;
	}
}

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

int n;
int p;

int main() {
  while (1) {
    int nums[50] = {0};

    scanf("%d%d", &n, &p);
    if (n == 0 && p == 0) return 0;

    int defp = p;
    int idx = 0;
    while (1) {
      if (p > 0) {
        nums[idx]++;
        p--;
        if (nums[idx] == defp) break;
      } else {
        p = nums[idx];
        nums[idx] = 0;
      }

      idx = (idx+1)%n;
    }

    printf("%d\n", idx);
  }
}

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

int c[50];
int main() {
  int n, p, a, b;
  while (cin >> n && n) {
    a = 0;
    cin >> p;
    for (int i = 0; i < n; i++) c[i] = 0;
    b = p;
    c[a] = 1, p--;
    while (c[a] != b) {
      a = (a == (n - 1)) ? 0 : ++a;
      if (p == 0) {
        p += c[a], c[a] = 0;
      } else
        c[a]++, p--;
    }
    cout << a << endl;
  }
  return 0;
}

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

int main(int argc, char const* argv[])
{
  while (true) {
    int n, p;
    std::cin >> n >> p;
    if (n == 0 && p == 0) {
      break;
    }
    int pebbles = p;
    std::vector<int> mayor(n);
    int turn = 0;
    while (true) {
      if (pebbles == 0) {
        pebbles = mayor[turn];
        mayor[turn] = 0;
      } else {
        ++mayor[turn];
        --pebbles;
        if (mayor[turn] == p) {
          std::cout << turn << std::endl;
          break;
        }
      }
      turn = (turn + 1) % n;
    }
  }
  return 0;
}

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

#define int long long
#define DEBUG(x) cerr <<#x<<":"<<x<<endl;
#define DEBUG2(x, y) cerr <<#x<<":"<<x<<" "<<#y<<":"<<y<<endl;
#define DEBUG3(x, y, z) cerr <<#x<<":"<<x<<" "<<#y<<":"<<y<<" "<<#z<<":"<<z<<endl;
#define DEBUGVC(x) cerr <<#x<<": "; for(auto a:x) cerr << a <<" "; cerr << endl;
#define DEBUGMP(x) cerr <<#x<<":"<<endl; for(auto a:x) cerr << a.first <<" "<< a.second << endl;
#define MAX_N (1000006)
#define INF (1LL << 60)
const int MOD = (int)1e9+7;


signed main() {
    while(1) {
        int n, p;
        cin >> n >> p;
        if(n==0 && p==0) break;
        vector<int> c(n, 0LL);
        int idx = 0;
        int q = p;
        while(1) {
            //DEBUG2(idx, p);
            //DEBUGVC(c);
            if(p!=0) {
                c[idx]+=1;
                p--;
                if(p==0){
                    if(c[idx]==q){
                        cout << idx << endl;
                        break;
                    }
                }
            }
            else if(p==0) {
                p += c[idx];
                c[idx] = 0;
            }
            idx++;
            idx %= n;
        }
    }
}

C++: #include <iostream>
//#include <algorithm>
//#include <vector>
//#include <stack>
//#include <queue>
//#include <map>
//#include <cmath>
//#include <string>
//#include <sstream>
//#include <iomanip>
//#include <complex>
using namespace std;
 
//#define ll long long
//#define vvi vector< vector<int> >
//#define All(X) X.begin(),X.end()
//#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
//#define pb push_back 
//ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
 
int main(){
  while(1){
    int n, p;
    cin >> n >> p;
    if(n==0&&p==0) break;
    int kago=p;
    int pc[n-1];
    int i;
    while(1){
      int k=i%(n-1);
      if(pc[k]==p){
	cout << k << lend;
	break;}
      if(kago>0){
	kago--;
	pc[k]++;}
      else{
	kago=pc[k];
	pc[k]=0;}
      i++;}
  }

  return 0;
}

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

int main(){
	int people;
	int stone;
	int hand[55];

	while(1){
		int j=0;
		for(int i=0; i < 50; i++) hand[i] = 0;

		cin >> people >> stone;

		if((people|stone) == 0) break;

		int box = stone;

		while(1){
			if(box == 1){
				int cont = 0;
				for(int i=0; i < people; i++){
					if(hand[i] == 0) cont++;
				}
				if(people == cont+1) break;
				else{
					hand[j % people]++;
					box--;
				}
			}
			else if(box != 0){
				hand[j % people]++;
				box--;
			}
			else if(box == 0){
				box += hand[j % people];
				hand[j % people] = 0;
			}
			j++;
		}
		int ans = j % people;
		cout << ans << endl;
	}
	return 0;
}

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 cup=p;
		int num=0;
		while(1){
			if(cup!=0){
				v[num]++;
				cup--;
			}
			else if(v[num]!=0){
				cup=v[num];
				v[num]=0;
			}
			if(v[num]==p)
				break;
			num++;
			num%=n;
		}
		cout << num << endl;
	}
	return 0;
}